Bug 1265708 - Pre: Implement StringUtils.stripRef r=sebastian

MozReview-Commit-ID: AO25iiUMXLE

--HG--
extra : rebase_source : dcf2be733663ad242c28e1fab9a5cb814887bbd4
This commit is contained in:
Andrzej Hunt 2016-06-03 14:04:39 -07:00
parent 342b90bc16
commit a4a2b068c3
3 changed files with 34 additions and 0 deletions

View File

@ -62,6 +62,26 @@ public class StringUtils {
return wasSearchQuery;
}
/**
* Strip the ref from a URL, if present
*
* @return The base URL, without the ref. The original String is returned if it has no ref,
* of if the input is malformed.
*/
public static String stripRef(final String inputURL) {
if (inputURL == null) {
return null;
}
final int refIndex = inputURL.indexOf('#');
if (refIndex >= 0) {
return inputURL.substring(0, refIndex);
}
return inputURL;
}
public static class UrlFlags {
public static final int NONE = 0;
public static final int STRIP_HTTPS = 1;

View File

@ -131,6 +131,7 @@ gujar.sources += ['java/org/mozilla/gecko/' + x for x in [
'util/StringUtils.java',
'util/ThreadUtils.java',
'util/UIAsyncTask.java',
'util/URLUtils.java',
'util/UUIDUtil.java',
'util/WeakReferenceHandler.java',
'util/WebActivityMapper.java',

View File

@ -11,6 +11,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mozilla.gecko.background.testhelpers.TestRunner;
import static org.junit.Assert.assertEquals;
@RunWith(TestRunner.class)
public class TestStringUtils {
@Test
@ -40,4 +42,15 @@ public class TestStringUtils {
Assert.assertFalse(StringUtils.isHttpOrHttps("google.com"));
Assert.assertFalse(StringUtils.isHttpOrHttps("git@github.com:mozilla/gecko-dev.git"));
}
@Test
public void testStripRef() {
assertEquals(StringUtils.stripRef(null), null);
assertEquals(StringUtils.stripRef(""), "");
assertEquals(StringUtils.stripRef("??AAABBBCCC"), "??AAABBBCCC");
assertEquals(StringUtils.stripRef("https://mozilla.org"), "https://mozilla.org");
assertEquals(StringUtils.stripRef("https://mozilla.org#BBBB"), "https://mozilla.org");
assertEquals(StringUtils.stripRef("https://mozilla.org/#BBBB"), "https://mozilla.org/");
}
}