1 package com.atlassian.security.auth.trustedapps;
2
3 import java.util.Arrays;
4
5 import junit.framework.TestCase;
6
7 public class TestStringUtil extends TestCase
8 {
9 public void testSplitSimple() throws Exception
10 {
11 String[] result = StringUtil.split("[\"one\",\"four\", \"another\", \"six\"]");
12 assertNotNull(result);
13 assertEquals(4, result.length);
14 assertEquals("one", result[0]);
15 assertEquals("four", result[1]);
16 assertEquals("another", result[2]);
17 assertEquals("six", result[3]);
18 }
19
20 public void testSplitSimpleNoTrailingCloseBrace() throws Exception
21 {
22 String[] result = StringUtil.split("[\"one\",\"four\", \"another\", \"six\"");
23 assertNotNull(result);
24 assertEquals(4, result.length);
25 assertEquals("one", result[0]);
26 assertEquals("four", result[1]);
27 assertEquals("another", result[2]);
28 assertEquals("six", result[3]);
29 }
30
31 public void testSplitWithCommaValues() throws Exception
32 {
33 String[] result = StringUtil.split("[\"one\",\"four\", \"ano,ther\", \"s,',',ix\"]");
34 assertNotNull(result);
35 assertEquals(4, result.length);
36 assertEquals("one", result[0]);
37 assertEquals("four", result[1]);
38 assertEquals("ano,ther", result[2]);
39 assertEquals("s,',',ix", result[3]);
40 }
41
42 public void testSplitWithNulls() throws Exception
43 {
44 String[] result = StringUtil.split("[\"one\",,, \"six\"]");
45 assertNotNull(result);
46 assertEquals(4, result.length);
47 assertEquals("one", result[0]);
48 assertNull(result[1]);
49 assertNull(result[2]);
50 assertEquals("six", result[3]);
51 }
52
53 public void testSplitWithTrailingComma() throws Exception
54 {
55 String[] result = StringUtil.split("[\"wun\",]");
56 assertNotNull(result);
57 assertEquals(1, result.length);
58 assertEquals("wun", result[0]);
59 }
60
61 public void testSplitWithTrailingCommaNoTrailingBrace() throws Exception
62 {
63 String[] result = StringUtil.split("[\"wun\",");
64 assertNotNull(result);
65 assertEquals(1, result.length);
66 assertEquals("wun", result[0]);
67 }
68
69 public void testSplitEmpty() throws Exception
70 {
71 String[] result = StringUtil.split("[]");
72 assertNotNull(result);
73 assertEquals(0, result.length);
74 }
75
76 public void testSplitPartialEmpty() throws Exception
77 {
78 String[] result = StringUtil.split("[");
79 assertNotNull(result);
80 assertEquals(0, result.length);
81 }
82
83 public void testSplitThrowsNoLeadingBrace() throws Exception
84 {
85 try
86 {
87 StringUtil.split("one");
88 fail("IllegalStateException expected");
89 }
90 catch (IllegalStateException expected)
91 {
92 }
93 }
94
95 public void testArrayToString() throws Exception
96 {
97 String encoded = StringUtil.toString(new String[] {"one", "two", "three"});
98 assertEquals("[\"one\",\"two\",\"three\"]", encoded);
99 }
100
101 public void testArrayToStringEncodesQuotes() throws Exception
102 {
103 String encoded = StringUtil.toString(new String[] {"one", "tw\"o", "three"});
104 assertEquals("[\"one\",\"tw\\\"o\",\"three\"]", encoded);
105 }
106
107 public void testArrayToStringEncodesSemiTab() throws Exception
108 {
109 String encoded = StringUtil.toString(new String[] {"one", "tw;\to", "three"});
110 assertEquals("[\"one\",\"tw;\\to\",\"three\"]", encoded);
111 }
112
113 public void testRoundTripWithQuotes() throws Exception
114 {
115 final String[] input = new String[] {"one", "tw\"o", "three"};
116 final String encoded = StringUtil.toString(input);
117 assertEquals("[\"one\",\"tw\\\"o\",\"three\"]", encoded);
118 final String[] output = StringUtil.split(encoded);
119 assertTrue(Arrays.equals(input, output));
120 }
121
122 public void testRoundTripWithTabs() throws Exception
123 {
124 final String[] input = new String[] {"one", "tw;\to", "three"};
125 final String encoded = StringUtil.toString(input);
126 assertEquals("[\"one\",\"tw;\\to\",\"three\"]", encoded);
127 final String[] output = StringUtil.split(encoded);
128 assertTrue(Arrays.equals(input, output));
129 }
130 }