1 22 package org.jboss.test.naming.test; 23 24 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.util.Properties ; 28 import java.util.Hashtable ; 29 import java.io.Serializable ; 30 import java.io.IOException ; 31 import java.io.ObjectOutputStream ; 32 import java.io.ObjectInputStream ; 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NameAlreadyBoundException ; 36 import javax.naming.NamingException ; 37 import javax.naming.Name ; 38 import javax.naming.spi.StateFactory ; 39 import javax.naming.spi.ObjectFactory ; 40 41 import junit.framework.TestCase; 42 import junit.framework.Test; 43 import junit.framework.TestSuite; 44 import junit.textui.TestRunner; 45 46 import org.jboss.logging.Logger; 47 48 53 public class ImplUnitTestCase extends TestCase 54 { 55 static final Logger log = Logger.getLogger(ImplUnitTestCase.class); 56 57 58 63 public ImplUnitTestCase(String name) 64 { 65 super(name); 66 } 67 68 73 public void testCreateSubcontext() throws Exception 74 { 75 log.debug("+++ testCreateSubcontext"); 76 InitialContext ctx = getInitialContext(); 77 ctx.createSubcontext("foo"); 78 try 79 { 80 ctx.createSubcontext("foo"); 81 fail("Second createSubcontext(foo) did NOT fail"); 82 } 83 catch (NameAlreadyBoundException e) 84 { 85 log.debug("Second createSubcontext(foo) failed as expected"); 86 } 87 ctx.createSubcontext("foo/bar"); 88 ctx.unbind("foo/bar"); 89 ctx.unbind("foo"); 90 } 91 92 96 public void testLookup() throws Exception 97 { 98 log.debug("+++ testLookup"); 99 InitialContext ctx = getInitialContext(); 100 Object obj = ctx.lookup(""); 101 log.debug("lookup('') = "+obj); 102 } 103 104 public void testEncPerf() throws Exception 105 { 106 int count = Integer.getInteger("jbosstest.threadcount", 10).intValue(); 107 int iterations = Integer.getInteger("jbosstest.iterationcount", 1000).intValue(); 108 log.info("Creating "+count+"threads doing "+iterations+" iterations"); 109 InitialContext ctx = getInitialContext(); 110 URL [] empty = {}; 111 Thread [] testThreads = new Thread [count]; 112 for(int t = 0; t < count; t ++) 113 { 114 ClassLoader encLoader = URLClassLoader.newInstance(empty); 115 Thread.currentThread().setContextClassLoader(encLoader); 116 Runnable test = new ENCTester(ctx, iterations); 117 Thread thr = new Thread (test, "Tester#"+t); 118 thr.setContextClassLoader(encLoader); 119 thr.start(); 120 testThreads[t] = thr; 121 } 122 123 for(int t = 0; t < count; t ++) 124 { 125 Thread thr = testThreads[t]; 126 thr.join(); 127 } 128 } 129 130 134 public void testFactorySupport() throws NamingException 135 { 136 log.info("+++ testFactorySupport"); 137 NotSerializableObject nso = new NotSerializableObject( "nsc" ); 138 Context ctx = getInitialContext(); 139 140 try 141 { 142 ctx.bind("test", nso); 143 fail(); 144 } 145 catch( NamingException ex ) 146 { 147 log.debug("bind failed as expected", ex); 148 } 149 150 Properties env = new Properties (); 151 env.setProperty(Context.STATE_FACTORIES, TestFactory.class.getName()); 152 env.setProperty(Context.OBJECT_FACTORIES, TestFactory.class.getName()); 153 ctx = new InitialContext (env); 154 155 log.debug("Retest with TestFactory enabled"); 156 ctx.bind("test", nso); 157 158 Object boundObject = ctx.lookup( "test" ); 159 assertNotNull( boundObject ); 160 NotSerializableObject nso2 = (NotSerializableObject) boundObject; 162 assertEquals( nso.getId(), nso2.getId() ); 163 } 164 165 static InitialContext getInitialContext() throws NamingException 166 { 167 InitialContext ctx = new InitialContext (); 168 return ctx; 169 } 170 171 private static class ENCTester implements Runnable 172 { 173 Context enc; 174 int iterations; 175 176 ENCTester(InitialContext ctx, int iterations) throws Exception 177 { 178 log.info("CL: "+Thread.currentThread().getContextClassLoader()); 179 this.iterations = iterations; 180 enc = (Context ) ctx.lookup("java:comp"); 181 enc = enc.createSubcontext("env"); 182 enc.bind("int", new Integer (1)); 183 enc.bind("double", new Double (1.234)); 184 enc.bind("string", "str"); 185 enc.bind("url", new URL ("http://www.jboss.org")); 186 } 187 188 public void run() 189 { 190 try 191 { 192 InitialContext ctx = new InitialContext (); 193 for(int i = 0; i < iterations; i ++) 194 { 195 Integer i1 = (Integer ) enc.lookup("int"); 196 log.debug("int: "+i1); 197 i1 = (Integer ) ctx.lookup("java:comp/env/int"); 198 log.debug("java:comp/env/int: "+i1); 199 Double d = (Double ) enc.lookup("double"); 200 log.debug("double: "+d); 201 d = (Double ) ctx.lookup("java:comp/env/double"); 202 log.debug("java:comp/env/double: "+d); 203 String s = (String ) enc.lookup("string"); 204 log.debug("string: "+s); 205 s = (String ) ctx.lookup("java:comp/env/string"); 206 log.debug("java:comp/env/string: "+s); 207 URL u = (URL ) enc.lookup("url"); 208 log.debug("url: "+u); 209 u = (URL ) ctx.lookup("java:comp/env/url"); 210 log.debug("java:comp/env/url: "+u); 211 } 212 } 213 catch(Exception e) 214 { 215 e.printStackTrace(); 216 } 217 } 218 } 219 220 private static class NotSerializableObject 221 { 222 protected String id; 223 224 public NotSerializableObject() {} 225 226 public NotSerializableObject( String id ) 227 { 228 this.id = id; 229 } 230 231 public String getId() 232 { 233 return id; 234 } 235 236 public String toString() 237 { 238 return "NotSerializableObject<" + getId() + ">"; 239 } 240 } 241 242 private static class SerializableObject extends NotSerializableObject 243 implements Serializable 244 { 245 private static long serialVersionUID = 1; 246 247 public SerializableObject () {} 248 249 public SerializableObject (String id) 250 { 251 super( id ); 252 } 253 254 public String toString() 255 { 256 return "SerializableObject<" + getId() + ">"; 257 } 258 259 private void writeObject(ObjectOutputStream out) 260 throws IOException 261 { 262 out.writeObject(getId()); 263 } 264 private void readObject(ObjectInputStream in) 265 throws IOException , ClassNotFoundException 266 { 267 id = (String ) in.readObject(); 268 } 269 270 } 271 272 273 public static class TestFactory implements StateFactory , ObjectFactory 274 { 275 public Object getStateToBind (Object obj, Name name, Context nameCtx, 276 Hashtable environment) throws NamingException 277 { 278 if( obj instanceof NotSerializableObject ) 279 { 280 String id = ((NotSerializableObject) obj).getId(); 281 return new SerializableObject( id ); 282 } 283 284 return null; 285 } 286 287 public Object getObjectInstance (Object obj, Name name, Context nameCtx, 288 Hashtable env) throws Exception 289 { 290 log.debug("getObjectInstance, obj:" + obj + ", name: " + name 291 + ", nameCtx: " + nameCtx +", env: "+env); 292 if( obj instanceof SerializableObject ) 293 { 294 String id = ((SerializableObject) obj).getId(); 295 return new NotSerializableObject( id ); 296 } 297 298 return null; 299 } 300 } 301 302 public static Test suite() 303 { 304 TestSuite suite = new TestSuite(); 305 suite.addTest(new TestSuite(ImplUnitTestCase.class)); 306 307 NamingServerSetup wrapper = new NamingServerSetup(suite); 309 return wrapper; 310 } 311 312 316 public static void main(String [] args) 317 { 318 TestRunner.run(ImplUnitTestCase.suite()); 319 } 320 } 321 | Popular Tags |