package org.huihoo.jfox.jndi.test;

import java.io.Serializable;
import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;

import junit.framework.TestCase;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ContextTest extends TestCase {
  private static Context ctx;

  protected void setUp() throws Exception {
//    System.setProperty("java.naming.factory.initial", JNDIProperties.INITIAL_CONTEXT_FACTORY);
//    System.setProperty(Context.OBJECT_FACTORIES, JNDIProperties.INITIAL_CONTEXT_FACTORY);
//    System.setProperty(Context.URL_PKG_PREFIXES, JNDIProperties.URL_PKG_PREFIXES);
//    System.setProperty("java.naming.provider.url", JNDIProperties.PROVIDER_URL);
    ctx = new InitialContext();

  }

  public void testSimple() {
    try {
      ctx.rebind("a",new Test("Hello"));
      Test test = lookupTest("/a");
      lookupTest("java:/a");
      lookupTest("java://localhost/a");
      lookupTest("java://localhost:1099/a");
      assertTrue(test.get().equals("Hello"));
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }

  }

  public static Test lookupTest(String name) throws Exception {
    return (Test)javax.rmi.PortableRemoteObject.narrow(ctx.lookup(name),Test.class);
  }

  public static void main(String[] args) {

  }
}

class Test implements Serializable{
  private String word = "Hello,World!";

  public Test(String word) {
    this.word = word;
  }

    public String get() throws RemoteException {
        return word;
    }
}