1 17 18 package org.sape.carbon.services.jndi.test; 19 20 import java.util.HashMap ; 21 import java.util.Hashtable ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 29 import junit.extensions.ActiveTestSuite; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import org.sape.carbon.core.component.Lookup; 35 36 import org.sape.carbon.services.jndi.InitialContextFactory; 37 import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration; 38 39 40 47 public class InitialContextFactoryTest extends TestCase { 48 49 public InitialContextFactoryTest(String name) { 50 super(name); 51 } 52 53 public void testGetInstance() throws NamingException { 54 InitialContextFactory factory = (InitialContextFactory) 55 Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest"); 56 57 InitialContext context = factory.getContext(); 58 59 Hashtable liveEnvironment = context.getEnvironment(); 61 Map configEnvironment = 62 ((InitialContextFactoryConfiguration) factory).getEnvironment(); 63 64 checkResults(configEnvironment, liveEnvironment); 65 } 66 67 public void testGetInstanceWithParams() throws NamingException { 68 InitialContextFactory factory = (InitialContextFactory) 69 Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest"); 70 71 Map customProperties = new HashMap (); 72 customProperties.put("dummy.property", "foo"); 73 74 InitialContext context = factory.getContext(customProperties); 75 76 Hashtable liveEnvironment = context.getEnvironment(); 78 checkResults(customProperties, liveEnvironment); 79 } 80 81 public void testGetInstanceIgnoreConfig() throws NamingException { 82 InitialContextFactory factory = (InitialContextFactory) 83 Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest"); 84 85 Map customProperties = new HashMap ( 86 ((InitialContextFactoryConfiguration) factory).getEnvironment()); 87 customProperties.put("dummy.property", "foo"); 88 89 InitialContext context = factory.getContext(customProperties); 90 91 Hashtable liveEnvironment = context.getEnvironment(); 93 checkResults(customProperties, liveEnvironment); 94 } 95 96 public void testNullParameters() throws NamingException { 97 InitialContextFactory factory = (InitialContextFactory) 98 Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest"); 99 100 InitialContext context = factory.getContext(null); 102 103 Hashtable liveEnvironment = context.getEnvironment(); 105 Map configEnvironment = 106 ((InitialContextFactoryConfiguration) factory).getEnvironment(); 107 108 checkResults(configEnvironment, liveEnvironment); 109 110 context = factory.getContextIgnoreConfig(null); 112 } 113 114 private void checkResults(Map expected, Map actual) { 115 Iterator expectedIterator = expected.entrySet().iterator(); 116 while (expectedIterator.hasNext()) { 117 Map.Entry expectedEntry = (Map.Entry ) expectedIterator.next(); 118 119 assertTrue("unexpected environment property value: property [" + 120 expectedEntry.getKey() + "] expected value [" + 121 expectedEntry.getValue() + "] actual value [" + 122 actual.get(expectedEntry.getKey()) + "]", 123 expectedEntry.getValue().equals(actual.get(expectedEntry.getKey()))); 124 } 125 } 126 127 131 public static Test suite() { 132 TestSuite masterSuite = new TestSuite(); 133 Test singleThreadedTests = getSingleThreadedTests(); 135 if (singleThreadedTests != null) { 136 masterSuite.addTest(singleThreadedTests); 137 } 138 Test multiThreadedTests = getMultiThreadedTests(); 140 if (multiThreadedTests != null) { 141 masterSuite.addTest(multiThreadedTests); 142 } 143 return masterSuite; 144 } 145 146 156 private static Test getSingleThreadedTests() { 157 TestSuite suite = new TestSuite(); 158 159 suite.addTest(new InitialContextFactoryTest("testGetInstance")); 160 suite.addTest(new InitialContextFactoryTest("testGetInstanceWithParams")); 161 suite.addTest(new InitialContextFactoryTest("testGetInstanceIgnoreConfig")); 162 suite.addTest(new InitialContextFactoryTest("testNullParameters")); 163 164 return suite; 165 } 166 167 176 private static Test getMultiThreadedTests() { 177 TestSuite suite = new ActiveTestSuite(); 178 179 return suite; 180 } 181 182 191 private static void addTest( 192 TestSuite suite, 193 String testName, 194 int number) { 195 for (int count = 0; count < number; count++) { 196 suite.addTest(new InitialContextFactoryTest(testName)); 197 } 198 } 199 } | Popular Tags |