diff --git a/mobile/android/base/java/org/mozilla/gecko/util/UUIDUtil.java b/mobile/android/base/java/org/mozilla/gecko/util/UUIDUtil.java new file mode 100644 index 000000000000..cef303a870a9 --- /dev/null +++ b/mobile/android/base/java/org/mozilla/gecko/util/UUIDUtil.java @@ -0,0 +1,19 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.mozilla.gecko.util; + +import java.util.regex.Pattern; + +/** + * Utilities for UUIDs. + */ +public class UUIDUtil { + private UUIDUtil() {} + + public static final String UUID_REGEX = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"; + public static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX); +} diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index 26a4cf9b2bb8..7bba14103cef 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -126,6 +126,7 @@ gujar.sources += ['java/org/mozilla/gecko/' + x for x in [ 'util/StringUtils.java', 'util/ThreadUtils.java', 'util/UIAsyncTask.java', + 'util/UUIDUtil.java', 'util/WeakReferenceHandler.java', 'util/WebActivityMapper.java', 'util/WindowUtils.java', diff --git a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestUUIDUtil.java b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestUUIDUtil.java new file mode 100644 index 000000000000..732dd21b9969 --- /dev/null +++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestUUIDUtil.java @@ -0,0 +1,51 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.mozilla.gecko.util; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mozilla.gecko.background.testhelpers.TestRunner; + +import static org.junit.Assert.*; + +/** + * Tests for uuid utils. + */ +@RunWith(TestRunner.class) +public class TestUUIDUtil { + private static final String[] validUUIDs = { + "904cd9f8-af63-4525-8ce0-b9127e5364fa", + "8d584bd2-00ea-4043-a617-ed4ce7018ed0", + "3abad327-2669-4f68-b9ef-7ace8c5314d6", + }; + + private static final String[] invalidUUIDs = { + "its-not-a-uuid-mate", + "904cd9f8-af63-4525-8ce0-b9127e5364falol", + "904cd9f8-af63-4525-8ce0-b9127e5364f", + }; + + @Test + public void testUUIDRegex() { + for (final String uuid : validUUIDs) { + assertTrue("Valid UUID matches UUID-regex", uuid.matches(UUIDUtil.UUID_REGEX)); + } + for (final String uuid : invalidUUIDs) { + assertFalse("Invalid UUID does not match UUID-regex", uuid.matches(UUIDUtil.UUID_REGEX)); + } + } + + @Test + public void testUUIDPattern() { + for (final String uuid : validUUIDs) { + assertTrue("Valid UUID matches UUID-regex", UUIDUtil.UUID_PATTERN.matcher(uuid).matches()); + } + for (final String uuid : invalidUUIDs) { + assertFalse("Invalid UUID does not match UUID-regex", UUIDUtil.UUID_PATTERN.matcher(uuid).matches()); + } + } +}