|
2 | 2 | import org.junit.Assert;
|
3 | 3 | import org.junit.Test;
|
4 | 4 |
|
5 |
| -public class ContextUtilTests { |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +public final class ContextUtilTests { |
| 8 | + |
| 9 | + private static final class test{ |
| 10 | + |
| 11 | + private final String expected, context; |
| 12 | + private final boolean leadingSlash, trailingSlash; |
| 13 | + |
| 14 | + public test(final String expected, final String context, final boolean leadingSlash, final boolean trailingSlash){ |
| 15 | + this.expected = expected; |
| 16 | + this.context = context; |
| 17 | + this.leadingSlash = leadingSlash; |
| 18 | + this.trailingSlash = trailingSlash; |
| 19 | + } |
6 | 20 |
|
7 |
| - @Test |
8 |
| - public void testContexts(){ |
9 |
| - Assert.assertEquals("Failed test on blank (leading slash)" ,"/", ContextUtil.getContext("",true,false)); |
10 |
| - Assert.assertEquals("Failed test on blank (trailing slash)" ,"/", ContextUtil.getContext("",false,true)); |
11 |
| - Assert.assertEquals("Failed test on blank (both slashes)" ,"/", ContextUtil.getContext("",true,true)); |
12 |
| - |
13 |
| - Assert.assertEquals("Failed test on single (leading slash)" ,"/a" , ContextUtil.getContext("a",true,false)); |
14 |
| - Assert.assertEquals("Failed test on single (trailing slash)" ,"a/" , ContextUtil.getContext("a",false,true)); |
15 |
| - Assert.assertEquals("Failed test on single (both slashes)" ,"/a/", ContextUtil.getContext("a",true,true)); |
16 |
| - |
17 |
| - Assert.assertEquals("/testLeading" , ContextUtil.getContext("testLeading",true,false)); |
18 |
| - Assert.assertEquals("testTrailing/" , ContextUtil.getContext("testTrailing",false,true)); |
19 |
| - Assert.assertEquals("testNone" , ContextUtil.getContext("/testNone/",false,false)); |
20 |
| - Assert.assertEquals("/testBoth/" , ContextUtil.getContext("testBoth",true,true)); |
21 |
| - |
22 |
| - Assert.assertEquals("testBackSlash/" , ContextUtil.getContext("testBackSlash\\",false,true)); |
23 |
| - Assert.assertEquals("testConsBackSlash/", ContextUtil.getContext("testConsBackSlash\\\\",false,true)); |
24 |
| - Assert.assertEquals("testConsFwdSlash/" , ContextUtil.getContext("testConsFwdSlash//",false,true)); |
25 | 21 | }
|
26 | 22 |
|
27 | 23 | @Test
|
28 |
| - public void testJoin(){ |
29 |
| - Assert.assertEquals("Failed test on last blank join" ,"a" ,ContextUtil.joinContexts(false,false,"a","")); |
30 |
| - Assert.assertEquals("Failed test on last blank join+","/a/",ContextUtil.joinContexts(true,true,"a","")); |
| 24 | + public final void testContexts(){ |
| 25 | + final test[] tests = { |
| 26 | + new test("" , "/" , false , false), |
| 27 | + new test("/" , "" , true , false), |
| 28 | + new test("/" , "" , false , true ), |
| 29 | + new test("/" , "" , true , true ), |
| 30 | + new test("a" , "a" , false , false), |
| 31 | + new test("/a" , "a" , true , false), |
| 32 | + new test("a/" , "a" , false , true ), |
| 33 | + new test("/a/" , "a" , true , true ), |
| 34 | + new test("testNone" , "/testNone/" , false , false), |
| 35 | + new test("/testLeading" , "testLeading" , true , false), |
| 36 | + new test("testTrailing/" , "testTrailing" , false , true ), |
| 37 | + new test("/testBoth/" , "testBoth" , true , true ), |
| 38 | + new test("testNoneBackSlash" , "\\testNoneBackSlash\\" , false , false), |
| 39 | + new test("/testBackSlash/" , "\\testBackSlash\\" , true , true ), |
| 40 | + new test("/testConsecutiveBackSlash/" , "\\\\testConsecutiveBackSlash\\\\", true , true ), |
| 41 | + new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true ) |
| 42 | + }; |
31 | 43 |
|
32 |
| - Assert.assertEquals("Failed test on first blank join" ,"a" ,ContextUtil.joinContexts(false,false,"","a")); |
33 |
| - Assert.assertEquals("Failed test on first blank join+","/a/",ContextUtil.joinContexts(true,true,"","a")); |
| 44 | + for(final test test : tests) |
| 45 | + Assert.assertEquals(String.format("Incorrect context for #(\"%s\", %s, %s)", test.context,test.leadingSlash,test.trailingSlash),test.expected,ContextUtil.getContext(test.context,test.leadingSlash,test.trailingSlash)); |
| 46 | + } |
34 | 47 |
|
35 |
| - Assert.assertEquals("Failed test on both blank join" ,"" ,ContextUtil.joinContexts(false,false,"","")); |
36 |
| - Assert.assertEquals("Failed test on both blank join+","/",ContextUtil.joinContexts(true,true,"","")); |
| 48 | + // |
37 | 49 |
|
38 |
| - Assert.assertEquals("trailing/slash" ,ContextUtil.joinContexts(false,false,"trailing/","slash/")); |
39 |
| - Assert.assertEquals("/trailing/slash+/" ,ContextUtil.joinContexts(true,true,"trailing/","slash+/")); |
| 50 | + private static final class testJoin { |
40 | 51 |
|
41 |
| - Assert.assertEquals("leading/slash" ,ContextUtil.joinContexts(false,false,"/leading","/slash")); |
42 |
| - Assert.assertEquals("/leading/slash+/" ,ContextUtil.joinContexts(true,true,"/leading","/slash+")); |
| 52 | + private final String expected; |
| 53 | + private final String[] contexts; |
| 54 | + private final boolean leadingSlash, trailingSlash; |
43 | 55 |
|
44 |
| - Assert.assertEquals("double/slash" ,ContextUtil.joinContexts(false,false,"/double/","/slash/")); |
45 |
| - Assert.assertEquals("/double/slash+/" ,ContextUtil.joinContexts(true,true,"/double/","/slash+/")); |
| 56 | + public testJoin(final String expected, final boolean leadingSlash, final boolean trailingSlash, final String... contexts){ |
| 57 | + this.expected = expected; |
| 58 | + this.leadingSlash = leadingSlash; |
| 59 | + this.trailingSlash = trailingSlash; |
| 60 | + this.contexts = contexts; |
| 61 | + } |
46 | 62 |
|
47 |
| - Assert.assertEquals("no/slash" ,ContextUtil.joinContexts(false,false,"no","slash")); |
48 |
| - Assert.assertEquals("/no/slash+/" ,ContextUtil.joinContexts(true,true,"no","slash+")); |
| 63 | + } |
49 | 64 |
|
50 |
| - Assert.assertEquals("cons/slash" ,ContextUtil.joinContexts(false,false,"/cons/","/slash/")); |
51 |
| - Assert.assertEquals("/cons/slash+/" ,ContextUtil.joinContexts(true,true,"/cons/","/slash+/")); |
| 65 | + @Test |
| 66 | + public final void testJoin(){ |
| 67 | + final testJoin[] tests = { |
| 68 | + new testJoin("testBlank" ,false ,false ,"testBlank",""), |
| 69 | + new testJoin("/testBlank/" ,true ,true ,"testBlank",""), |
| 70 | + new testJoin("testBlank" ,false ,false ,"","testBlank"), |
| 71 | + new testJoin("/testBlank/" ,true ,true ,"","testBlank"), |
| 72 | + new testJoin("" ,false ,false ,"",""), |
| 73 | + new testJoin("/" ,true ,true ,"",""), |
| 74 | + new testJoin("trailing/slash" , false , false ,"trailing/","slash/"), |
| 75 | + new testJoin("/trailing/slash/" , true , true ,"trailing/","slash/"), |
| 76 | + new testJoin("leading/slash" , false , false ,"leading/","slash/"), |
| 77 | + new testJoin("/leading/slash/" , true , true ,"leading/","slash/"), |
| 78 | + new testJoin("double/slash" , false , false ,"/double/","/slash/"), |
| 79 | + new testJoin("/double/slash/" , true , true ,"/double/","/slash/"), |
| 80 | + new testJoin("no/slash" , false , false ,"no","slash"), |
| 81 | + new testJoin("/no/slash/" , true , true ,"no","slash"), |
| 82 | + new testJoin("consecutive/slash" , false , false ,"//consecutive//","//slash//"), |
| 83 | + new testJoin("/consecutive/slash/" , true , true ,"//consecutive//","//slash//"), |
| 84 | + new testJoin("mixed/slash" , false , false ,"\\mixed\\","//slash//"), |
| 85 | + new testJoin("/mixed/slash/" , true , true ,"\\mixed\\","//slash//"), |
| 86 | + }; |
52 | 87 |
|
53 |
| - Assert.assertEquals("mix/slash" ,ContextUtil.joinContexts(false,false,"\\mix\\","/slash/")); |
54 |
| - Assert.assertEquals("/mix/slash+/" ,ContextUtil.joinContexts(true,true,"\\mix\\","/slash+/")); |
| 88 | + for(final testJoin test : tests) |
| 89 | + Assert.assertEquals(String.format("Incorrect context for #(%s, %s, %s)", test.leadingSlash, test.trailingSlash, Arrays.toString(test.contexts)), test.expected, ContextUtil.joinContexts(test.leadingSlash, test.trailingSlash, test.contexts)); |
55 | 90 | }
|
56 | 91 |
|
57 | 92 | }
|
0 commit comments