1 22 package org.jboss.test.naming.test; 23 24 import java.util.Hashtable ; 25 import javax.naming.RefAddr ; 26 import javax.naming.StringRefAddr ; 27 import javax.naming.Reference ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.naming.spi.InitialContextFactoryBuilder ; 32 import javax.naming.spi.InitialContextFactory ; 33 import javax.naming.spi.NamingManager ; 34 35 import junit.extensions.TestSetup; 36 import junit.framework.Test; 37 38 import org.jboss.naming.ENCFactory; 39 import org.jnp.server.Main; 40 import org.jnp.interfaces.NamingContext; 41 42 46 public class NamingServerSetup extends TestSetup 47 implements InitialContextFactoryBuilder 48 { 49 private Main namingServer; 50 51 public NamingServerSetup(Test test) 52 { 53 super(test); 54 } 55 56 public InitialContextFactory createInitialContextFactory(Hashtable environment) 57 throws NamingException 58 { 59 return new InVMInitialContextFactory(); 60 } 61 62 protected void setUp() throws Exception 63 { 64 super.setUp(); 65 namingServer = new Main(); 66 namingServer.setPort(10099); 67 namingServer.start(); 68 69 NamingManager.setInitialContextFactoryBuilder(this); 70 74 InitialContext iniCtx = new InitialContext (); 75 ClassLoader topLoader = Thread.currentThread().getContextClassLoader(); 76 ENCFactory.setTopClassLoader(topLoader); 77 RefAddr refAddr = new StringRefAddr ("nns", "ENC"); 78 Reference envRef = new Reference ("javax.naming.Context", refAddr, ENCFactory.class.getName(), null); 79 Context ctx = (Context )iniCtx.lookup("java:"); 80 ctx.rebind("comp", envRef); 81 } 82 83 protected void tearDown() throws Exception 84 { 85 namingServer.stop(); 86 super.tearDown(); 87 } 88 89 static class InVMInitialContextFactory implements InitialContextFactory 90 { 91 public Context getInitialContext(Hashtable env) 92 throws NamingException 93 { 94 Hashtable env2 = (Hashtable ) env.clone(); 95 env2.remove(Context.PROVIDER_URL); 96 return new NamingContext(env2, null, null); 97 } 98 } 99 } 100 | Popular Tags |