]> gitweb.fperrin.net Git - GpsPrune.git/blob - test/tim/prune/gui/map/SiteNameUtilsTest.java
Version 20.4, May 2021
[GpsPrune.git] / test / tim / prune / gui / map / SiteNameUtilsTest.java
1 package tim.prune.gui.map;
2
3 import static org.junit.jupiter.api.Assertions.*;
4
5 import java.util.HashSet;
6
7 import org.junit.jupiter.api.Test;
8
9 /**
10  * JUnit tests for site name utils
11  */
12 class SiteNameUtilsTest
13 {
14
15         @Test
16         void testPickServerNameWithoutWildcards()
17         {
18                 testPickSingleUrl("abc", "abc");
19                 testPickSingleUrl("ab[]c", "abc");
20                 testPickSingleUrl("[]abc", "abc");
21                 testPickSingleUrl("abc[]", "abc");
22         }
23
24         /**
25          * Test a pattern without wildcards which should always produce the expected result
26          * @param inPattern pattern for site name
27          * @param inExpected expected resolved name
28          */
29         private void testPickSingleUrl(String inPattern, String inExpected)
30         {
31                 for (int i=0; i<20; i++)
32                 {
33                         String resolved = SiteNameUtils.pickServerUrl(inPattern);
34                         assertEquals(inExpected, resolved, "Failed: " + inPattern);
35                 }
36         }
37
38         @Test
39         void testPickUsingWildcards()
40         {
41                 testRandomPick("ab[123]c", new String[]{"ab1c", "ab2c", "ab3c"});
42                 testRandomPick("1234.[abcd]", new String[]{"1234.a", "1234.b", "1234.c", "1234.d"});
43         }
44
45         /**
46          * Test a pattern with wildcards which should produce several different results randomly
47          * @param inPattern pattern for site name
48          * @param inExpected array of expected resolved names
49          */
50         private void testRandomPick(String inPattern, String[] inExpected)
51         {
52                 HashSet<String> results = new HashSet<String>();
53                 for (int i=0; i<30; i++)
54                 {
55                         results.add(SiteNameUtils.pickServerUrl(inPattern));
56                 }
57                 // Check that all expected results were returned
58                 assertEquals(inExpected.length, results.size());
59                 for (String expec : inExpected) {
60                         assertTrue(results.contains(expec));
61                 }
62         }
63 }