1 17 18 package org.sape.carbon.core.config.format.test; 19 20 import java.util.Arrays ; 21 import java.util.Calendar ; 22 import java.util.Date ; 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.InvalidConfigurationException; 31 import org.sape.carbon.core.config.PropertyConfiguration; 32 import org.sape.carbon.core.config.format.ConfigurationFormatService; 33 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 34 import org.sape.carbon.core.config.node.Node; 35 import org.sape.carbon.core.exception.InvalidParameterException; 36 import org.sape.carbon.core.util.classify.SeverityEnum; 37 38 import junit.extensions.ActiveTestSuite; 39 import junit.framework.Test; 40 import junit.framework.TestCase; 41 import junit.framework.TestSuite; 42 43 52 public class ConfigurationFormatServiceTest extends TestCase { 53 54 public static final String TEST_STORE_LOCATION = "/core/test/WriteTest"; 55 public static final String TEST_TOKEN_LOCATION = "/core/test/TokenTest"; 56 public static final String PROPERTY_TOKEN_LOCATION = 57 "/core/test/PropertyTokens"; 58 public static final String CONFIG_TOKEN_LOCATION = 59 "/core/test/ConfigTokens"; 60 61 62 public ConfigurationFormatServiceTest(String name) { 63 super(name); 64 } 65 66 67 71 public static Test suite() { 72 TestSuite masterSuite = new TestSuite(); 73 74 Test singleThreadedTests = getSingleThreadedTests(); 76 if (singleThreadedTests != null) { 77 masterSuite.addTest(singleThreadedTests); 78 } 79 80 Test multiThreadedTests = getMultiThreadedTests(); 82 if (multiThreadedTests != null) { 83 masterSuite.addTest(multiThreadedTests); 84 } 85 86 return masterSuite; 87 } 88 89 122 private static Test getSingleThreadedTests() { 123 TestSuite suite = new TestSuite(); 124 130 131 suite.addTest(new ConfigurationFormatServiceTest("testNewConfiguration")); 132 suite.addTest(new ConfigurationFormatServiceTest("testWritable")); 133 suite.addTest(new ConfigurationFormatServiceTest("testChangeBool")); 134 suite.addTest(new ConfigurationFormatServiceTest("testChangeByte")); 135 suite.addTest(new ConfigurationFormatServiceTest("testChangeShort")); 136 suite.addTest(new ConfigurationFormatServiceTest("testChangeInt")); 137 suite.addTest(new ConfigurationFormatServiceTest("testChangeLong")); 138 suite.addTest(new ConfigurationFormatServiceTest("testChangeFloat")); 139 suite.addTest(new ConfigurationFormatServiceTest("testChangeDouble")); 140 suite.addTest(new ConfigurationFormatServiceTest("testChangeString")); 141 suite.addTest(new ConfigurationFormatServiceTest("testChangeClass")); 142 suite.addTest(new ConfigurationFormatServiceTest("testChangeDate")); 143 suite.addTest(new ConfigurationFormatServiceTest("testEmptyArray")); 144 suite.addTest(new ConfigurationFormatServiceTest("testNullValue")); 145 suite.addTest(new ConfigurationFormatServiceTest("testChangeSubConfiguration")); 146 suite.addTest(new ConfigurationFormatServiceTest("testReferences")); 147 148 suite.addTest(new ConfigurationFormatServiceTest("testEnumSupport")); 149 150 suite.addTest(new ConfigurationFormatServiceTest("testArrayAccess")); 151 suite.addTest(new ConfigurationFormatServiceTest("testIndexedArrayAccess")); 152 suite.addTest(new ConfigurationFormatServiceTest("testArrayBounds")); 153 suite.addTest(new ConfigurationFormatServiceTest("testAddArrayItem")); 154 155 suite.addTest(new ConfigurationFormatServiceTest("testFormatServiceConfigAccess")); 156 157 suite.addTest(new ConfigurationFormatServiceTest("testDefaultingConfig")); 158 suite.addTest(new ConfigurationFormatServiceTest("testSuperInterfaceDefaultedConfigurations")); 159 160 suite.addTest(new ConfigurationFormatServiceTest("testConfigurationWrite")); 161 suite.addTest(new ConfigurationFormatServiceTest("testConfigRead")); 162 163 suite.addTest(new ConfigurationFormatServiceTest("testClone")); 164 165 suite.addTest(new ConfigurationFormatServiceTest("testTokenReplacement")); 166 167 suite.addTest(new ConfigurationFormatServiceTest("testEqualsAndHashCode")); 168 suite.addTest(new ConfigurationFormatServiceTest("testNonCacheableTypes")); 169 170 return suite; 171 } 172 173 182 private static Test getMultiThreadedTests() { 183 TestSuite suite = new ActiveTestSuite(); 184 190 addTest(suite, "testConfigRead",20); 191 192 return suite; 193 } 194 195 205 private static void addTest(TestSuite suite, String testName, int number) { 206 for(int count=0; count<number; count++) { 207 suite.addTest(new ConfigurationFormatServiceTest(testName)); 208 } 209 } 210 211 212 213 214 215 216 217 218 private static ConfigurationFormatService configurationFormatService; 219 private static FormatTestConfiguration config; 220 221 222 private static boolean BOOL = true; 223 private static byte BYTE = Byte.MAX_VALUE - 1; 224 private static short SHORT = Short.MAX_VALUE - 1; 225 private static int INT = Integer.MAX_VALUE - 1; 226 private static long LONG = Long.MAX_VALUE - 1L; 227 private static float FLOAT = Float.MAX_VALUE - 2.2F; 228 private static double DOUBLE = Double.MAX_VALUE - 2.2D; 229 private static String STRING = "Hello, World!"; 230 private static Class CLASS = ConfigurationFormatServiceTest.class; 231 232 233 private static Date DATE; 234 static { 235 Calendar cal = Calendar.getInstance(); 236 cal.set(2002,8,22,0,0,0); 237 cal.set(Calendar.MILLISECOND,0); 238 DATE = cal.getTime(); 239 } 240 241 245 public void testNewConfiguration() { 246 ConfigurationFormatServiceTest.configurationFormatService = 247 new DefaultConfigurationFormatService(); 248 249 ConfigurationFormatServiceTest.config = 250 (FormatTestConfiguration) 251 ConfigurationFormatServiceTest. 252 configurationFormatService. 253 newConfiguration(FormatTestConfiguration.class); 254 } 255 256 259 public void testWritable() { 260 String testString = "FooBarBazSplat"; 261 FormatTestConfiguration testConfig = (FormatTestConfiguration) 262 ConfigurationFormatServiceTest.config.clone(); 263 264 if (!testConfig.isConfigurationWritable()) { 265 fail("Configuration clone was not writable"); 266 } 267 testConfig.setMyString(testString); 268 if (!testConfig.getMyString().equals(testString)) { 269 fail("Unable to set a string in a document that was writable."); 270 } 271 272 testConfig.setConfigurationReadOnly(); 273 try { 274 testConfig.setMyString(testString); 275 fail("Did not receive the expected exception when writing to a read-only document."); 276 } catch (UnsupportedOperationException uoe) { 277 } 279 280 testConfig = (FormatTestConfiguration) testConfig.clone(); 281 282 if (!testConfig.isConfigurationWritable()) { 283 fail("Configuration clone was not writable"); 284 } 285 286 } 287 288 291 public void testChangeBool() { 292 ConfigurationFormatServiceTest.config.setMyBool(BOOL); 293 if (ConfigurationFormatServiceTest.config.getMyBool() != BOOL) { 294 fail("Bool value not the same as value set."); 295 } 296 } 297 298 301 public void testChangeByte() { 302 ConfigurationFormatServiceTest.config.setMyByte(BYTE); 303 if (ConfigurationFormatServiceTest.config.getMyByte() != BYTE) { 304 fail("Byte value not the same as value set."); 305 } 306 } 307 308 311 public void testChangeShort() { 312 ConfigurationFormatServiceTest.config.setMyShort(SHORT); 313 if (ConfigurationFormatServiceTest.config.getMyShort() != SHORT) { 314 fail("Short value not the same as value set."); 315 } 316 } 317 318 321 public void testChangeInt() { 322 ConfigurationFormatServiceTest.config.setMyInt(INT); 323 if (ConfigurationFormatServiceTest.config.getMyInt() != INT) { 324 fail("Int value not the same as value set."); 325 } 326 } 327 328 331 public void testChangeLong() { 332 ConfigurationFormatServiceTest.config.setMyLong(LONG); 333 if (ConfigurationFormatServiceTest.config.getMyLong() != LONG) { 334 fail("Long value not the same as value set."); 335 } 336 } 337 338 341 public void testChangeFloat() { 342 ConfigurationFormatServiceTest.config.setMyFloat(FLOAT); 343 if (ConfigurationFormatServiceTest.config.getMyFloat() != FLOAT) { 344 fail("Float value not the same as value set."); 345 } 346 } 347 348 351 public void testChangeDouble() { 352 ConfigurationFormatServiceTest.config.setMyDouble(DOUBLE); 353 if (ConfigurationFormatServiceTest.config.getMyDouble() != DOUBLE) { 354 fail("Double value not the same as value set."); 355 } 356 } 357 358 361 public void testChangeString() { 362 ConfigurationFormatServiceTest.config.setMyString(STRING); 363 if (ConfigurationFormatServiceTest.config.getMyString() != STRING) { 364 fail("String value not the same as value set."); 365 } 366 } 367 368 371 public void testChangeClass() { 372 ConfigurationFormatServiceTest.config.setMyClass(CLASS); 373 if (ConfigurationFormatServiceTest.config.getMyClass() != CLASS) { 374 fail("Class value not the same as value set."); 375 } 376 } 377 378 381 public void testChangeDate() { 382 ConfigurationFormatServiceTest.config.setMyDate(DATE); 383 if (!ConfigurationFormatServiceTest.config.getMyDate().equals(DATE)) { 384 fail("Date value [" + 385 DATE + 386 "] not the same as value set [" + 387 ConfigurationFormatServiceTest.config.getMyDate() + "]."); 388 } 389 } 390 391 394 public void testChangeSubConfiguration() { 395 396 Configuration newConfig = 397 configurationFormatService.newConfiguration(Configuration.class); 398 399 String expectedSubName = 400 ConfigurationFormatServiceTest.config.getConfigurationName() 401 + Node.DELIMITER 402 + "SubConfiguration"; 403 404 ConfigurationFormatServiceTest 405 .config 406 .setSubConfiguration(newConfig); 407 408 assertTrue( 409 "Reference to original configuration was not maintained", 410 ConfigurationFormatServiceTest 411 .config 412 .getSubConfiguration() 413 .getConfigurationName() 414 .equals(expectedSubName)); 415 } 416 417 420 public void testReferences() { 421 ConfigurationFormatServiceTest 422 .config 423 .setSubConfigurationRef( 424 (Config.getInstance().fetchConfiguration(TEST_TOKEN_LOCATION))); 425 426 assertTrue( 427 "Reference to original configuration was not maintained", 428 ConfigurationFormatServiceTest 429 .config 430 .getSubConfigurationRef() 431 .getConfigurationName() 432 .equals(TEST_TOKEN_LOCATION)); 433 } 434 435 public void testEnumSupport() { 436 ConfigurationFormatServiceTest.config.setEnum(SeverityEnum.FATAL); 437 if (ConfigurationFormatServiceTest.config.getEnum() != SeverityEnum.FATAL) { 438 fail("Enum value not the same as value set."); 439 } 440 } 441 442 443 public void testNullValue() { 444 String value = ConfigurationFormatServiceTest.config.getNullValue(); 445 446 if (value != null) { 447 fail("Was expecting attribute [NullValue] to be null, got: " + value); 448 } 449 450 boolean failed = false; 451 int intValue = -100; 452 try { 453 intValue = ConfigurationFormatServiceTest.config.getNullPrimitive(); 454 } catch (InvalidConfigurationException ice) { 455 failed = true; 456 } 457 if (!failed) { 458 fail("Expected thrown exception when retrieving a primitive that was null, but got [" + 459 intValue + "]."); 460 } 461 462 463 value = ConfigurationFormatServiceTest.config.getDefaultNullValue(); 464 465 if (value != null) { 466 fail("Was expecting attribute [DefaultNullValue] to be null, got: " + value); 467 } 468 } 469 470 471 475 public void testEmptyArray() { 476 String [] empty = ConfigurationFormatServiceTest.config.getEmptyArray(); 477 478 if (empty == null) { 479 fail("Empty array was null."); 480 } 481 482 if (empty.length != 0) { 483 fail("Emtpy array had a non-zero length of: " + empty.length); 484 } 485 486 } 487 488 492 public void testArrayAccess() { 493 int[] testIntArray = new int[] { 1, 2, 3, 4 }; 495 ConfigurationFormatServiceTest.config.setIntArray(testIntArray); 496 if (!Arrays.equals( 497 testIntArray, ConfigurationFormatServiceTest.config.getIntArray())) { 498 499 fail("Array returned from configuration did not match original " + 500 "array, either the set or get of the array failed"); 501 } 502 503 String [] testStringArray = new String [] { "1", "2", "3", "4" }; 505 ConfigurationFormatServiceTest.config.setStringArray(testStringArray); 506 String [] stringArrayFromConfig = 507 ConfigurationFormatServiceTest.config.getStringArray(); 508 509 if (testStringArray.length != stringArrayFromConfig.length) { 510 fail("Array returned from configuration did not match original " + 511 "array, either the set or get of the array failed"); 512 } 513 for (int i = 0; i < testStringArray.length; i++) { 514 if (!testStringArray[i].equals(stringArrayFromConfig[i])) { 515 fail("Array returned from configuration did not match " + 516 "original array, either the set or get of the array failed"); 517 } 518 } 519 520 FormatTestConfiguration[] testConfigArray = 522 new FormatTestConfiguration[4]; 523 524 for (int i = 0; i < testConfigArray.length; i++) { 525 testConfigArray[i] = (FormatTestConfiguration) 526 ConfigurationFormatServiceTest.configurationFormatService. 527 newConfiguration(FormatTestConfiguration.class); 528 529 testConfigArray[i].setMyInt(i + 1); 530 } 531 532 ConfigurationFormatServiceTest.config.setMyArray(testConfigArray); 533 FormatTestConfiguration[] arrayFromConfig = 534 ConfigurationFormatServiceTest.config.getMyArray(); 535 536 if (testConfigArray.length != arrayFromConfig.length) { 537 fail("Array returned from configuration did not match original " + 538 "array, either the set or get of the array failed"); 539 } 540 for (int i = 0; i < testConfigArray.length; i++) { 541 if (testConfigArray[i].getMyInt() != 542 arrayFromConfig[i].getMyInt()) { 543 544 fail("Array returned from configuration did not match " + 545 "original array, either the set or get of the array failed"); 546 } 547 } 548 } 549 550 554 public void testIndexedArrayAccess() { 555 ConfigurationFormatServiceTest.config.setIntArray(2, 10); 557 if (ConfigurationFormatServiceTest.config.getIntArray(2) != 10) { 558 fail("Array element from configuration did not match set value, " + 559 "either the set or get of the array failed"); 560 } 561 562 ConfigurationFormatServiceTest.config.setStringArray(2, "10"); 564 if (!ConfigurationFormatServiceTest.config. 565 getStringArray(2).equals("10")) { 566 567 fail("Array element from configuration did not match set value, " + 568 "either the set or get of the array failed"); 569 } 570 571 FormatTestConfiguration testConfig = (FormatTestConfiguration) 573 ConfigurationFormatServiceTest.configurationFormatService. 574 newConfiguration(FormatTestConfiguration.class); 575 576 testConfig.setMyInt(10); 577 578 ConfigurationFormatServiceTest.config.setMyArray(2, testConfig); 579 if (ConfigurationFormatServiceTest.config.getMyArray(2).getMyInt() != 10) { 580 fail("Array element from configuration did not match set value, " + 581 "either the set or get of the array failed"); 582 } 583 } 584 585 590 public void testArrayBounds() { 591 try { 593 ConfigurationFormatServiceTest.config.setIntArray(-1, 10); 594 fail("InvalidParameterException not thrown for invalid index"); 595 } catch(InvalidParameterException ipe) { 596 } 598 try { 599 ConfigurationFormatServiceTest.config.setIntArray(4, 10); 600 fail("InvalidParameterException not thrown for invalid index"); 601 } catch(InvalidParameterException ipe) { 602 } 604 try { 605 ConfigurationFormatServiceTest.config.getIntArray(-1); 606 fail("InvalidParameterException not thrown for invalid index"); 607 } catch(InvalidParameterException ipe) { 608 } 610 try { 611 ConfigurationFormatServiceTest.config.getIntArray(4); 612 fail("InvalidParameterException not thrown for invalid index"); 613 } catch(InvalidParameterException ipe) { 614 } 616 617 try { 619 ConfigurationFormatServiceTest.config.setStringArray(-1, "10"); 620 fail("InvalidParameterException not thrown for invalid index"); 621 } catch(InvalidParameterException ipe) { 622 } 624 try { 625 ConfigurationFormatServiceTest.config.setStringArray(4, "10"); 626 fail("InvalidParameterException not thrown for invalid index"); 627 } catch(InvalidParameterException ipe) { 628 } 630 try { 631 ConfigurationFormatServiceTest.config.getStringArray(-1); 632 fail("InvalidParameterException not thrown for invalid index"); 633 } catch(InvalidParameterException ipe) { 634 } 636 try { 637 ConfigurationFormatServiceTest.config.getStringArray(4); 638 fail("InvalidParameterException not thrown for invalid index"); 639 } catch(InvalidParameterException ipe) { 640 } 642 643 FormatTestConfiguration testConfig = (FormatTestConfiguration) 645 ConfigurationFormatServiceTest.configurationFormatService. 646 newConfiguration(FormatTestConfiguration.class); 647 648 try { 649 ConfigurationFormatServiceTest.config.setMyArray(-1, testConfig); 650 fail("InvalidParameterException not thrown for invalid index"); 651 } catch(InvalidParameterException ipe) { 652 } 654 try { 655 ConfigurationFormatServiceTest.config.setMyArray(4, testConfig); 656 fail("InvalidParameterException not thrown for invalid index"); 657 } catch(InvalidParameterException ipe) { 658 } 660 try { 661 ConfigurationFormatServiceTest.config.getMyArray(-1); 662 fail("InvalidParameterException not thrown for invalid index"); 663 } catch(InvalidParameterException ipe) { 664 } 666 try { 667 ConfigurationFormatServiceTest.config.getMyArray(4); 668 fail("InvalidParameterException not thrown for invalid index"); 669 } catch(InvalidParameterException ipe) { 670 } 672 } 673 674 678 public void testAddArrayItem() { 679 int intOriginalLength = 681 ConfigurationFormatServiceTest.config.getIntArray().length; 682 ConfigurationFormatServiceTest.config.addIntArray(20); 683 int[] intArrayFromConfig = 684 ConfigurationFormatServiceTest.config.getIntArray(); 685 if (intArrayFromConfig.length != intOriginalLength + 1 || 686 intArrayFromConfig[intArrayFromConfig.length - 1] != 20) { 687 688 fail("Array did not change as expected"); 689 } 690 691 int stringOriginalLength = 693 ConfigurationFormatServiceTest.config.getStringArray().length; 694 ConfigurationFormatServiceTest.config.addStringArray("20"); 695 String [] stringArrayFromConfig = 696 ConfigurationFormatServiceTest.config.getStringArray(); 697 if (stringArrayFromConfig.length != stringOriginalLength + 1 || 698 !stringArrayFromConfig[intArrayFromConfig.length - 1].equals("20")) { 699 700 fail("Array did not change as expected"); 701 } 702 703 int configOriginalLength = 705 ConfigurationFormatServiceTest.config.getMyArray().length; 706 707 FormatTestConfiguration testConfig = (FormatTestConfiguration) 708 ConfigurationFormatServiceTest.configurationFormatService. 709 newConfiguration(FormatTestConfiguration.class); 710 711 testConfig.setMyInt(20); 712 ConfigurationFormatServiceTest.config.addMyArray(testConfig); 713 FormatTestConfiguration[] configArrayFromConfig = 714 ConfigurationFormatServiceTest.config.getMyArray(); 715 if (configArrayFromConfig.length != configOriginalLength + 1 || 716 configArrayFromConfig[configArrayFromConfig.length - 1].getMyInt() != 20) { 717 718 fail("Array did not change as expected"); 719 } 720 } 721 722 726 public void testConfigurationWrite() { 727 728 try { 729 Config.getInstance().storeConfiguration( 730 TEST_STORE_LOCATION, 731 ConfigurationFormatServiceTest.config); 732 } catch (Exception e) { 733 e.printStackTrace(); 734 fail ("Unable to serialize configuration document" + e); 735 } 736 737 } 738 739 743 public void testConfigRead() throws Exception { 744 745 FormatTestConfiguration newC = 746 (FormatTestConfiguration) 747 Config.getInstance().fetchConfiguration(TEST_STORE_LOCATION); 748 749 if (newC.getMyFloat() != FLOAT) 750 fail("The document read in contains the wrong float value."); 751 } 752 753 757 public void testFormatServiceConfigAccess() { 758 759 configurationFormatService.alterChildConfiguration( 761 ConfigurationFormatServiceTest.config, 762 "SubConfiguration", 763 null); 764 765 assertNull( 766 "alterChildConfiguration with null new configuration " 767 + "did not clear existing configuration", 768 configurationFormatService.getChildConfiguration( 769 ConfigurationFormatServiceTest.config, 770 "SubConfiguration")); 771 772 Configuration newConfig = 773 configurationFormatService.newConfiguration(Configuration.class); 774 775 String expectedSubName = 776 ConfigurationFormatServiceTest.config.getConfigurationName() 777 + Node.DELIMITER 778 + "SubConfiguration"; 779 780 configurationFormatService.alterChildConfiguration( 782 ConfigurationFormatServiceTest.config, 783 "SubConfiguration", 784 newConfig); 785 786 assertTrue( 787 "Reference to original configuration was not maintained", 788 configurationFormatService.getChildConfiguration( 789 ConfigurationFormatServiceTest.config, 790 "SubConfiguration") 791 .getConfigurationName() 792 .equals(expectedSubName)); 793 } 794 795 798 public void testClone() throws Exception { 799 800 FormatTestConfiguration clone = 801 (FormatTestConfiguration) 802 ConfigurationFormatServiceTest.config.clone(); 803 804 String newMessage = "Hello, You've been cloned!"; 805 clone.setMyString(newMessage); 806 807 819 820 if (!clone.getMyString().equals(newMessage)) { 821 fail("Could not set String property after cloning."); 822 } 823 824 if (ConfigurationFormatServiceTest.config.getMyString().equals(newMessage)) { 825 fail("Setting a String property on the clone altered the origninal."); 826 } 827 } 828 829 830 831 832 public void testEqualsAndHashCode() { 833 Configuration subConfig1 = 834 ConfigurationFormatServiceTest.config.getSubConfiguration(); 835 836 Configuration subConfig2 = 837 ConfigurationFormatServiceTest.config.getSubConfiguration(); 838 839 assertTrue("subConfig1.equals(subConfig2) was not true", 840 subConfig1.equals(subConfig2)); 841 842 assertTrue("subConfig1 had different hashcode from subConfig2", 843 subConfig1.hashCode() == subConfig2.hashCode()); 844 845 assertTrue("subConfig1.equals(ConfigurationFormatServiceTest.config) was not false", 846 !subConfig1.equals(ConfigurationFormatServiceTest.config)); 847 848 assertTrue("subConfig1 had the same hashcode as ConfigurationFormatServiceTest.config", 849 subConfig1.hashCode() != 850 ConfigurationFormatServiceTest.config.hashCode()); 851 852 } 853 854 public void testNonCacheableTypes() { 855 ConfigurationFormatServiceTest.config.setTestComponent( 857 (TestComponent) 858 Lookup.getInstance().fetchComponent("/core/config/test/TestComponent")); 859 860 LifecycleInterceptor lifecycleView = 862 (LifecycleInterceptor) ConfigurationFormatServiceTest.config.getTestComponent(); 863 lifecycleView.destroyComponent(); 864 865 lifecycleView = 868 (LifecycleInterceptor) ConfigurationFormatServiceTest.config.getTestComponent(); 869 870 TestCase.assertTrue( 871 "Config object returned a stale component reference which " + 872 "means it was cached and it should not have been", 873 lifecycleView.getLifecycleState() == LifecycleStateEnum.RUNNING); 874 } 875 876 877 881 public static interface FormatTestConfiguration extends Configuration { 882 883 boolean getMyBool(); 884 void setMyBool(boolean value); 885 886 byte getMyByte(); 887 void setMyByte(byte value); 888 889 short getMyShort(); 890 void setMyShort(short value); 891 892 int getMyInt(); 893 void setMyInt(int value); 894 895 long getMyLong(); 896 void setMyLong(long value); 897 898 float getMyFloat(); 899 void setMyFloat(float value); 900 901 double getMyDouble(); 902 void setMyDouble(double value); 903 904 String getMyString(); 905 void setMyString(String value); 906 907 Object getMyValue(); 908 void setMyValue(Object value); 909 910 Class getMyClass(); 911 void setMyClass(Class value); 912 913 Date getMyDate(); 914 void setMyDate(Date value); 915 916 FormatTestConfiguration[] getMyArray(); 917 FormatTestConfiguration getMyArray(int index); 918 void setMyArray(FormatTestConfiguration[] value); 919 void addMyArray(FormatTestConfiguration value); 920 void setMyArray(int index, FormatTestConfiguration value); 921 922 int[] getIntArray(); 923 int getIntArray(int index); 924 void setIntArray(int[] value); 925 void addIntArray(int value); 926 void setIntArray(int index, int value); 927 928 String [] getStringArray(); 929 String getStringArray(int index); 930 void setStringArray(String [] value); 931 void addStringArray(String value); 932 void setStringArray(int index, String value); 933 934 String [] getEmptyArray(); 935 void setEmptyArray(String [] value); 936 937 SeverityEnum getEnum(); 938 void setEnum(SeverityEnum value); 939 940 String getNullValue(); 941 943 int getNullPrimitive(); 944 945 String DefaultNullValue = null; 946 String getDefaultNullValue(); 947 948 Configuration getSubConfiguration(); 949 void setSubConfiguration(Configuration config); 950 951 Configuration getSubConfigurationRef(); 952 void setSubConfigurationRef(Configuration config); 953 954 TestComponent getTestComponent(); 955 void setTestComponent(TestComponent component); 956 957 } 958 959 public static interface TokenTestConfiguration extends Configuration { 960 int getOne(); 961 String getTwo(); 962 String getThree(); 963 String getFour(); 964 String getFive(); 965 String getSix(); 966 String getSeven(); 967 String getEight(); 968 String getNine(); 969 String getTen(); 970 } 971 972 public static interface ConfigTokenConfiguration extends Configuration { 973 String getOne(); 974 } 975 976 public void testTokenReplacement() { 977 TokenTestConfiguration config = (TokenTestConfiguration) 978 Config.getInstance().fetchConfiguration(TEST_TOKEN_LOCATION); 979 980 PropertyConfiguration propTokensConfig = (PropertyConfiguration) 981 Config.getInstance().fetchConfiguration(PROPERTY_TOKEN_LOCATION); 982 983 ConfigTokenConfiguration configTokensConfig = (ConfigTokenConfiguration) 984 Config.getInstance().fetchConfiguration(CONFIG_TOKEN_LOCATION); 985 986 TestCase.assertTrue("Value was incorrect: [" + config.getOne() + "]", 987 config.getOne() == propTokensConfig.getIntProperty("One")); 988 989 TestCase.assertTrue("Value was incorrect: [" + config.getTwo() + "]", 990 config.getTwo().equals( 991 propTokensConfig.getProperty("One") + ":" + 992 propTokensConfig.getProperty("Two"))); 993 994 TestCase.assertTrue("Value was incorrect: [" + config.getThree() + "]", 995 config.getThree().equals( 996 "{" + propTokensConfig.getProperty("One") + "$\\")); 997 998 TestCase.assertTrue("Value was incorrect: [" + config.getFour() + "]", 999 config.getFour().equals("{}$\\")); 1000 1001 TestCase.assertTrue("Value was incorrect: [" + config.getFive() + "]", 1002 config.getFive().equals("{foo")); 1003 1004 try { 1005 config.getSix(); 1006 TestCase.fail( 1007 "did not catch expected InvalidConfigurationException"); 1008 } catch(InvalidConfigurationException ice) { 1009 } 1011 1012 try { 1013 config.getSeven(); 1014 TestCase.fail( 1015 "did not catch expected InvalidConfigurationException"); 1016 } catch(InvalidConfigurationException ice) { 1017 } 1019 1020 try { 1030 config.getNine(); 1031 TestCase.fail( 1032 "did not catch expected InvalidConfigurationException"); 1033 } catch(InvalidConfigurationException ice) { 1034 } 1036 1037 TestCase.assertTrue("Value was incorrect: [" + config.getTen() + "]", 1038 config.getTen().equals(configTokensConfig.getOne())); 1039 1040 } 1041 1042 1043 public static interface TestDefaultingConfiguration extends Configuration { 1044 1045 int MyInt = 3; 1046 int getMyInt(); 1047 void setMyInt(int value); 1048 1049 double MyDouble = 53.25D; 1050 double getMyDouble(); 1051 void setMyDouble(double value); 1052 1053 1054 } 1055 1056 1057 public void testDefaultingConfig() { 1058 1059 TestDefaultingConfiguration config = 1060 (TestDefaultingConfiguration) 1061 Config.getInstance().createConfiguration(TestDefaultingConfiguration.class); 1062 1063 int myInt = config.getMyInt(); 1064 if (myInt != 3) { 1065 fail("Did not get the expect default MyInt value of " + 3); 1066 } 1067 1068 config.setMyInt(10); 1069 myInt = config.getMyInt(); 1070 if (myInt != 10) { 1071 fail("Did not get the overriden MyInt value of " + 10); 1072 } 1073 1074 double myDouble = config.getMyDouble(); 1075 if (myDouble != 53.25D) { 1076 fail("Did not get the expect default MyDouble value of " + 52.25D); 1077 } 1078 1079 config.setMyDouble(12.12D); 1080 myDouble = config.getMyDouble(); 1081 if (myDouble != 12.12D) { 1082 fail("Did not get the overriden MyDouble value of " + 12.12D); 1083 } 1084 1085 } 1086 1087 1088 public static interface SuperConf extends Configuration { 1089 int MySuperDefaultedInt = 5; 1090 1091 String MySuperDefaultedString = "Foo"; 1092 } 1093 1094 public static interface SubConf extends SuperConf { 1095 int getMySuperDefaultedInt(); 1096 String getMySuperDefaultedString(); 1097 } 1098 1099 1100 public void testSuperInterfaceDefaultedConfigurations() { 1101 1102 SuperConf superConf = 1103 (SuperConf) 1104 Config.getInstance().createConfiguration(SuperConf.class); 1105 1106 SubConf subConf = 1107 (SubConf) 1108 Config.getInstance().createConfiguration(SubConf.class); 1109 1110 if (subConf.getMySuperDefaultedInt() != 5) { 1111 fail("Did not receive the expected default value from the " + 1112 "super interface. Got: " + subConf.getMySuperDefaultedInt()); 1113 } 1114 1115 if (!subConf.getMySuperDefaultedString().equals("Foo")) { 1116 fail("Did not receive the expected default String value from the " + 1117 "super interface. Got: " + subConf.getMySuperDefaultedString()); 1118 } 1119 } 1120 1121 1122} 1123 | Popular Tags |