1 package com.atlassian.config;
2
3 import com.atlassian.config.xml.AbstractDom4jXmlConfigurationPersister;
4 import junit.framework.TestCase;
5
6 import java.io.File;
7 import java.util.*;
8
9
10
11
12
13
14
15
16 public class Dom4jConfigurationTestCase extends TestCase
17 {
18 public static final String TEST_RESOURCES_PATH = "src/test/resources";
19
20 public void testSimpleConfig() throws Exception
21 {
22 ConfigurationPersister config = new AbstractDom4jXmlConfigurationPersister()
23 {
24 public String getRootName()
25 {
26 return "test-configuration";
27 }
28
29 public void save(String path, String fileName) throws ConfigurationException
30 {
31 addConfigElement("Hello Updated", "property1");
32 addConfigElement("GoodBye Updated", "property2");
33 addConfigElement(null, "property3");
34 saveDocument(path, fileName);
35 }
36
37 public Object load(String configPath, String configFile) throws ConfigurationException
38 {
39 File file = new File(configPath + "/" + configFile);
40 try
41 {
42 this.loadDocument(file);
43 }
44 catch (Exception e)
45 {
46 throw new ConfigurationException("Failed to load config from file: " + configPath + "/" + configFile, e);
47 }
48 Map props = new HashMap();
49 props.put("property1", getConfigElement(String.class, "property1"));
50 props.put("property2", getConfigElement(String.class, "property2"));
51
52 return props;
53 }
54 };
55
56 Map props = (Map) config.load(TEST_RESOURCES_PATH, "simple-config.xml");
57 assertEquals(2, props.size());
58 Map.Entry entry;
59 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
60 {
61 entry = (Map.Entry) iterator.next();
62 if (entry.getKey().equals("property1"))
63 {
64 assertEquals("Hello", entry.getValue());
65 }
66 else if (entry.getKey().equals("property2"))
67 {
68 assertEquals("Goodbye", entry.getValue());
69 }
70 else
71 {
72 fail("What the hell is " + entry.getKey() + "!");
73 }
74 }
75 config.save(TEST_RESOURCES_PATH, "simple-config2.xml");
76 }
77
78 public void testNestedConfig() throws Exception
79 {
80 ConfigurationPersister config = new AbstractDom4jXmlConfigurationPersister()
81 {
82 public String getRootName()
83 {
84 return "test-configuration";
85 }
86
87 public void save(String path, String fileName) throws ConfigurationException
88 {
89 addConfigElement("Hello Updated", "nest1/property1");
90
91
92
93 addConfigElement("GoodBye Updated", "nest1/nest2/property2");
94 saveDocument(path, fileName);
95 }
96
97 public Object load(String configPath, String configFile) throws ConfigurationException
98 {
99 File file = new File(configPath + "/" + configFile);
100 try
101 {
102 this.loadDocument(file);
103 }
104 catch (Exception e)
105 {
106 throw new ConfigurationException("Failed to load config from file: " + configPath + "/" + configFile, e);
107 }
108 Map props = new HashMap();
109 props.put("property1", getConfigElement(String.class, "nest1/property1"));
110 props.put("property2", getConfigElement(String.class, "nest1/nest2/property2"));
111
112 return props;
113 }
114 };
115
116 Map props = (Map) config.load(TEST_RESOURCES_PATH, "nested-config.xml");
117 assertEquals(2, props.size());
118 Map.Entry entry;
119 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
120 {
121 entry = (Map.Entry) iterator.next();
122 if (entry.getKey().equals("property1"))
123 {
124 assertEquals("Hello", entry.getValue());
125 }
126 else if (entry.getKey().equals("property2"))
127 {
128 assertEquals("Goodbye", entry.getValue());
129 }
130 else
131 {
132 fail("What the hell is " + entry.getKey() + "!");
133 }
134 }
135 config.save(TEST_RESOURCES_PATH, "nested-config2.xml");
136 }
137
138
139 public void testMapConfig() throws Exception
140 {
141 ConfigurationPersister config = new AbstractDom4jXmlConfigurationPersister()
142 {
143 public String getRootName()
144 {
145 return "test-configuration";
146 }
147
148 public void save(String path, String fileName) throws ConfigurationException
149 {
150 Map map = new HashMap();
151 map.put("property1", "Hello Updated");
152 map.put("property2", "Goodbye Updated");
153 addConfigElement(map, "properties");
154 saveDocument(path, fileName);
155 }
156
157 public Object load(String configPath, String configFile) throws ConfigurationException
158 {
159 File file = new File(configPath + "/" + configFile);
160 try
161 {
162 this.loadDocument(file);
163 }
164 catch (Exception e)
165 {
166 throw new ConfigurationException("Failed to load config from file: " + configPath + "/" + configFile, e);
167 }
168 Map props = (Map) getConfigElement(Map.class, "properties");
169
170 return props;
171 }
172 };
173
174 Map props = (Map) config.load(TEST_RESOURCES_PATH, "map-config.xml");
175 assertEquals(2, props.size());
176 Map.Entry entry;
177 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
178 {
179 entry = (Map.Entry) iterator.next();
180 if (entry.getKey().equals("property1"))
181 {
182 assertEquals("Hello", entry.getValue());
183 }
184 else if (entry.getKey().equals("property2"))
185 {
186 assertEquals("Goodbye", entry.getValue());
187 }
188 else
189 {
190 fail("What the hell is " + entry.getKey() + "!");
191 }
192 }
193 config.save(TEST_RESOURCES_PATH, "map-config2.xml");
194 }
195
196 public void testListConfig() throws Exception
197 {
198 ConfigurationPersister config = new AbstractDom4jXmlConfigurationPersister()
199 {
200 public String getRootName()
201 {
202 return "test-configuration";
203 }
204
205 public void save(String path, String fileName) throws ConfigurationException
206 {
207 List list = new ArrayList();
208 list.add("Hello Updated");
209 list.add("Goodbye Updated");
210 addConfigElement(list, "alist");
211 saveDocument(path, fileName);
212 }
213
214 public Object load(String configPath, String configFile) throws ConfigurationException
215 {
216 File file = new File(configPath + "/" + configFile);
217 try
218 {
219 this.loadDocument(file);
220 }
221 catch (Exception e)
222 {
223 throw new ConfigurationException("Failed to load config from file: " + configPath + "/" + configFile, e);
224 }
225 List list = (List) getConfigElement(List.class, "alist");
226
227 return list;
228 }
229 };
230
231 List list = (List) config.load(TEST_RESOURCES_PATH, "list-config.xml");
232 assertEquals(2, list.size());
233 assertEquals("Hello", list.get(0).toString());
234 assertEquals("Goodbye", list.get(1).toString());
235
236 config.save(TEST_RESOURCES_PATH, "list-config2.xml");
237 }
238
239 protected void tearDown() throws Exception
240 {
241
242
243
244
245
246 }
247 }