1 17 18 package org.sape.carbon.services.config.jndi.test; 19 20 import org.sape.carbon.core.bootstrap.BootStrapper; 21 import org.sape.carbon.core.config.Config; 22 import org.sape.carbon.core.config.ConfigurationException; 23 import org.sape.carbon.core.config.PropertyConfiguration; 24 import org.sape.carbon.core.config.node.ConfigurationDocument; 25 import org.sape.carbon.core.config.node.Folder; 26 import org.sape.carbon.core.config.node.Node; 27 import org.sape.carbon.core.config.node.event.NodeEventListener; 28 import org.sape.carbon.core.config.test.InternalNodeTest; 29 import org.sape.carbon.core.config.test.TestConfiguration; 30 31 import junit.extensions.ActiveTestSuite; 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 47 public class InternalJNDINodeTest 48 extends InternalNodeTest 49 implements NodeEventListener { 50 51 private static final String JNDI_NODE_NAME = 52 "/jndi_config/test/JNDINodeTest"; 53 private static final String THREAD_TEST_NODE_NAME1 = "TestNode1"; 54 private static final String THREAD_TEST_NODE_NAME2 = "TestNode2"; 55 private static final String THREAD_LINK_TEST_NODE_NAME = "TestNode3"; 56 private static final String LINK_TARGET_NAME = "TestNode3Target"; 57 private static final String JNDI_EVENT_TEST_NODE_NAME = "JNDIEventTest"; 58 private static final String JNDI_EVENT_TEST_LINK_NAME = 59 "/jndi_config/test/JNDINodeEventTest"; 60 61 private Log log = LogFactory.getLog(this.getClass()); 62 63 private boolean nodeChangedCalled = false; 64 private boolean nodeRemovedCalled = false; 65 66 70 public InternalJNDINodeTest(String name) { 71 super(name); 72 } 73 74 77 protected void setUp() throws Exception { 78 super.root = 79 (Folder) Config.getInstance().fetchNode( 80 InternalJNDINodeTest.JNDI_NODE_NAME); 81 } 82 83 public void testThreadedRelativeLink() { 84 try { 85 testSetAndGetAttribute( 86 (ConfigurationDocument) super.root.fetchChild( 87 THREAD_LINK_TEST_NODE_NAME)); 88 } catch (ConfigurationException ce) { 89 fail("caught ConfigurationException: " + ce); 90 } 91 } 92 93 public void testThreadedAccess1() { 94 try { 95 testSetAndGetAttribute( 96 (ConfigurationDocument) super.root.fetchChild( 97 THREAD_TEST_NODE_NAME1)); 98 } catch (ConfigurationException ce) { 99 fail("caught ConfigurationException: " + ce); 100 } 101 } 102 103 public void testThreadedAccess2() { 104 try { 105 testSetAndGetAttribute( 106 (ConfigurationDocument) super.root.fetchChild( 107 THREAD_TEST_NODE_NAME2)); 108 } catch (ConfigurationException ce) { 109 fail("caught ConfigurationException: " + ce); 110 } 111 } 112 113 public void testJNDIEvents() throws Exception { 114 115 if ("true".equals(BootStrapper.getInstance().getDeploymentProperty( 116 "test.config-jndi.EventsEnabled"))) { 117 118 try { 119 TestConfiguration testConfig = 120 (TestConfiguration) Config 121 .getInstance() 122 .createConfiguration( 123 TestConfiguration.class); 124 125 ConfigurationDocument eventTestDoc = 126 this.root.addConfigurationDocument( 127 JNDI_EVENT_TEST_NODE_NAME, 128 testConfig); 129 eventTestDoc.addNodeListener(this); 130 131 Node parallelRootNode = 132 Config.getInstance().fetchNode(JNDI_EVENT_TEST_LINK_NAME); 133 134 ConfigurationDocument parallelEventTestDoc = 135 (ConfigurationDocument) parallelRootNode.fetchChild( 136 JNDI_EVENT_TEST_NODE_NAME); 137 138 testConfig.setTestValue("foo"); 139 140 parallelEventTestDoc.writeConfiguration(testConfig); 141 142 synchronized (this) { 143 if (!this.nodeChangedCalled) { 144 wait(1000); 145 } 146 TestCase.assertTrue( 147 "Node either did not receive a JNDI event or " 148 + "was not refreshed due to one.", 149 this.nodeChangedCalled); 150 } 151 152 parallelEventTestDoc.remove(); 153 154 synchronized (this) { 155 if (!this.nodeRemovedCalled) { 156 wait(1000); 157 } 158 TestCase.assertTrue( 159 "Node either did not receive a JNDI event or " 160 + "was not refreshed due to one.", 161 this.nodeRemovedCalled); 162 } 163 } finally { 164 if (this.root.containsChild(JNDI_EVENT_TEST_NODE_NAME)) { 165 this.root.fetchChild(JNDI_EVENT_TEST_NODE_NAME).remove(); 166 } 167 } 168 } else { 169 log.info("JNDI events are disabled so they will not be tested"); 170 } 171 } 172 173 private void testSetAndGetAttribute(ConfigurationDocument doc) 174 throws ConfigurationException { 175 176 for (int i = 0; i < 5; i++) { 177 TestJNDIConfiguration config = 178 (TestJNDIConfiguration) doc.readConfiguration(); 179 180 int newValue = config.getValue(); 181 config.setValue(newValue); 182 doc.writeConfiguration(config); 183 config = (TestJNDIConfiguration) doc.readConfiguration(); 184 185 if (config.getValue() != newValue) { 186 fail( 187 "value within config was not the expected value, expected [" 188 + newValue 189 + "], actual [" 190 + config.getValue() 191 + "]"); 192 } 193 } 194 } 195 196 200 public static Test suite() { 201 TestSuite masterSuite = new TestSuite(); 202 Test singleThreadedTests = getSingleThreadedTests(); 204 if (singleThreadedTests != null) { 205 masterSuite.addTest(singleThreadedTests); 206 } 207 Test multiThreadedTests = getMultiThreadedTests(); 209 if (multiThreadedTests != null) { 210 masterSuite.addTest(multiThreadedTests); 211 } 212 return masterSuite; 213 } 214 215 221 private static Test getSingleThreadedTests() { 222 TestSuite suite = new TestSuite(); 223 224 suite.addTest(new InternalJNDINodeTest("testAddSubFolder")); 225 suite.addTest(new InternalJNDINodeTest("testAddConfigurationDocument")); 226 suite.addTest(new InternalJNDINodeTest("testWriteConfiguration")); 227 suite.addTest(new InternalJNDINodeTest("testReadConfiguration")); 228 suite.addTest(new InternalJNDINodeTest("testNodeAttributes")); 229 suite.addTest(new InternalJNDINodeTest("testFetchChildren")); 230 suite.addTest( 231 new InternalJNDINodeTest("testAddDuplicateConfigurationDocument")); 232 suite.addTest(new InternalJNDINodeTest("testAddDuplicateSubFolder")); 233 suite.addTest(new InternalJNDINodeTest("testAddNestedConfiguration")); 234 suite.addTest(new InternalJNDINodeTest("testReadNestedConfiguration")); 235 suite.addTest( 236 new InternalJNDINodeTest("testRemoveNestedConfiguration")); 237 suite.addTest(new InternalJNDINodeTest("testNodeRemoval")); 238 suite.addTest(new InternalJNDINodeTest("testFetchRemovedNode")); 239 suite.addTest(new InternalJNDINodeTest("testNullParameters")); 240 suite.addTest(new InternalJNDINodeTest("testLinkFolder")); 241 suite.addTest(new InternalJNDINodeTest("testLinkDoc")); 242 suite.addTest(new InternalJNDINodeTest("testJNDIEvents")); 243 244 return suite; 245 } 246 247 252 private static Test getMultiThreadedTests() { 253 TestSuite suite = new ActiveTestSuite(); 254 255 261 addTest(suite, "testThreadedRelativeLink", 1); 262 addTest(suite, "testThreadedAccess1", 1); 263 addTest(suite, "testThreadedAccess2", 1); 264 265 return suite; 266 } 267 268 276 private static void addTest(TestSuite suite, String testName, int number) { 277 for (int count = 0; count < number; count++) { 278 suite.addTest(new InternalJNDINodeTest(testName)); 279 } 280 } 281 284 protected String getTestNodeAttributesAbsoluteName() { 285 return JNDI_NODE_NAME + super.getTestNodeAttributesAbsoluteName(); 286 } 287 288 291 public synchronized void nodeChanged(Node changedNode) { 292 this.nodeChangedCalled = true; 293 notify(); 294 } 295 296 299 public synchronized void nodeRemoved(String removedNodeName) { 300 this.nodeRemovedCalled = true; 301 notify(); 302 } 303 304 } 305 | Popular Tags |