jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/335182 )

Change subject: Update: improve WikiSite.authorityToLanguageCode() robustness
......................................................................


Update: improve WikiSite.authorityToLanguageCode() robustness

Return correct outputs for no and mobile subdomain authority inputs. Add
some tests to WikiSite

Bug: T152980
Change-Id: I2a6075b746b36462086a8aa639d06d6bb6e1c275
---
M app/src/main/java/org/wikipedia/dataclient/WikiSite.java
M app/src/test/java/org/wikipedia/dataclient/WikiSiteTest.java
2 files changed, 193 insertions(+), 24 deletions(-)

Approvals:
  Dbrant: Looks good to me, approved
  jenkins-bot: Verified
  Mholloway: Looks good to me, but someone else must approve



diff --git a/app/src/main/java/org/wikipedia/dataclient/WikiSite.java 
b/app/src/main/java/org/wikipedia/dataclient/WikiSite.java
index ceaae17..fa58902 100644
--- a/app/src/main/java/org/wikipedia/dataclient/WikiSite.java
+++ b/app/src/main/java/org/wikipedia/dataclient/WikiSite.java
@@ -29,6 +29,15 @@
  *     <li>Simple English Wikipedia (beta cluster mirror): HTTP / 
simple.wikipedia.beta.wmflabs.org / simple</li>
  *     <li>Development: HTTP / 192.168.1.11:8080 / (none)</li>
  * </ul>
+ *
+ * <strong>As shown above, the language code or mapping is part of the 
authority:</strong>
+ * <ul>
+ *     <lh>Validity: authority / language code</lh>
+ *     <li>Correct: "test.wikipedia.org" / "test"</li>
+ *     <li>Correct: "wikipedia.org", ""</li>
+ *     <li>Correct: "no.wikipedia.org", "nb"</li>
+ *     <li>Incorrect: "wikipedia.org", "test"</li>
+ * </ul>
  */
 public class WikiSite implements Parcelable {
     public static final Parcelable.Creator<WikiSite> CREATOR = new 
Parcelable.Creator<WikiSite>() {
@@ -138,7 +147,7 @@
      *     <li>Võro Wikipedia: fiu-vro.m.wikipedia.org</li>
      *     <li>Simple English Wikipedia: simple.m.wikipedia.org</li>
      *     <li>Simple English Wikipedia (beta cluster mirror): 
simple.m.wikipedia.beta.wmflabs.org</li>
-     *     <li>Development: m.192.168.1.11:8080</li>
+     *     <li>Development: m.192.168.1.11</li>
      * </ul>
      */
     @NonNull
@@ -146,6 +155,7 @@
         return authorityToMobile(host());
     }
 
+    /**  @return the port if specified or -1 if invalid or not present */
     public int port() {
         return uri.getPort();
     }
@@ -162,7 +172,7 @@
      * @return The canonical URL. e.g., https://en.wikipedia.org.
      */
     public String url() {
-        return scheme() + "://" + authority();
+        return uri.toString();
     }
 
     /**
@@ -274,9 +284,17 @@
         }
     }
 
-    @NonNull
-    private static String authorityToLanguageCode(@NonNull String authority) {
-        return authority.split("\\.")[0];
+    @NonNull private static String authorityToLanguageCode(@NonNull String 
authority) {
+        String[] parts = authority.split("\\.");
+        final int minLengthForSubdomain = 3;
+        if (parts.length < minLengthForSubdomain
+                || parts.length == minLengthForSubdomain && 
parts[0].equals("m")) {
+            // ""
+            // wikipedia.org
+            // m.wikipedia.org
+            return "";
+        }
+        return parts[0];
     }
 
     /** @param authority Host and optional port. */
diff --git a/app/src/test/java/org/wikipedia/dataclient/WikiSiteTest.java 
b/app/src/test/java/org/wikipedia/dataclient/WikiSiteTest.java
index bfbc3cf..ceb65a0 100644
--- a/app/src/test/java/org/wikipedia/dataclient/WikiSiteTest.java
+++ b/app/src/test/java/org/wikipedia/dataclient/WikiSiteTest.java
@@ -13,14 +13,173 @@
 import static org.hamcrest.Matchers.not;
 
 @RunWith(TestRunner.class) public class WikiSiteTest {
+    @Test public void testSupportedAuthority() {
+        assertThat(WikiSite.supportedAuthority("fr.wikipedia.org"), is(true));
+        assertThat(WikiSite.supportedAuthority("fr.m.wikipedia.org"), 
is(true));
+        assertThat(WikiSite.supportedAuthority("roa-rup.wikipedia.org"), 
is(true));
+
+        assertThat(WikiSite.supportedAuthority("google.com"), is(false));
+    }
+
+    @Test public void testForLanguageCodeScheme() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.scheme(), is("https"));
+    }
+
+    @Test public void testForLanguageCodeAuthority() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.authority(), is("test.wikipedia.org"));
+    }
+
+    @Test public void testForLanguageCodeLanguage() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.languageCode(), is("test"));
+    }
+
+    @Test public void testForLanguageCodeNoLanguage() {
+        WikiSite subject = WikiSite.forLanguageCode("");
+        assertThat(subject.languageCode(), is(""));
+    }
+
+    @Test public void testForLanguageCodeNoLanguageAuthority() {
+        WikiSite subject = WikiSite.forLanguageCode("");
+        assertThat(subject.authority(), is("wikipedia.org"));
+    }
+
+    @Test public void testForLanguageCodeLanguageAuthority() {
+        WikiSite subject = WikiSite.forLanguageCode("zh-hans");
+        assertThat(subject.authority(), is("zh.wikipedia.org"));
+    }
+
+    @Test public void testCtorScheme() {
+        WikiSite subject = new WikiSite(false, 
"simple.wikipedia.beta.wmflabs.org", "simple");
+        assertThat(subject.secureScheme(), is(false));
+    }
+
+    @Test public void testCtorNoScheme() {
+        WikiSite subject = new WikiSite("wikipedia.org");
+        assertThat(subject.secureScheme(), is(true));
+    }
+
+    @Test public void testCtorAuthority() {
+        WikiSite subject = new WikiSite("test.wikipedia.org");
+        assertThat(subject.authority(), is("test.wikipedia.org"));
+    }
+
+    @Test public void testCtorAuthorityLanguage() {
+        WikiSite subject = new WikiSite("test.wikipedia.org");
+        assertThat(subject.languageCode(), is("test"));
+    }
+
+    @Test public void testCtorAuthorityNoLanguage() {
+        WikiSite subject = new WikiSite("wikipedia.org");
+        assertThat(subject.languageCode(), is(""));
+    }
+
+    @Test public void testCtorMobileAuthorityLanguage() {
+        WikiSite subject = new WikiSite("test.m.wikipedia.org");
+        assertThat(subject.languageCode(), is("test"));
+    }
+
+    @Test public void testCtorMobileAuthorityNoLanguage() {
+        WikiSite subject = new WikiSite("m.wikipedia.org");
+        assertThat(subject.languageCode(), is(""));
+    }
+
     @Test public void testCtorParcel() throws Throwable {
         WikiSite subject = WikiSite.forLanguageCode("test");
         TestParcelUtil.test(subject);
     }
 
-    @Test public void testCtorAuthorityDefaultScheme() {
-        WikiSite subject = new WikiSite("wikipedia.org");
+    @Test public void testSecureSchemeHttp() {
+        WikiSite subject = new WikiSite(false, "192.168.1.11:8080", "");
+        assertThat(subject.secureScheme(), is(false));
+    }
+
+    @Test public void testSecureSchemeHttps() {
+        WikiSite subject = new WikiSite(true, "192.168.1.11:8080", "");
         assertThat(subject.secureScheme(), is(true));
+    }
+
+    @Test public void testSchemeHttp() {
+        WikiSite subject = new WikiSite(false, "meta.wikimedia.org", "");
+        assertThat(subject.scheme(), is("http"));
+    }
+
+    @Test public void testSchemeHttps() {
+        WikiSite subject = new WikiSite(true, "meta.wikimedia.org", "");
+        assertThat(subject.scheme(), is("https"));
+    }
+
+    @Test public void testAuthority() {
+        WikiSite subject = new WikiSite(true, "test.wikipedia.org", "test");
+        assertThat(subject.authority(), is("test.wikipedia.org"));
+    }
+
+    @Test public void testMobileAuthorityLanguage() {
+        WikiSite subject = WikiSite.forLanguageCode("fiu-vro");
+        assertThat(subject.mobileAuthority(), is("fiu-vro.m.wikipedia.org"));
+    }
+
+    @Test public void testMobileAuthorityNoLanguage() {
+        WikiSite subject = new WikiSite("wikipedia.org");
+        assertThat(subject.mobileAuthority(), is("m.wikipedia.org"));
+    }
+
+    @Test public void testMobileAuthorityLanguageAuthority() {
+        WikiSite subject = new WikiSite("no.wikipedia.org", "nb");
+        assertThat(subject.mobileAuthority(), is("no.m.wikipedia.org"));
+    }
+
+    @Test public void testHost() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.host(), is("test.wikipedia.org"));
+    }
+
+    @Test public void testMobileHost() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.mobileHost(), is("test.m.wikipedia.org"));
+    }
+
+    @Test public void testMobileHostNoLanguage() {
+        WikiSite subject = WikiSite.forLanguageCode("");
+        assertThat(subject.mobileHost(), is("m.wikipedia.org"));
+    }
+
+    @Test public void testPort() {
+        final int port = 8080;
+        WikiSite subject = new WikiSite("192.168.1.11:" + port);
+        assertThat(subject.port(), is(port));
+    }
+
+    @Test public void testPortDefault() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.port(), is(-1));
+    }
+
+    @Test public void testPath() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.path("Segment"), is("/w/Segment"));
+    }
+
+    @Test public void testPathEmpty() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.path(""), is("/w/"));
+    }
+
+    @Test public void testUrl() {
+        WikiSite subject = new WikiSite(false, "test.192.168.1.11:8080", 
"test");
+        assertThat(subject.url(), is("http://test.192.168.1.11:8080";));
+    }
+
+    @Test public void testUrlPath() {
+        WikiSite subject = WikiSite.forLanguageCode("test");
+        assertThat(subject.url("Segment"), 
is("https://test.wikipedia.org/w/Segment";));
+    }
+
+    @Test public void testLanguageCode() {
+        WikiSite subject = WikiSite.forLanguageCode("lang");
+        assertThat(subject.languageCode(), is("lang"));
     }
 
     @Test public void testUnmarshal() {
@@ -33,6 +192,15 @@
         assertThat(GsonUnmarshaller.unmarshal(WikiSite.class, 
GsonMarshaller.marshal(wiki)), is(wiki));
     }
 
+    @Test public void testTitleForInternalLink() {
+        WikiSite wiki = WikiSite.forLanguageCode("en");
+        assertThat(new PageTitle("Main Page", wiki), 
is(wiki.titleForInternalLink("")));
+        assertThat(new PageTitle("Main Page", wiki), 
is(wiki.titleForInternalLink("/wiki/")));
+        assertThat(new PageTitle("wiki", wiki), 
is(wiki.titleForInternalLink("wiki")));
+        assertThat(new PageTitle("wiki", wiki), 
is(wiki.titleForInternalLink("/wiki/wiki")));
+        assertThat(new PageTitle("wiki/wiki", wiki), 
is(wiki.titleForInternalLink("/wiki/wiki/wiki")));
+    }
+
     @Test public void testEquals() {
         assertThat(WikiSite.forLanguageCode("en"), 
is(WikiSite.forLanguageCode("en")));
 
@@ -42,22 +210,5 @@
 
     @Test public void testNormalization() {
         assertThat("bm.wikipedia.org", 
is(WikiSite.forLanguageCode("bm").authority()));
-    }
-
-    @Test public void testIsSupportedSite() {
-        assertThat(WikiSite.supportedAuthority("fr.wikipedia.org"), is(true));
-        assertThat(WikiSite.supportedAuthority("fr.m.wikipedia.org"), 
is(true));
-        assertThat(WikiSite.supportedAuthority("roa-rup.wikipedia.org"), 
is(true));
-
-        assertThat(WikiSite.supportedAuthority("google.com"), is(false));
-    }
-
-    @Test public void testTitleForInternalLink() {
-        WikiSite wiki = WikiSite.forLanguageCode("en");
-        assertThat(new PageTitle("Main Page", wiki), 
is(wiki.titleForInternalLink("")));
-        assertThat(new PageTitle("Main Page", wiki), 
is(wiki.titleForInternalLink("/wiki/")));
-        assertThat(new PageTitle("wiki", wiki), 
is(wiki.titleForInternalLink("wiki")));
-        assertThat(new PageTitle("wiki", wiki), 
is(wiki.titleForInternalLink("/wiki/wiki")));
-        assertThat(new PageTitle("wiki/wiki", wiki), 
is(wiki.titleForInternalLink("/wiki/wiki/wiki")));
     }
 }
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/335182
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2a6075b746b36462086a8aa639d06d6bb6e1c275
Gerrit-PatchSet: 2
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org>
Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org>
Gerrit-Reviewer: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Sniedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to