| 1 17 18 package org.sape.carbon.core.config.format.test; 19 20 import java.util.Arrays ; 21 22 import org.sape.carbon.core.config.Config; 23 import org.sape.carbon.core.config.InvalidConfigurationException; 24 import org.sape.carbon.core.config.PropertyConfiguration; 25 26 import junit.extensions.ActiveTestSuite; 27 import junit.framework.Test; 28 import junit.framework.TestCase; 29 import junit.framework.TestSuite; 30 31 32 41 public class PropertyConfigurationTest extends TestCase { 42 43 private static final String TEST_CHILD_DOCUMENT = 44 "/core/test/ExtendablePropertyConfigTestChild"; 45 private static final String TEST_PARENT_DOCUMENT = 46 "/core/test/ExtendablePropertyConfigTestParent"; 47 48 private PropertyConfiguration config; 49 50 public PropertyConfigurationTest(String name) { 51 super(name); 52 } 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 config = (PropertyConfiguration) 57 Config.getInstance().createConfiguration(PropertyConfiguration.class); 58 } 59 60 71 72 75 public void testWritable() { 76 String testStringName = "WriteTestProperty"; 77 String testString = "FooBarBazSplat"; 78 79 if (!this.config.isConfigurationWritable()) { 80 fail("Configuration was not created writable."); 81 } 82 83 this.config.setConfigurationReadOnly(); 84 try { 85 this.config.setProperty(testStringName, testString); 86 fail("Did not receive the expected exception when writing to a read-only document."); 87 } catch (UnsupportedOperationException uoe) { 88 } 90 91 this.config = (PropertyConfiguration) this.config.clone(); 92 if (!this.config.isConfigurationWritable()) { 93 fail("Configuration clone was not writable"); 94 } 95 } 96 97 98 public void testStringProperty() { 99 final String propertyName = "MyString"; 100 final String expected = "Hello, World!"; 101 102 this.config.setProperty(propertyName, expected); 103 String value = this.config.getProperty(propertyName); 104 if (!expected.equals(value)) { 105 fail("String property failed, got [" + 106 value + "] expected [" + 107 expected + "]."); 108 } 109 } 110 111 public void testShortProperty() { 112 final String propertyName = "MyShort"; 113 final short expected = 3; 114 115 this.config.setShortProperty(propertyName, expected); 116 short value = this.config.getShortProperty(propertyName); 117 if (expected != value) { 118 fail("Short property failed, got [" + 119 value + "] expected [" + 120 expected + "]."); 121 } 122 } 123 124 public void testIntProperty() { 125 final String propertyName = "MyInt"; 126 final int expected = 4; 127 128 this.config.setIntProperty(propertyName, expected); 129 int value = this.config.getIntProperty(propertyName); 130 if (expected != value) { 131 fail("Int property failed, got [" + 132 value + "] expected [" + 133 expected + "]."); 134 } 135 } 136 137 public void testLongProperty() { 138 final String propertyName = "MyLong"; 139 final long expected = 5; 140 141 this.config.setLongProperty(propertyName, expected); 142 long value = this.config.getLongProperty(propertyName); 143 if (expected != value) { 144 fail("Long property failed, got [" + 145 value + "] expected [" + 146 expected + "]."); 147 } 148 } 149 150 public void testWSVProperty() { 151 final String propertyName = "MyWhiteSpacedValue"; 152 final String [] expected = {"A","B","C"}; 153 154 this.config.setWSVProperty(propertyName, expected); 155 String [] value = this.config.getWSVProperty(propertyName); 156 157 if ( ! Arrays.equals(expected, value) ) { 158 fail("The retrieved array " + Arrays.asList(value)+ 159 " doesnt match with the expected values "+Arrays.asList(expected)); 160 } 161 } 162 163 public void testCSVProperty() { 164 final String propertyName = "MyCommaSpacedValue"; 165 final String [] expected = {"D","E","F"}; 166 167 this.config.setCSVProperty(propertyName, expected); 168 String [] value = this.config.getCSVProperty(propertyName); 169 170 if ( ! Arrays.equals(expected, value) ) { 171 fail("The retrieved array " + Arrays.asList(value)+ 172 " doesnt match with the expected values "+Arrays.asList(expected)); 173 } 174 } 175 176 public void testDeepProperty() { 177 final String propertyName = "MyArray.MyFloat"; 178 final float expected = 1.1F; 179 180 this.config.setFloatProperty(propertyName, expected); 181 float value = this.config.getFloatProperty(propertyName); 182 if (expected != value) { 183 fail("Deep float property failed, got [" + 184 value + "] expected [" + 185 expected + "]."); 186 } 187 } 188 189 public void testDeeperProperty() { 190 final String propertyName = "A.B.C.D.E.F.G"; 191 final String expected = "foobar"; 192 193 this.config.setProperty(propertyName, expected); 194 String value = this.config.getProperty(propertyName); 195 if (!expected.equals(value)) { 196 fail("Deep string property failed, got [" + 197 value + "] expected [" + 198 expected + "]."); 199 } 200 } 201 202 public void testDefaultValues() { 203 final String propertyName = "BogusName"; 204 final String defaultValue = "foobar"; 205 206 try { 207 this.config.getProperty(propertyName); 208 fail("Did not catch expected InvalidConfigurationException"); 209 } catch (InvalidConfigurationException ice) { 210 } 212 213 String value = this.config.getProperty(propertyName, defaultValue); 214 215 if (!defaultValue.equals(value)) { 216 fail("Property default failed, got [" + 217 value + "] expected [" + 218 defaultValue + "]."); 219 } 220 } 221 222 public void testChildStringProperty() { 223 PropertyConfiguration configParent = 224 (PropertyConfiguration) 225 Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT); 226 227 PropertyConfiguration configChild = 228 (PropertyConfiguration) 229 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 230 231 String value = configChild.getProperty("MyString"); 232 String expected = "Hello, World!"; 233 if (!expected.equals(value)) { 234 fail("String property failed, got [" + 235 value + "] expected [" + 236 expected + "]."); 237 } 238 239 if (!configChild.getProperty("MyStringP").equals( 240 configParent.getProperty("MyStringP"))) { 241 242 fail("String property inheritance failed, child value did not " + 243 "match parent, child: [" + configChild.getProperty("MyStringP") + 244 "], parent: [" + configParent.getProperty("MyStringP") + "]"); 245 } 246 } 247 248 public void testChildShortProperty() { 249 PropertyConfiguration configParent = 250 (PropertyConfiguration) 251 Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT); 252 253 PropertyConfiguration configChild = 254 (PropertyConfiguration) 255 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 256 257 short value = configChild.getShortProperty("MyShort"); 258 short expected = 3; 259 if (expected != value) { 260 fail("Short property failed, got [" + 261 value + "] expected [" + 262 expected + "]."); 263 } 264 265 if (configChild.getShortProperty("MyShortP") != 266 configParent.getShortProperty("MyShortP")) { 267 268 fail("Short property inheritance failed, child value did not " + 269 "match parent, child: [" + configChild.getShortProperty("MyShortP") + 270 "], parent: [" + configParent.getShortProperty("MyShortP") + "]"); 271 } 272 } 273 274 public void testChildIntProperty() { 275 PropertyConfiguration configParent = 276 (PropertyConfiguration) 277 Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT); 278 279 PropertyConfiguration configChild = 280 (PropertyConfiguration) 281 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 282 283 int value = configChild.getIntProperty("MyInt"); 284 int expected = 4; 285 if (expected != value) { 286 fail("Int property failed, got [" + 287 value + "] expected [" + 288 expected + "]."); 289 } 290 291 if (configChild.getIntProperty("MyIntP") != 292 configParent.getIntProperty("MyIntP")) { 293 294 fail("Int property inheritance failed, child value did not " + 295 "match parent, child: [" + configChild.getIntProperty("MyIntP") + 296 "], parent: [" + configParent.getIntProperty("MyIntP") + "]"); 297 } 298 } 299 300 public void testChildLongProperty() { 301 PropertyConfiguration configParent = 302 (PropertyConfiguration) 303 Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT); 304 305 PropertyConfiguration configChild = 306 (PropertyConfiguration) 307 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 308 309 long value = configChild.getLongProperty("MyLong"); 310 long expected = 5; 311 if (expected != value) { 312 fail("Long property failed, got [" + 313 value + "] expected [" + 314 expected + "]."); 315 } 316 317 if (configChild.getLongProperty("MyLongP") != 318 configParent.getLongProperty("MyLongP")) { 319 320 fail("Long property inheritance failed, child value did not " + 321 "match parent, child: [" + configChild.getLongProperty("MyLongP") + 322 "], parent: [" + configParent.getLongProperty("MyLongP") + "]"); 323 } 324 } 325 326 public void testChildDeepProperty() { 327 PropertyConfiguration configParent = 328 (PropertyConfiguration) 329 Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT); 330 331 PropertyConfiguration configChild = 332 (PropertyConfiguration) 333 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 334 335 float value = configChild.getFloatProperty("MyArray.MyFloat"); 336 float expected = 1.1F; 337 if (expected != value) { 338 fail("Deep Float property failed, got [" + 339 value + "] expected [" + 340 expected + "]."); 341 } 342 343 if (configChild.getFloatProperty("MyArrayP.MyFloat") != 344 configParent.getFloatProperty("MyArrayP.MyFloat")) { 345 346 fail("Long property inheritance failed, child value did not " + 347 "match parent, child: [" + configChild.getFloatProperty("MyArrayP.MyFloat") + 348 "], parent: [" + configParent.getFloatProperty("MyArrayP.MyFloat") + "]"); 349 } 350 } 351 352 public void testChildDeeperProperty() { 353 PropertyConfiguration configParent = 354 (PropertyConfiguration) 355 Config.getInstance().fetchConfiguration(TEST_PARENT_DOCUMENT); 356 357 PropertyConfiguration configChild = 358 (PropertyConfiguration) 359 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 360 361 String value = configChild.getProperty("A.B.C.D.E.F.G"); 362 String expected = "foobar"; 363 if (!expected.equals(value)) { 364 fail("Deep string property failed, got [" + 365 value + "] expected [" + 366 expected + "]."); 367 } 368 369 if (configChild.getProperty("AP.B.C.D.E.F.G") != 370 configParent.getProperty("AP.B.C.D.E.F.G")) { 371 372 fail("Deep string property inheritance failed, child value did not " + 373 "match parent, child: [" + configChild.getProperty("AP.B.C.D.E.F.G") + 374 "], parent: [" + configParent.getProperty("AP.B.C.D.E.F.G") + "]"); 375 } 376 } 377 378 public void testOverriddenProperty() { 379 PropertyConfiguration configChild = 380 (PropertyConfiguration) 381 Config.getInstance().fetchConfiguration(TEST_CHILD_DOCUMENT); 382 383 String value = configChild.getProperty("Overriden"); 384 String expected = "bar"; 385 if (!expected.equals(value)) { 386 fail("Deep string property failed, got [" + 387 value + "] expected [" + 388 expected + "]."); 389 } 390 } 391 392 396 public static Test suite() { 397 TestSuite masterSuite = new TestSuite(); 398 399 Test singleThreadedTests = getSingleThreadedTests(); 401 if (singleThreadedTests != null) { 402 masterSuite.addTest(singleThreadedTests); 403 } 404 405 Test multiThreadedTests = getMultiThreadedTests(); 407 if (multiThreadedTests != null) { 408 masterSuite.addTest(multiThreadedTests); 409 } 410 411 return masterSuite; 412 } 413 414 423 private static Test getSingleThreadedTests() { 424 TestSuite suite = new TestSuite(); 425 426 suite.addTest(new PropertyConfigurationTest("testWritable")); 427 suite.addTest(new PropertyConfigurationTest("testStringProperty")); 428 suite.addTest(new PropertyConfigurationTest("testShortProperty")); 429 suite.addTest(new PropertyConfigurationTest("testIntProperty")); 430 suite.addTest(new PropertyConfigurationTest("testLongProperty")); 431 suite.addTest(new PropertyConfigurationTest("testDeepProperty")); 432 suite.addTest(new PropertyConfigurationTest("testDeeperProperty")); 433 434 suite.addTest(new PropertyConfigurationTest("testChildStringProperty")); 435 suite.addTest(new PropertyConfigurationTest("testChildShortProperty")); 436 suite.addTest(new PropertyConfigurationTest("testChildIntProperty")); 437 suite.addTest(new PropertyConfigurationTest("testChildLongProperty")); 438 suite.addTest(new PropertyConfigurationTest("testChildDeepProperty")); 439 suite.addTest(new PropertyConfigurationTest("testChildDeeperProperty")); 440 441 suite.addTest(new PropertyConfigurationTest("testOverriddenProperty")); 442 443 suite.addTest(new PropertyConfigurationTest("testWSVProperty")); 444 suite.addTest(new PropertyConfigurationTest("testCSVProperty")); 445 suite.addTest(new PropertyConfigurationTest("testDefaultValues")); 446 447 return suite; 448 } 449 450 459 private static Test getMultiThreadedTests() { 460 TestSuite suite = new ActiveTestSuite(); 461 467 return suite; 468 } 469 470 480 private static void addTest(TestSuite suite, String testName, int number) { 481 for(int count=0; count<number; count++) { 482 suite.addTest(new PropertyConfigurationTest(testName)); 483 } 484 } 485 486 } 487 | Popular Tags |