1 package com.atlassian.sal.core.pluginsettings;
2
3 import java.util.*;
4
5 import org.apache.commons.lang.StringEscapeUtils;
6 import org.junit.Before;
7 import org.junit.Test;
8 import static org.junit.Assert.*;
9
10 public class TestAbstractStringPluginSettings
11 {
12 private PluginSettingsAcceptor acceptor;
13 private static final String KEY = "some key";
14
15 @Before
16 public void setUp()
17 {
18 acceptor = new PluginSettingsAcceptor();
19 }
20
21 @Test
22 public void testString()
23 {
24 String value = "this is the value";
25
26 acceptor.put(KEY, value);
27 assertEquals("Values should be equal.", value, acceptor.get(KEY));
28 }
29
30 @Test
31 public void testSpecialCharsString()
32 {
33 String value = "this\tis\bthe\nvalue\rand\ffun";
34
35 acceptor.put(KEY, value);
36 assertEquals("Values should be equal.", value, acceptor.get(KEY));
37 }
38
39 @Test
40 public void testList()
41 {
42 final List<String> value = new ArrayList<String>();
43 final String first = "Beanz Meanz Heinz";
44 final String second = "Sic 'em Rex!";
45
46 value.add(first);
47 value.add(second);
48
49 acceptor.put(KEY, value);
50
51 final Object actual = acceptor.get(KEY);
52
53 assertTrue(actual instanceof List);
54
55 final List<String> real = (List) actual;
56
57 assertEquals("List size should be the same", 2, real.size());
58 assertEquals("List should still be in order", first, real.get(0));
59 assertEquals("List should still be in order", second, real.get(1));
60 }
61
62 @Test
63 public void testSpecialCharsInList()
64 {
65 final List<String> value = new ArrayList<String>();
66
67 final String first = "At\tfirst\bwhen\nI\rsee\fyou cry";
68 final String second = "\tit\bmakes\nme\rsmile\f";
69
70 value.add(first);
71 value.add(second);
72 acceptor.put(KEY, value);
73
74 final Object actual = acceptor.get(KEY);
75
76 assertTrue(actual instanceof List);
77
78 final List<String> real = (List) actual;
79
80 assertEquals("List size should be the same", 2, real.size());
81 assertEquals("The content should match the original data", first, real.get(0));
82 assertEquals("The content should match the original data", second, real.get(1));
83 }
84
85 @Test
86 public void testMap()
87 {
88 final Map<String, String> value = new HashMap<String, String>();
89 final String[] first = {"antzpantz", "Sic 'em Rex!"};
90 final String[] second = {"homestar", "Lookin' at a thing in a bag"};
91 final String[] third = {"he-man", "By the power of Greyskull!!!"};
92
93 mapPut(value, first);
94 mapPut(value, second);
95 mapPut(value, third);
96
97 acceptor.put(KEY, value);
98
99 final Object actual = acceptor.get(KEY);
100
101 assertTrue(actual instanceof Map);
102
103 final Map real = (Map) actual;
104
105 assertEquals("The size should be the same as what was entered.", 3, real.entrySet().size());
106 assertMapEntryEquals("Map should retrieve the correct value for each key", real, first);
107 assertMapEntryEquals("Map should retrieve the correct value for each key", real, second);
108 assertMapEntryEquals("Map should retrieve the correct value for each key", real, third);
109 }
110
111 @Test
112 public void testSpecialCharsInMap()
113 {
114 final Map<String, String> value = new HashMap<String, String>();
115 final String[] first = {"one", "one\tthing\bI\ndon't\rknow\fwhy"};
116 final String[] second = {"two", "I\tdoesn't\beven\nmatter\rhow\fhard you try"};
117
118 mapPut(value, first);
119 mapPut(value, second);
120
121 acceptor.put(KEY, value);
122
123 final Object actual = acceptor.get(KEY);
124
125 assertTrue(actual instanceof Map);
126
127 final Map real = (Map) actual;
128
129 assertEquals("The size should be the same as what was entered.", 2, real.entrySet().size());
130 assertMapEntryEquals("The content should match the original data", real, first);
131 assertMapEntryEquals("The content should match the original data", real, second);
132 }
133
134 @Test
135 public void testEmptyMap()
136 {
137 final Map<String, String> value = new HashMap<String, String>();
138 acceptor.put(KEY, value);
139 final Object actual = acceptor.get(KEY);
140 assertTrue(actual instanceof Map);
141 assertTrue(((Map) actual).isEmpty());
142 }
143
144 @Test
145 public void testProperties()
146 {
147 final Properties value = new Properties();
148 final String[] first = {"antzpantz", "Sic 'em Rex!"};
149 final String[] second = {"homestar", "Lookin' at a thing in a bag"};
150 final String[] third = {"he-man", "By the power of Greyskull!!!"};
151
152 propertiesPut(value, first);
153 propertiesPut(value, second);
154 propertiesPut(value, third);
155
156 acceptor.put(KEY, value);
157
158 final Object actual = acceptor.get(KEY);
159
160 assertTrue(actual instanceof Properties);
161
162 final Properties real = (Properties) actual;
163
164 assertPropertiesEntryEquals("Propertis should contain the same value for each key", real, first);
165 assertPropertiesEntryEquals("Propertis should contain the same value for each key", real, second);
166 assertPropertiesEntryEquals("Propertis should contain the same value for each key", real, third);
167 }
168
169 @Test
170 public void testSpecialCharsInKeys()
171 {
172 String key1 = "this\tis\bthe\nkey\rsample\fhohoho1";
173 String value1 = "value1";
174
175 String key2 = "this\tis\bthe\nkey\rsample\fhohoho2";
176 List<String> value2 = Arrays.asList("value2");
177
178 String key3 = "this\tis\bthe\nkey\rsample\fhohoho3";
179 Map<String, String> value3 = new HashMap<String, String>();
180 value3.put(key3 + "inner1", "value3inner1");
181 value3.put(key3 + "inner2", "value3inner2");
182
183 String key4 = "this\tis\bthe\nkey\rsample\fhohoho4";
184 Properties value4 = new Properties();
185 value4.setProperty(key4 + "inner1", "value4inner1");
186 value4.setProperty(key4 + "inner2", "value4inner2");
187
188 acceptor.put(key1, value1);
189 acceptor.put(key2, value2);
190 acceptor.put(key3, value3);
191 acceptor.put(key4, value4);
192
193 assertEquals("Values should be equal.", value1, acceptor.get(key1));
194 assertEquals("Values should be equal.", value2, acceptor.get(key2));
195 assertEquals("Values should be equal.", value3, acceptor.get(key3));
196 assertEquals("Values should be equal.", value4, acceptor.get(key4));
197 }
198
199 @Test
200 public void testPutReturnValueNull()
201 {
202 List<String> value = Arrays.asList("one", "two", "three");
203 assertNull(acceptor.put(KEY, value));
204 }
205
206 @Test
207 public void testPutReturnValueOldValue()
208 {
209 List<String> oldValue = Arrays.asList("two", "three", "four");
210 acceptor.put(KEY, oldValue);
211 List<String> value = Arrays.asList("one", "two", "three");
212 assertEquals(oldValue, acceptor.put(KEY, value));
213 }
214
215 @Test
216 public void testRemoveReturnValueNull()
217 {
218 assertNull(acceptor.remove(KEY));
219 }
220
221 @Test
222 public void testRemoveReturnValueOldValue()
223 {
224 List<String> oldValue = Arrays.asList("two", "three", "four");
225 acceptor.put(KEY, oldValue);
226 assertEquals(oldValue, acceptor.remove(KEY));
227 }
228
229 private void assertPropertiesEntryEquals(String errMsg, Properties real, String[] kvPair)
230 {
231 assertEquals(errMsg, kvPair[1], real.getProperty(kvPair[0]));
232 }
233
234 private static void propertiesPut(Properties properties, String[] values)
235 {
236 properties.setProperty(values[0], values[1]);
237 }
238
239 private static void assertMapEntryEquals(String errMsg, Map real, String[] kvPair)
240 {
241 assertEquals(errMsg, kvPair[1], real.get(kvPair[0]));
242 }
243
244 private static void mapPut(Map<String, String> map, String[] values)
245 {
246 map.put(values[0], values[1]);
247 }
248
249
250 private static final class PluginSettingsAcceptor extends AbstractStringPluginSettings
251 {
252 private final Map<String,String> backingStore = new HashMap();
253
254 protected void putActual(String key, String val)
255 {
256 backingStore.put(key, val);
257 }
258
259 protected String getActual(String key)
260 {
261 return backingStore.get(key);
262 }
263
264 protected void removeActual(String key)
265 {
266 backingStore.remove(key);
267 }
268 }
269 }