1 17 18 package org.sape.carbon.core.config.test; 19 20 import org.sape.carbon.core.config.Config; 21 import org.sape.carbon.core.config.Configuration; 22 import org.sape.carbon.core.config.ConfigurationException; 23 import org.sape.carbon.core.config.ConfigurationNotFoundException; 24 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 25 import org.sape.carbon.core.config.node.Node; 26 import org.sape.carbon.core.config.node.event.NodeEventListener; 27 import org.sape.carbon.core.exception.InvalidParameterException; 28 29 import junit.extensions.ActiveTestSuite; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 42 public class DefaultConfigurationServiceTest 43 extends TestCase 44 implements NodeEventListener { 45 46 private static final String TEST_VALUE = "hello mom"; 47 private static final String TEST_CONFIG_NAME = "/fee/fi/fo/fum"; 48 private static final String BAD_CONFIG_NAME_1 = "/fee/fi/fo/fum/foo"; 49 private static final String BAD_CONFIG_NAME_2 = "/fee/fi/fo/fummmm"; 50 private static final String TEST_CONFIG_ROOT = "fee"; 51 private static final String BAD_NAME = "fee/fi"; 52 53 private int nodeChangedCalled = 0; 54 private int nodeRemovedCalled = 0; 55 56 public DefaultConfigurationServiceTest(String name) { 57 super(name); 58 } 59 60 public void testStoreConfiguration() throws ConfigurationException { 61 62 DefaultConfigurationFormatService formatter = 63 new DefaultConfigurationFormatService(); 64 65 TestConfiguration config = 66 (TestConfiguration) formatter.newConfiguration( 67 TestConfiguration.class); 68 69 config.setTestValue(TEST_VALUE); 70 71 Config.getInstance().storeConfiguration(TEST_CONFIG_NAME, config); 72 73 } 74 75 public void testFetchConfiguration() { 76 TestConfiguration config = 77 (TestConfiguration) Config.getInstance().fetchConfiguration( 78 TEST_CONFIG_NAME); 79 80 if (!config.getTestValue().equals(TEST_VALUE)) { 81 TestCase.fail( 82 "Configuration value was not as expected: " 83 + "Actual - [" 84 + config.getTestValue() 85 + "] " 86 + "Expected - [" 87 + TEST_VALUE 88 + "]"); 89 } 90 } 91 92 public void testNodeExists() { 93 if (!Config.getInstance().nodeExists(TEST_CONFIG_NAME)) { 94 fail("Configuration " + TEST_CONFIG_NAME + " was not detected " + 95 "by nodeExists check."); 96 } 97 98 if (Config.getInstance().nodeExists(BAD_CONFIG_NAME_1)) { 99 fail("Configuration " + BAD_CONFIG_NAME_1 + " was detected " + 100 "as existing by nodeExists check, when it does not exist."); 101 } 102 } 103 104 public void testNodeNotFound() { 105 try { 106 Configuration config = 107 Config.getInstance().fetchConfiguration(BAD_CONFIG_NAME_1); 108 109 TestCase.fail( 110 "Did not catch " 111 + "ConfigurationNotFoundException as expected"); 112 } catch (ConfigurationNotFoundException ipe) { 113 } 115 116 try { 117 Configuration config = 118 Config.getInstance().fetchConfiguration(BAD_CONFIG_NAME_2); 119 120 TestCase.fail( 121 "Did not catch " 122 + "ConfigurationNotFoundException as expected"); 123 } catch (ConfigurationNotFoundException cnfe) { 124 } 126 } 127 128 131 public void testFetchRootNode() throws Exception { 132 Node root = Config.getInstance().fetchNode(Config.ROOT_NODE_NAME); 133 134 Node testRoot = root.fetchChild(TEST_CONFIG_ROOT); 135 136 testRoot.remove(); 137 138 } 139 140 public void testBadNodeName() { 141 try { 142 Config.getInstance().fetchConfiguration(BAD_NAME); 143 TestCase.fail( 144 "Did not catch " + "InvalidParameterException as expected"); 145 } catch (InvalidParameterException ipe) { 146 } 148 149 try { 150 Config.getInstance().fetchConfiguration(null); 151 TestCase.fail( 152 "Did not catch " + "NullPointerException as expected"); 153 } catch (NullPointerException npe) { 154 } 156 } 157 158 public void testNodeEvents() throws ConfigurationException { 159 TestConfiguration config = 160 (TestConfiguration) Config 161 .getInstance() 162 .getConfigurationService() 163 .fetchWritableConfiguration(TEST_CONFIG_NAME); 164 165 TestConfiguration subConfig = 166 (TestConfiguration) Config 167 .getInstance() 168 .getConfigurationService() 169 .createConfiguration(TestConfiguration.class); 170 171 config.setSubConfig(subConfig); 172 Config.getInstance().storeConfiguration(TEST_CONFIG_NAME, config); 173 174 Config.getInstance().addNodeListener( 175 TEST_CONFIG_NAME, 176 this); 177 Config.getInstance().addNodeListener( 178 TEST_CONFIG_NAME + Node.DELIMITER + "SubConfig", 179 this); 180 181 testStoreConfiguration(); 182 183 assertTrue( 184 "Listener was not notified of node change", 185 this.nodeChangedCalled == 2); 186 187 Config.getInstance().fetchNode(TEST_CONFIG_NAME).refresh(); 188 189 assertTrue( 190 "Listener was not notified of node change on refresh", 191 this.nodeChangedCalled == 3); 192 193 Config.getInstance().fetchNode(TEST_CONFIG_NAME).remove(); 194 195 assertTrue( 196 "Listener was not notified of node removal", 197 this.nodeRemovedCalled == 1); 198 199 } 200 201 public void nodeChanged(Node changedNode) { 202 this.nodeChangedCalled++; 203 } 204 205 208 public void nodeRemoved(String removedNodeName) { 209 this.nodeRemovedCalled++; 210 } 211 212 216 public static Test suite() { 217 TestSuite masterSuite = new TestSuite(); 218 Test singleThreadedTests = getSingleThreadedTests(); 220 if (singleThreadedTests != null) { 221 masterSuite.addTest(singleThreadedTests); 222 } 223 Test multiThreadedTests = getMultiThreadedTests(); 225 if (multiThreadedTests != null) { 226 masterSuite.addTest(multiThreadedTests); 227 } 228 return masterSuite; 229 } 230 231 237 private static Test getSingleThreadedTests() { 238 TestSuite suite = new TestSuite(); 239 240 suite.addTest( 241 new DefaultConfigurationServiceTest("testStoreConfiguration")); 242 suite.addTest( 243 new DefaultConfigurationServiceTest("testFetchConfiguration")); 244 suite.addTest( 245 new DefaultConfigurationServiceTest("testNodeExists")); 246 suite.addTest(new DefaultConfigurationServiceTest("testNodeEvents")); 247 suite.addTest(new DefaultConfigurationServiceTest("testNodeNotFound")); 248 suite.addTest(new DefaultConfigurationServiceTest("testFetchRootNode")); 249 suite.addTest(new DefaultConfigurationServiceTest("testBadNodeName")); 250 251 return suite; 252 } 253 254 259 private static Test getMultiThreadedTests() { 260 TestSuite suite = new ActiveTestSuite(); 261 262 268 269 return suite; 270 } 271 272 280 private static void addTest(TestSuite suite, String testName, int number) { 281 for (int count = 0; count < number; count++) { 282 suite.addTest(new DefaultConfigurationServiceTest(testName)); 283 } 284 } 285 286 } 287 | Popular Tags |