1 17 18 package org.sape.carbon.core.config.format.test; 19 20 import java.awt.*; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 import org.sape.carbon.core.component.Lookup; 25 import org.sape.carbon.core.component.lifecycle.LifecycleInterceptor; 26 import org.sape.carbon.core.component.lifecycle.LifecycleStateEnum; 27 import org.sape.carbon.core.component.proxy.test.TestComponent; 28 import org.sape.carbon.core.config.Config; 29 import org.sape.carbon.core.config.Configuration; 30 import org.sape.carbon.core.config.ConfigurationStoreException; 31 import org.sape.carbon.core.config.node.Node; 32 import org.sape.carbon.core.config.test.TestConfiguration; 33 34 import junit.extensions.ActiveTestSuite; 35 import junit.framework.Test; 36 import junit.framework.TestCase; 37 import junit.framework.TestSuite; 38 39 48 public class ConfigurationArrayTypeTest extends TestCase { 49 50 public static final String TEST_DOCUMENT = "/core/config/test/ArrayTest"; 51 52 53 public static final String NODE_A_PATH = "Path of Node A"; 54 public static final String NODE_B_PATH = "Path of Node B"; 55 56 57 public ConfigurationArrayTypeTest(String name) { 58 super(name); 59 } 60 61 62 63 public void testStringArray() { 64 65 HolderConfiguration config = 66 (HolderConfiguration) 67 Config.getInstance().createConfiguration(HolderConfiguration.class); 68 69 config.addString("a"); 70 config.addString("b"); 71 config.addString("c"); 72 73 String [] strings = config.getString(); 74 for (int i = 0; i < strings.length; i++) { 75 String string = strings[i]; 76 } 78 79 assertEquals( 80 "String array value not as expected", 81 config.getString(0),"a"); 82 83 assertEquals( 84 "String array value not as expected", 85 config.getString(1),"b"); 86 87 assertEquals( 88 "String array value not as expected", 89 config.getString(2),"c"); 90 91 } 92 93 94 public void testComplexArray() { 95 96 HolderConfiguration config = 97 (HolderConfiguration) 98 Config.getInstance().createConfiguration(HolderConfiguration.class); 99 100 NodeConfiguration nodeA = 101 (NodeConfiguration) 102 Config.getInstance().createConfiguration(NodeConfiguration.class); 103 nodeA.setPath(NODE_A_PATH); 104 105 config.addNode(nodeA); 106 107 NodeConfiguration nodeB = 108 (NodeConfiguration) 109 Config.getInstance().createConfiguration(NodeConfiguration.class); 110 nodeB.setPath(NODE_B_PATH); 111 112 config.addNode(nodeB); 113 114 NodeConfiguration[] nodes = config.getNode(); 115 assertEquals("Map did not contain added node by array access.", 116 nodeA.getPath(), nodes[0].getPath()); 117 118 assertEquals("Map did not contain added node by array access", 119 nodeB.getPath(), nodes[1].getPath()); 120 121 assertEquals("Map did not contain added node by array access.", 122 nodeA.getPath(), config.getNode(0).getPath()); 123 124 assertEquals("Map did not contain added node by array access", 125 nodeB.getPath(), config.getNode(1).getPath()); 126 } 127 128 129 130 public void testPrimitiveArray() { 131 HolderConfiguration config = 132 (HolderConfiguration) 133 Config.getInstance().createConfiguration(HolderConfiguration.class); 134 135 config.addInt(0); 136 config.addInt(1); 137 config.addInt(2); 138 config.addInt(3); 139 140 141 for (int i = 0; i < 4; i++) { 142 assertEquals( 143 "Array primitive not equal", 144 config.getInt(i), i); 145 } 146 147 } 148 149 public void testArraySet() { 150 HolderConfiguration config = 151 (HolderConfiguration) 152 Config.getInstance().createConfiguration(HolderConfiguration.class); 153 154 int[] testInts = new int[] { 0,1}; 155 156 config.setInt(testInts); 157 158 for (int i = 0; i < testInts.length; i++) { 159 assertEquals( 160 "Set by Array object did not set array values", 161 testInts[i],config.getInt(i)); 162 } 163 } 164 165 public void testAlterPrimitiveContents() { 166 HolderConfiguration config = 167 (HolderConfiguration) 168 Config.getInstance().createConfiguration(HolderConfiguration.class); 169 170 config.addInt(0); 171 config.addInt(1); 172 config.addInt(2); 173 config.addInt(3); 174 175 config.setInt(2,42); 177 178 assertEquals( 179 "The altered integer array attribute did not seem to be set properly", 180 config.getInt(2),42); 181 182 } 183 184 public void testArrayChildConfigurations() throws Exception { 185 Node testNode = Config.getInstance().fetchNode(TEST_DOCUMENT); 186 Node[] children = testNode.fetchChildren(); 187 188 List validChildren = new ArrayList (); 189 validChildren.add("Node[0]"); 190 validChildren.add("Node[1]"); 191 validChildren.add("NestTest"); 192 193 assertTrue( 194 TEST_DOCUMENT + 195 " had wrong number of children, found [" + 196 children.length + 197 "] expected [" + validChildren.size() + "]", 198 children.length == validChildren.size()); 199 200 for (int i = 0; i < children.length; i++) { 201 Node child = children[i]; 202 assertTrue( 203 TEST_DOCUMENT + 204 " contained unexpected or duplicate child name [" + 205 child.getName() + "]", 206 validChildren.remove(child.getName())); 207 } 208 } 209 210 public void testNestedArray() { 211 212 NestedArray root = 213 (NestedArray) 214 Config.getInstance().createConfiguration(NestedArray.class); 215 addNest(root,5); 216 217 checkNest(root,5); 218 } 219 220 protected void addNest(NestedArray nestedArray, int nests) { 221 nestedArray.setName(String.valueOf(nests)); 222 if (nests > 0) { 223 --nests; 224 NestedArray nest = 225 (NestedArray) 226 Config.getInstance().createConfiguration(NestedArray.class); 227 228 addNest(nest, nests); 229 230 nestedArray.addNestedArray(nest); 231 } 232 } 233 protected void checkNest(NestedArray nestedArray, int nests) { 234 assertEquals( 235 "The nested array was not named according to its key.", 236 String.valueOf(nests), nestedArray.getName()); 237 238 239 if (nests > 0) { 240 --nests; 241 NestedArray child = nestedArray.getNestedArray(0); 242 assertNotNull( 243 "Nested child [" + nests + "] was not found.", 244 child); 245 checkNest(child, nests); 246 } 247 } 248 249 250 public void testPersist() throws ConfigurationStoreException { 251 252 HolderConfiguration config = 253 (HolderConfiguration) 254 Config.getInstance().createConfiguration(HolderConfiguration.class); 255 256 257 NodeConfiguration nodeA = 258 (NodeConfiguration) 259 Config.getInstance().createConfiguration(NodeConfiguration.class); 260 nodeA.setPath("Path of Node A"); 261 config.addNode(nodeA); 262 263 264 NodeConfiguration nodeB = 265 (NodeConfiguration) 266 Config.getInstance().createConfiguration(NodeConfiguration.class); 267 nodeB.setPath("Path of Node B"); 268 config.addNode(nodeB); 269 270 config.addString("a"); 271 config.addString("b"); 272 config.addString("c"); 273 274 275 config.addInt(0); 276 config.addInt(1); 277 config.addInt(2); 278 config.addInt(3); 279 280 281 NestedArray root = 282 (NestedArray) 283 Config.getInstance().createConfiguration(NestedArray.class); 284 285 addNest(root,5); 286 config.setNestTest(root); 287 288 Config.getInstance().storeConfiguration(TEST_DOCUMENT, config); 289 290 } 291 292 public void testChildAccess() { 293 NodeConfiguration config = 294 (NodeConfiguration) 295 Config.getInstance().fetchWritableConfiguration( 296 TEST_DOCUMENT + "/Node[0]"); 297 298 assertEquals( 299 "The child-accessed configuration did not contain the expected property", 300 config.getPath(), NODE_A_PATH); 301 } 302 303 public void testAlterChild() throws ConfigurationStoreException { 304 String alteredPath = "AlteredPath"; 305 String testChild = TEST_DOCUMENT + "/Node[0]"; 306 307 NodeConfiguration child = 308 (NodeConfiguration) 309 Config.getInstance().createConfiguration(NodeConfiguration.class); 310 child.setPath(alteredPath); 311 Config.getInstance().storeConfiguration( 312 testChild, 313 child); 314 315 NodeConfiguration readChild = 316 (NodeConfiguration) 317 Config.getInstance().fetchConfiguration(testChild); 318 319 assertEquals( 320 "The child, altered configuration did not contain the expected path", 321 readChild.getPath(), alteredPath); 322 } 323 324 public void testNonCacheableTypes() { 325 HolderConfiguration config = 327 (HolderConfiguration) 328 Config.getInstance().createConfiguration(HolderConfiguration.class); 329 330 config.addTestComponents( 331 (TestComponent) 332 Lookup.getInstance().fetchComponent("/core/config/test/TestComponent")); 333 334 LifecycleInterceptor lifecycleView = 336 (LifecycleInterceptor) config.getTestComponents(0); 337 lifecycleView.destroyComponent(); 338 339 lifecycleView = 342 (LifecycleInterceptor) config.getTestComponents(0); 343 344 TestCase.assertTrue( 345 "Config object returned a stale component reference which " + 346 "means it was cached and it should not have been", 347 lifecycleView.getLifecycleState() == LifecycleStateEnum.RUNNING); 348 } 349 350 public static interface HolderConfiguration extends Configuration { 351 352 String [] getString(); 353 String getString(int index); 354 void setString(int index, String value); 355 void setString(String [] values); 356 void addString(String value); 357 358 359 NodeConfiguration[] getNode(); 360 NodeConfiguration getNode(int index); 361 void setNode(int index, NodeConfiguration node); 362 void setNode(NodeConfiguration[] nodeArray); 363 void addNode(NodeConfiguration nodeArray); 364 365 int[] getInt(); 366 int getInt(int index); 367 void setInt(int index, int value); 368 void setInt(int[] integers); 369 void addInt(int value); 370 371 NestedArray getNestTest(); 372 void setNestTest(NestedArray nest); 373 374 TestComponent[] getTestComponents(); 375 TestComponent getTestComponents(int index); 376 void setTestComponents(TestComponent[] components); 377 void setTestComponents(int index, TestComponent component); 378 void addTestComponents(TestComponent component); 379 } 380 381 public static interface NodeConfiguration extends Configuration { 382 String getPath(); 383 void setPath(String path); 384 385 } 386 387 388 public static interface NestedArray extends Configuration { 389 390 String getName(); 391 void setName(String value); 392 393 NestedArray[] getNestedArray(); 394 NestedArray getNestedArray(int index); 395 void setNestedArray(int index, NestedArray value); 396 void setNestedArray(NestedArray[] nestedArray); 397 void addNestedArray(NestedArray nestedArray); 398 399 } 400 401 402 403 404 408 public static Test suite() { 409 TestSuite masterSuite = new TestSuite(); 410 411 Test singleThreadedTests = getSingleThreadedTests(); 413 if (singleThreadedTests != null) { 414 masterSuite.addTest(singleThreadedTests); 415 } 416 417 Test multiThreadedTests = getMultiThreadedTests(); 419 if (multiThreadedTests != null) { 420 masterSuite.addTest(multiThreadedTests); 421 } 422 423 return masterSuite; 424 } 425 426 435 private static Test getSingleThreadedTests() { 436 TestSuite suite = new TestSuite(); 437 438 suite.addTest(new ConfigurationArrayTypeTest("testPrimitiveArray")); 439 suite.addTest(new ConfigurationArrayTypeTest("testNestedArray")); 440 suite.addTest(new ConfigurationArrayTypeTest("testComplexArray")); 441 suite.addTest(new ConfigurationArrayTypeTest("testAlterPrimitiveContents")); 442 suite.addTest(new ConfigurationArrayTypeTest("testStringArray")); 443 suite.addTest(new ConfigurationArrayTypeTest("testArraySet")); 444 suite.addTest(new ConfigurationArrayTypeTest("testPersist")); 445 suite.addTest(new ConfigurationArrayTypeTest("testChildAccess")); 446 suite.addTest(new ConfigurationArrayTypeTest("testAlterChild")); 447 suite.addTest(new ConfigurationArrayTypeTest("testArrayChildConfigurations")); 448 suite.addTest(new ConfigurationArrayTypeTest("testNonCacheableTypes")); 449 450 return suite; 451 } 452 453 462 private static Test getMultiThreadedTests() { 463 TestSuite suite = new ActiveTestSuite(); 464 470 return suite; 471 } 472 473 483 private static void addTest(TestSuite suite, String testName, int number) { 484 for (int count = 0; count < number; count++) { 485 suite.addTest(new ConfigurationArrayTypeTest(testName)); 486 } 487 } 488 } | Popular Tags |