|
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.NestedConfigParser; |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| public class NestedConfigurationTest extends TestCase implements PropertyListener { |
|
20 |
| |
|
21 |
| private Configuration cfg; |
|
22 |
0
| public NestedConfigurationTest(String name) {
|
|
23 |
0
| super(name);
|
|
24 |
| } |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
0
| public static void main(String[] args) {
|
|
31 |
0
| junit.textui.TestRunner.run(NestedConfigurationTest.class);
|
|
32 |
| } |
|
33 |
| |
|
34 |
0
| protected void setUp() {
|
|
35 |
0
| System.setProperty("jconfig.parser", NestedConfigParser.class.getName());
|
|
36 |
0
| cfg = new NestedConfiguration("ConfigurationTest");
|
|
37 |
0
| assertNotNull(cfg);
|
|
38 |
| } |
|
39 |
| |
|
40 |
0
| protected void tearDown() {
|
|
41 |
| } |
|
42 |
| |
|
43 |
0
| public void testGetAllCategoryNames() {
|
|
44 |
0
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
45 |
0
| String[] names = config.getCategoryNames();
|
|
46 |
0
| assertEquals(6,names.length);
|
|
47 |
| } |
|
48 |
| |
|
49 |
0
| public void testSetCategory2() {
|
|
50 |
0
| cfg.setCategory("test");
|
|
51 |
0
| String[] names = cfg.getCategoryNames();
|
|
52 |
0
| assertEquals(2,names.length);
|
|
53 |
| } |
|
54 |
| |
|
55 |
0
| public void testAddMainCategory() {
|
|
56 |
0
| cfg.setCategory("test",true);
|
|
57 |
0
| String[] names = cfg.getCategoryNames();
|
|
58 |
0
| assertEquals(2,names.length);
|
|
59 |
0
| String main = cfg.getMainCategoryName();
|
|
60 |
0
| assertEquals("test",main);
|
|
61 |
| } |
|
62 |
| |
|
63 |
0
| public void testRemoveCategory() {
|
|
64 |
0
| cfg.setCategory("test");
|
|
65 |
0
| String[] names = cfg.getCategoryNames();
|
|
66 |
0
| assertEquals(2,names.length);
|
|
67 |
0
| cfg.removeCategory("test");
|
|
68 |
0
| names = cfg.getCategoryNames();
|
|
69 |
0
| assertEquals(1,names.length);
|
|
70 |
| } |
|
71 |
| |
|
72 |
0
| public void testGetNumberOfCategories() {
|
|
73 |
0
| cfg.setCategory("test");
|
|
74 |
0
| int count = cfg.getNumberOfCategories();
|
|
75 |
0
| assertEquals(2,count);
|
|
76 |
| } |
|
77 |
| |
|
78 |
0
| public void testSetAndGetExistingPropertyForMainCategory() {
|
|
79 |
0
| cfg.setProperty("testName","testValue");
|
|
80 |
0
| String prop = cfg.getProperty("testName");
|
|
81 |
0
| assertEquals("testValue",prop);
|
|
82 |
| } |
|
83 |
| |
|
84 |
0
| public void testGetNonExistingPropertyForMainCategory() {
|
|
85 |
0
| String prop = cfg.getProperty("nonExisting");
|
|
86 |
0
| assertEquals(null,prop);
|
|
87 |
| } |
|
88 |
| |
|
89 |
0
| public void testGetNonExistingPropertyWithDefaultForMainCategory() {
|
|
90 |
0
| String prop = cfg.getProperty("nonExisting","default");
|
|
91 |
0
| assertEquals("default",prop);
|
|
92 |
| } |
|
93 |
| |
|
94 |
0
| public void testSetAndGetExistingPropertyForNonMainCategory() {
|
|
95 |
0
| cfg.setCategory("test");
|
|
96 |
0
| cfg.setProperty("testName","testValue","test");
|
|
97 |
0
| String prop = cfg.getProperty("testName",null,"test");
|
|
98 |
0
| assertEquals("testValue",prop);
|
|
99 |
| } |
|
100 |
| |
|
101 |
0
| public void testGetNonExistingPropertyForNonMainCategory() {
|
|
102 |
0
| cfg.setCategory("test");
|
|
103 |
0
| String prop = cfg.getProperty("testName",null,"test");
|
|
104 |
0
| assertEquals(null,prop);
|
|
105 |
| } |
|
106 |
| |
|
107 |
0
| public void testGetNonExistingPropertyWithDefaultForNonMainCategory() {
|
|
108 |
0
| cfg.setCategory("test");
|
|
109 |
0
| String prop = cfg.getProperty("testName","default","test");
|
|
110 |
0
| assertEquals("default",prop);
|
|
111 |
| } |
|
112 |
| |
|
113 |
0
| public void testGetNonExistingPropertyWithInheritance() {
|
|
114 |
0
| cfg.setCategory("test");
|
|
115 |
0
| cfg.setProperty("upperTest","value");
|
|
116 |
0
| String prop = cfg.getProperty("upperTest",null,"test");
|
|
117 |
0
| assertEquals("value",prop);
|
|
118 |
| } |
|
119 |
| |
|
120 |
0
| public void testGetIntProperty() {
|
|
121 |
0
| cfg.setCategory("test");
|
|
122 |
0
| cfg.setProperty("intValue","1","test");
|
|
123 |
0
| int prop = cfg.getIntProperty("intValue",100,"test");
|
|
124 |
0
| assertEquals(1,prop);
|
|
125 |
| } |
|
126 |
| |
|
127 |
0
| public void testGetBooleanProperty() {
|
|
128 |
0
| cfg.setCategory("test");
|
|
129 |
0
| cfg.setProperty("booleanValue","true","test");
|
|
130 |
0
| boolean prop = cfg.getBooleanProperty("booleanValue",true,"test");
|
|
131 |
0
| assertEquals(true,prop);
|
|
132 |
| } |
|
133 |
| |
|
134 |
0
| public void testGetLongProperty() {
|
|
135 |
0
| cfg.setCategory("test");
|
|
136 |
0
| cfg.setProperty("longValue","327681","test");
|
|
137 |
0
| long prop = cfg.getLongProperty("longValue",100000,"test");
|
|
138 |
0
| assertEquals(327681,prop);
|
|
139 |
| } |
|
140 |
| |
|
141 |
0
| public void testGetDoubleProperty() {
|
|
142 |
0
| cfg.setCategory("test");
|
|
143 |
0
| cfg.setProperty("doubleValue","32.43","test");
|
|
144 |
0
| double prop = cfg.getDoubleProperty("doubleValue",1.0,"test");
|
|
145 |
0
| assertEquals(32.43,prop,0.0);
|
|
146 |
| } |
|
147 |
| |
|
148 |
0
| public void testGetCharProperty() {
|
|
149 |
0
| cfg.setCategory("test");
|
|
150 |
0
| cfg.setProperty("charValue","C","test");
|
|
151 |
0
| char prop = cfg.getCharProperty("charValue",'D',"test");
|
|
152 |
0
| assertEquals('C',prop);
|
|
153 |
| } |
|
154 |
| |
|
155 |
0
| public void testPropertyListener() {
|
|
156 |
0
| cfg.addPropertyListener(this);
|
|
157 |
0
| cfg.setCategory("test");
|
|
158 |
0
| cfg.setProperty("testName","testValue","test");
|
|
159 |
| } |
|
160 |
| |
|
161 |
0
| public void propertyChanged(PropertyChangedEvent e) {
|
|
162 |
0
| assertEquals("test",e.getNewValue());
|
|
163 |
0
| assertEquals("testName",e.getPropertyName());
|
|
164 |
0
| assertEquals("testValue", e.getOldValue());
|
|
165 |
| } |
|
166 |
| |
|
167 |
0
| public void testNumberOfCategories() {
|
|
168 |
0
| assertEquals(1, cfg.getNumberOfCategories());
|
|
169 |
| } |
|
170 |
| |
|
171 |
0
| public void testGetPropertyNamesForNECategory() {
|
|
172 |
0
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
173 |
0
| assertNotNull(config);
|
|
174 |
0
| String names[] = config.getPropertyNames("I do not exist");
|
|
175 |
0
| assertNull(names);
|
|
176 |
| } |
|
177 |
| |
|
178 |
0
| public void testGetPropertyNames() {
|
|
179 |
0
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
180 |
0
| assertNotNull(config);
|
|
181 |
0
| String names[] = config.getPropertyNames("inner/myinner/moreinner");
|
|
182 |
0
| assertNotNull(names);
|
|
183 |
0
| assertEquals(2,names.length);
|
|
184 |
| } |
|
185 |
| |
|
186 |
0
| public void testGetPropertyWithEscape() {
|
|
187 |
0
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
188 |
0
| assertNotNull(config);
|
|
189 |
0
| String val = config.getProperty("getme",null,"MyApp");
|
|
190 |
0
| assertNotNull(val);
|
|
191 |
| } |
|
192 |
| |
|
193 |
0
| public void testGetIncludedProperty() {
|
|
194 |
0
| Configuration config = ConfigurationManager.getConfiguration("nested");
|
|
195 |
0
| assertNotNull(config);
|
|
196 |
0
| String val = config.getProperty("varprop1",null,"includeTest");
|
|
197 |
0
| assertNotNull(val);
|
|
198 |
0
| assertEquals("value1",val);
|
|
199 |
| } |
|
200 |
| } |