X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=test%2Ftim%2Fprune%2Fgui%2Fmap%2FSiteNameUtilsTest.java;fp=test%2Ftim%2Fprune%2Fgui%2Fmap%2FSiteNameUtilsTest.java;h=e01ed5240ee32739daa142390e299b7dda76c5ef;hp=0000000000000000000000000000000000000000;hb=cd5dd0c207b676067e85e0885b90f05445b7e229;hpb=1db53356139320890a8d10e982865a1899e11b81 diff --git a/test/tim/prune/gui/map/SiteNameUtilsTest.java b/test/tim/prune/gui/map/SiteNameUtilsTest.java new file mode 100644 index 0000000..e01ed52 --- /dev/null +++ b/test/tim/prune/gui/map/SiteNameUtilsTest.java @@ -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 results = new HashSet(); + 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)); + } + } +}