/* * Created on Dec 15, 2004 * @author mike */ package org.jnp.test; import org.jnp.server.Main; import org.jnp.test.TestJNPSockets.ClientSocketFactory; import org.jnp.test.TestJNPSockets.ServerSocketFactory; import java.io.IOException; import java.io.Serializable; import java.util.Hashtable; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.Name; import javax.naming.NamingException; import javax.naming.spi.ObjectFactory; import javax.naming.spi.StateFactory; import junit.framework.TestCase; public class TestNamingContext extends TestCase { static Main server; static int serverPort; public TestNamingContext (String name) { super( name ); } protected void setUp() throws Exception { if( server != null ) return; server = new Main(); server.setPort(0); server.setBindAddress("localhost"); server.setClientSocketFactory(ClientSocketFactory.class.getName()); server.setServerSocketFactory(ServerSocketFactory.class.getName()); server.start(); serverPort = server.getPort(); } public void testFactorySupport() throws NamingException { Properties env = new Properties(); env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); env.setProperty(Context.PROVIDER_URL, "localhost:"+serverPort); env.setProperty(Context.URL_PKG_PREFIXES, "org.jnp.interfaces"); NotSerializableObject nso = new NotSerializableObject( "nsc" ); Context ctx = new InitialContext(env); try { ctx.bind("test", nso); fail(); } catch( NamingException ex ) {} env.setProperty(Context.STATE_FACTORIES, TestFactory.class.getName()); env.setProperty(Context.OBJECT_FACTORIES, TestFactory.class.getName()); ctx = new InitialContext(env); ctx.bind("test", nso); Object boundObject = ctx.lookup( "test" ); assertNotNull( boundObject ); // make sure it's not of type SerializableObject assertEquals( NotSerializableObject.class, boundObject.getClass() ); assertEquals( nso.getId(), ((NotSerializableObject) boundObject).getId() ); } private static class NotSerializableObject { protected String id; public NotSerializableObject() {} public NotSerializableObject( String id ) { this.id = id; } public String getId() { return id; } public String toString() { return "NotSerializableObject<" + getId() + ">"; } } private static class SerializableObject extends NotSerializableObject implements Serializable { public SerializableObject () {} public SerializableObject (String id) { super( id ); } public String toString() { return "SerializableObject<" + getId() + ">"; } private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.writeObject(getId()); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { id = (String) in.readObject(); } } public static class TestFactory implements StateFactory, ObjectFactory { public Object getStateToBind (Object obj, Name name, Context nameCtx, Hashtable environment) throws NamingException { if( obj instanceof NotSerializableObject ) { String id = ((NotSerializableObject) obj).getId(); return new SerializableObject( id ); } return null; } public Object getObjectInstance (Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { System.out.println("GET INSTANCE: " + obj ); System.out.println("NAME: " + name ); System.out.println("CTX: " + nameCtx ); if( obj instanceof SerializableObject ) { String id = ((SerializableObject) obj).getId(); return new NotSerializableObject( id ); } return null; } } }