]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - test/tim/prune/gui/map/SiteNameUtilsTest.java
Version 20.4, May 2021
[GpsPrune.git] / test / tim / prune / gui / map / SiteNameUtilsTest.java
diff --git a/test/tim/prune/gui/map/SiteNameUtilsTest.java b/test/tim/prune/gui/map/SiteNameUtilsTest.java
new file mode 100644 (file)
index 0000000..e01ed52
--- /dev/null
@@ -0,0 +1,63 @@
+package tim.prune.gui.map;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.HashSet;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * JUnit tests for site name utils
+ */
+class SiteNameUtilsTest
+{
+
+       @Test
+       void testPickServerNameWithoutWildcards()
+       {
+               testPickSingleUrl("abc", "abc");
+               testPickSingleUrl("ab[]c", "abc");
+               testPickSingleUrl("[]abc", "abc");
+               testPickSingleUrl("abc[]", "abc");
+       }
+
+       /**
+        * Test a pattern without wildcards which should always produce the expected result
+        * @param inPattern pattern for site name
+        * @param inExpected expected resolved name
+        */
+       private void testPickSingleUrl(String inPattern, String inExpected)
+       {
+               for (int i=0; i<20; i++)
+               {
+                       String resolved = SiteNameUtils.pickServerUrl(inPattern);
+                       assertEquals(inExpected, resolved, "Failed: " + inPattern);
+               }
+       }
+
+       @Test
+       void testPickUsingWildcards()
+       {
+               testRandomPick("ab[123]c", new String[]{"ab1c", "ab2c", "ab3c"});
+               testRandomPick("1234.[abcd]", new String[]{"1234.a", "1234.b", "1234.c", "1234.d"});
+       }
+
+       /**
+        * Test a pattern with wildcards which should produce several different results randomly
+        * @param inPattern pattern for site name
+        * @param inExpected array of expected resolved names
+        */
+       private void testRandomPick(String inPattern, String[] inExpected)
+       {
+               HashSet<String> results = new HashSet<String>();
+               for (int i=0; i<30; i++)
+               {
+                       results.add(SiteNameUtils.pickServerUrl(inPattern));
+               }
+               // Check that all expected results were returned
+               assertEquals(inExpected.length, results.size());
+               for (String expec : inExpected) {
+                       assertTrue(results.contains(expec));
+               }
+       }
+}