|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| package org.jconfig; |
|
7 |
| |
|
8 |
| import junit.framework.TestCase; |
|
9 |
| |
|
10 |
| import org.jconfig.event.PropertyChangedEvent; |
|
11 |
| import org.jconfig.event.PropertyListener; |
|
12 |
| import org.jconfig.parser.DefaultConfigParser; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| public class ConfigurationTest extends TestCase implements PropertyListener { |
|
20 |
| |
|
21 |
| private Configuration cfg; |
|
22 |
27
| public ConfigurationTest(String name) {
|
|
23 |
27
| super(name);
|
|
24 |
| } |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
0
| public static void main(String[] args) {
|
|
31 |
0
| junit.textui.TestRunner.run(ConfigurationTest.class);
|
|
32 |
| } |
|
33 |
| |
|
34 |
27
| protected void setUp() {
|
|
35 |
27
| System.setProperty("jconfig.parser", DefaultConfigParser.class.getName());
|
|
36 |
27
| cfg = new DefaultConfiguration("ConfigurationTest");
|
|
37 |
27
| assertNotNull(cfg);
|
|
38 |
| } |
|
39 |
| |
|
40 |
27
| protected void tearDown() {
|
|
41 |
| } |
|
42 |
| |
|
43 |
1
| public void testGetAllCategoryNames() {
|
|
44 |
1
| String[] names = cfg.getCategoryNames();
|
|
45 |
1
| assertEquals(1,names.length);
|
|
46 |
1
| assertEquals("general",names[0]);
|
|
47 |
| } |
|
48 |
| |
|
49 |
1
| public void testSetCategory2() {
|
|
50 |
1
| cfg.setCategory("test");
|
|
51 |
1
| String[] names = cfg.getCategoryNames();
|
|
52 |
1
| assertEquals(2,names.length);
|
|
53 |
| } |
|
54 |
| |
|
55 |
1
| public void testAddMainCategory() {
|
|
56 |
1
| cfg.setCategory("test",true);
|
|
57 |
1
| String[] names = cfg.getCategoryNames();
|
|
58 |
1
| assertEquals(2,names.length);
|
|
59 |
1
| String main = cfg.getMainCategoryName();
|
|
60 |
1
| assertEquals("test",main);
|
|
61 |
| } |
|
62 |
| |
|
63 |
1
| public void testRemoveCategory() {
|
|
64 |
1
| cfg.setCategory("test");
|
|
65 |
1
| String[] names = cfg.getCategoryNames();
|
|
66 |
1
| assertEquals(2,names.length);
|
|
67 |
1
| cfg.removeCategory("test");
|
|
68 |
1
| names = cfg.getCategoryNames();
|
|
69 |
1
| assertEquals(1,names.length);
|
|
70 |
| } |
|
71 |
| |
|
72 |
1
| public void testGetNumberOfCategories() {
|
|
73 |
1
| cfg.setCategory("test");
|
|
74 |
1
| int count = cfg.getNumberOfCategories();
|
|
75 |
1
| assertEquals(2,count);
|
|
76 |
| } |
|
77 |
| |
|
78 |
1
| public void testSetAndGetExistingPropertyForMainCategory() {
|
|
79 |
1
| cfg.setProperty("testName","testValue");
|
|
80 |
1
| String prop = cfg.getProperty("testName");
|
|
81 |
1
| assertEquals("testValue",prop);
|
|
82 |
| } |
|
83 |
| |
|
84 |
1
| public void testGetNonExistingPropertyForMainCategory() {
|
|
85 |
1
| String prop = cfg.getProperty("nonExisting");
|
|
86 |
1
| assertEquals(null,prop);
|
|
87 |
| } |
|
88 |
| |
|
89 |
1
| public void testGetNonExistingPropertyWithDefaultForMainCategory() {
|
|
90 |
1
| String prop = cfg.getProperty("nonExisting","default");
|
|
91 |
1
| assertEquals("default",prop);
|
|
92 |
| } |
|
93 |
| |
|
94 |
1
| public void testSetAndGetExistingPropertyForNonMainCategory() {
|
|
95 |
1
| cfg.setCategory("test");
|
|
96 |
1
| cfg.setProperty("testName","testValue","test");
|
|
97 |
1
| String prop = cfg.getProperty("testName",null,"test");
|
|
98 |
1
| assertEquals("testValue",prop);
|
|
99 |
| } |
|
100 |
| |
|
101 |
1
| public void testGetNonExistingPropertyForNonMainCategory() {
|
|
102 |
1
| cfg.setCategory("test");
|
|
103 |
1
| String prop = cfg.getProperty("testName",null,"test");
|
|
104 |
1
| assertEquals(null,prop);
|
|
105 |
| } |
|
106 |
| |
|
107 |
1
| public void testGetNonExistingPropertyWithDefaultForNonMainCategory() {
|
|
108 |
1
| cfg.setCategory("test");
|
|
109 |
1
| String prop = cfg.getProperty("testName","default","test");
|
|
110 |
1
| assertEquals("default",prop);
|
|
111 |
| } |
|
112 |
| |
|
113 |
1
| public void testGetNonExistingPropertyWithInheritance() {
|
|
114 |
1
| cfg.setCategory("test");
|
|
115 |
1
| cfg.setProperty("upperTest","value");
|
|
116 |
1
| String prop = cfg.getProperty("upperTest",null,"test");
|
|
117 |
1
| assertEquals("value",prop);
|
|
118 |
| } |
|
119 |
| |
|
120 |
1
| public void testGetIntProperty() {
|
|
121 |
1
| cfg.setCategory("test");
|
|
122 |
1
| cfg.setProperty("intValue","1","test");
|
|
123 |
1
| int prop = cfg.getIntProperty("intValue",100,"test");
|
|
124 |
1
| assertEquals(1,prop);
|
|
125 |
1
| Category category = cfg.getCategory("test");
|
|
126 |
1
| assertEquals(prop, category.getIntProperty("intValue",100));
|
|
127 |
| } |
|
128 |
| |
|
129 |
1
| public void testGetBooleanProperty() {
|
|
130 |
1
| cfg.setCategory("test");
|
|
131 |
1
| cfg.setProperty("booleanValue","true","test");
|
|
132 |
1
| boolean prop = cfg.getBooleanProperty("booleanValue",true,"test");
|
|
133 |
1
| assertEquals(true,prop);
|
|
134 |
| } |
|
135 |
| |
|
136 |
1
| public void testGetLongProperty() {
|
|
137 |
1
| cfg.setCategory("test");
|
|
138 |
1
| cfg.setProperty("longValue","327681","test");
|
|
139 |
1
| long prop = cfg.getLongProperty("longValue",100000,"test");
|
|
140 |
1
| assertEquals(327681,prop);
|
|
141 |
| } |
|
142 |
| |
|
143 |
1
| public void testGetDoubleProperty() {
|
|
144 |
1
| cfg.setCategory("test");
|
|
145 |
1
| cfg.setProperty("doubleValue","32.43","test");
|
|
146 |
1
| double prop = cfg.getDoubleProperty("doubleValue",1.0,"test");
|
|
147 |
1
| assertEquals(32.43,prop,0.0);
|
|
148 |
| } |
|
149 |
| |
|
150 |
1
| public void testGetCharProperty() {
|
|
151 |
1
| cfg.setCategory("test");
|
|
152 |
1
| cfg.setProperty("charValue","C","test");
|
|
153 |
1
| char prop = cfg.getCharProperty("charValue",'D',"test");
|
|
154 |
1
| assertEquals('C',prop);
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
1
| public void testPropertyListener() {
|
|
163 |
1
| MockPropListener mpl = new MockPropListener();
|
|
164 |
1
| cfg.addPropertyListener(mpl, "test");
|
|
165 |
1
| cfg.setProperty("testName","testValue","test");
|
|
166 |
1
| PropertyChangedEvent e = mpl.getEvent();
|
|
167 |
1
| assertNotNull(e);
|
|
168 |
1
| assertEquals("Expected property name incorrect.", "testName", e.getPropertyName());
|
|
169 |
1
| assertEquals("Expected new value incorrect.", "testValue", e.getNewValue());
|
|
170 |
1
| assertEquals("Expected event type not found", PropertyChangedEvent.PROPERTY_ADDED, e.getEventType());
|
|
171 |
1
| assertNull("Expected null, because this property was added!", e.getOldValue());
|
|
172 |
| |
|
173 |
1
| cfg.setProperty("testName","newTestValue","test");
|
|
174 |
1
| e = mpl.getEvent();
|
|
175 |
1
| assertNotNull(e);
|
|
176 |
1
| assertEquals("Expected property name incorrect.", "testName", e.getPropertyName());
|
|
177 |
1
| assertEquals("Expected new value incorrect.", "newTestValue", e.getNewValue());
|
|
178 |
1
| assertEquals("Expected event type not found", PropertyChangedEvent.PROPERTY_CHANGED, e.getEventType());
|
|
179 |
1
| assertEquals("Expected old value to be set!", "testValue", e.getOldValue());
|
|
180 |
| |
|
181 |
1
| cfg.setProperty("testName",null,"test");
|
|
182 |
1
| e = mpl.getEvent();
|
|
183 |
1
| assertNotNull(e);
|
|
184 |
1
| assertEquals("Expected property name incorrect.", "testName", e.getPropertyName());
|
|
185 |
1
| assertNull("Expected new value incorrect.", e.getNewValue());
|
|
186 |
1
| assertEquals("Expected event type not found", PropertyChangedEvent.PROPERTY_REMOVED, e.getEventType());
|
|
187 |
1
| assertEquals("Expected old value to be set!", "newTestValue", e.getOldValue());
|
|
188 |
| } |
|
189 |
| |
|
190 |
0
| public void propertyChanged(PropertyChangedEvent e) {
|
|
191 |
0
| assertEquals("test",e.getNewValue());
|
|
192 |
0
| assertEquals("testName",e.getPropertyName());
|
|
193 |
0
| assertEquals("testValue", e.getOldValue());
|
|
194 |
| } |
|
195 |
| |
|
196 |
1
| public void testNumberOfCategories() {
|
|
197 |
1
| assertEquals(1, cfg.getNumberOfCategories());
|
|
198 |
| } |
|
199 |
| |
|
200 |
1
| public void testGetConfigName() {
|
|
201 |
1
| Configuration config = ConfigurationManager.getConfiguration("test");
|
|
202 |
1
| assertNotNull(config);
|
|
203 |
1
| assertEquals("test",config.getConfigName());
|
|
204 |
| } |
|
205 |
| |
|
206 |
1
| public void testSetCategory() {
|
|
207 |
1
| Configuration config = ConfigurationManager.getConfiguration("abc");
|
|
208 |
1
| config.setCategory("xyz", false);
|
|
209 |
1
| config.setCategory("xyz", true);
|
|
210 |
1
| assertEquals("xyz", config.getMainCategoryName());
|
|
211 |
| } |
|
212 |
| |
|
213 |
1
| public void testSetAndGetPropertyFromNEConfig() {
|
|
214 |
1
| Configuration config = ConfigurationManager.getConfiguration("abcd");
|
|
215 |
1
| config.setProperty("hello","world");
|
|
216 |
1
| String val = config.getProperty("hello");
|
|
217 |
1
| assertEquals("world",val);
|
|
218 |
| } |
|
219 |
| |
|
220 |
1
| public void testGetArray() {
|
|
221 |
1
| Configuration config = ConfigurationManager.getConfiguration();
|
|
222 |
1
| assertNotNull(config);
|
|
223 |
1
| String[] ar = config.getArray("array");
|
|
224 |
1
| assertNotNull(ar);
|
|
225 |
1
| assertEquals("is",ar[0]);
|
|
226 |
1
| assertEquals("out",ar[3]);
|
|
227 |
1
| ar = config.getArray("arr",new String[]{"hello","world"});
|
|
228 |
1
| assertNotNull(ar);
|
|
229 |
1
| assertEquals("world",ar[1]);
|
|
230 |
1
| ar = config.getArray("complexArray",new String[]{"hello","world"},"MyApp");
|
|
231 |
1
| assertNotNull(ar);
|
|
232 |
1
| assertEquals("hello",ar[0]);
|
|
233 |
1
| assertEquals("MyStuff",ar[1]);
|
|
234 |
| } |
|
235 |
| |
|
236 |
1
| public void testGetPropertyNames() {
|
|
237 |
1
| Configuration config = ConfigurationManager.getConfiguration();
|
|
238 |
1
| assertNotNull(config);
|
|
239 |
1
| String names[] = config.getPropertyNames("I do not exist");
|
|
240 |
1
| assertNull(names);
|
|
241 |
| } |
|
242 |
| |
|
243 |
1
| public void testGetPropertyWithEscape() {
|
|
244 |
1
| Configuration config = ConfigurationManager.getConfiguration();
|
|
245 |
1
| assertNotNull(config);
|
|
246 |
1
| String val = config.getProperty("getme",null,"MyApp");
|
|
247 |
1
| assertNotNull(val);
|
|
248 |
| } |
|
249 |
| |
|
250 |
1
| public void testCreated() {
|
|
251 |
1
| Configuration config = ConfigurationManager.getConfiguration("I am new");
|
|
252 |
1
| assertNotNull(config);
|
|
253 |
1
| assertEquals(true,config.isNew());
|
|
254 |
| } |
|
255 |
| |
|
256 |
1
| public void testCreated2() {
|
|
257 |
1
| Configuration config = ConfigurationManager.getConfiguration();
|
|
258 |
1
| assertNotNull(config);
|
|
259 |
1
| assertEquals(false,config.isNew());
|
|
260 |
| } |
|
261 |
| } |