[jboss-cvs] JBossAS SVN: r64719 - trunk/testsuite/src/main/org/jboss/test/naming/test.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Aug 20 15:38:46 EDT 2007


Author: bstansberry at jboss.com
Date: 2007-08-20 15:38:45 -0400 (Mon, 20 Aug 2007)
New Revision: 64719

Modified:
   trunk/testsuite/src/main/org/jboss/test/naming/test/NamingRestartUnitTestCase.java
Log:
[JBAS-4615] Add tests for all Context methods

Modified: trunk/testsuite/src/main/org/jboss/test/naming/test/NamingRestartUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/naming/test/NamingRestartUnitTestCase.java	2007-08-20 19:38:26 UTC (rev 64718)
+++ trunk/testsuite/src/main/org/jboss/test/naming/test/NamingRestartUnitTestCase.java	2007-08-20 19:38:45 UTC (rev 64719)
@@ -24,14 +24,18 @@
 
 import java.util.Properties;
 
+import javax.naming.Binding;
 import javax.naming.Context;
 import javax.naming.InitialContext;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 
 import junit.framework.Test;
 
 import org.jboss.test.JBossTestCase;
 import org.jboss.test.naming.restart.ObjectBinder;
+import org.jnp.interfaces.MarshalledValuePair;
 
 /**
  * A NamingRestartUnitTestCase.
@@ -44,8 +48,13 @@
    private static final String namingPort = "19099";
    private static final String haNamingPort = "19100";
  
-   private static boolean deployed = true;
+   private static final String BIND_NAME = "BindName";
+   private static final String BIND_VALUE = "BindValue";
    
+   private static final String SUBCONTEXT_NAME = "RemoteSubcontext";
+   
+   private static boolean deployed = false;
+   
    public NamingRestartUnitTestCase(String name)
    {
       super(name);
@@ -53,45 +62,121 @@
 
    public static Test suite() throws Exception
    {
-      return getDeploySetup(NamingRestartUnitTestCase.class, "naming-restart.sar");
+      return getDeploySetup(NamingRestartUnitTestCase.class, null);
    }
    
-   @Override
    protected void setUp() throws Exception
    {
       super.setUp();
       
       if (!deployed)
          deploy("naming-restart.sar");
+      deployed = true;
    }
    
+   
+   
+   protected void tearDown() throws Exception
+   {
+      if (deployed)
+      {
+         undeploy("naming-restart.sar");
+         deployed = false;
+      }
+      
+      super.tearDown();
+   }
+
+   public void testBind() throws Exception
+   {
+      log.info("Running testBind()");
+      
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         ctx2.bind(BIND_NAME, BIND_VALUE);
+      }
+      catch (NamingException e)
+      {
+         log.error("Caught NamingException", e);
+         fail(e.getMessage());
+      }
+      
+      assertEquals(BIND_VALUE, ctx2.lookup(BIND_NAME));
+      
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+   }
+   
+   public void testCreateSubcontext() throws Exception
+   {
+      log.info("Running testCreateSubcontext()");
+      
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         Context sub = ctx2.createSubcontext(SUBCONTEXT_NAME);
+         sub.bind(BIND_NAME, BIND_VALUE);
+         assertEquals("Proper bind to " + SUBCONTEXT_NAME, BIND_VALUE, sub.lookup(BIND_NAME));
+      }
+      catch (NamingException e)
+      {
+         log.error("Caught NamingException", e);
+         fail(e.getMessage());
+      }
+      
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+   }
+   
    public void testLookupAfterHANamingRestart() throws Exception
    {
-      namingRestartTest(haNamingPort);
+      log.info("Running testLookupAfterHANamingRestart");
+      
+      lookupTest(haNamingPort);
    }
 
    public void testLookupAfterNamingRestart() throws Exception
    {
-      namingRestartTest(namingPort);
+      log.info("Running testLookupAfterNamingRestart");
+      
+      lookupTest(namingPort);
    }
    
-   private void namingRestartTest(String port) throws Exception
+   private void lookupTest(String port) throws Exception
    {
-      String namingURL = getServerHost() + ":" + port;
-      Properties env = new Properties();
-      env.setProperty("java.naming.provider.url", namingURL);
-      env.setProperty("jnp.disableDiscovery", "true");
+      Properties env = createNamingEnvironment(port);
       
       Context ctx1 = new InitialContext(env);
       assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
       
-      // HOLD ONTO REF to ctx1 so the weak ref to it does not get gc'ed from
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
       // static map in org.jnp.interfaces.NamingContext.
       
       // Redeploy the naming service
-      deployed = false;
-      redeploy("naming-restart.sar");
-      deployed = true;
+      redeployNaming();
       
       Context ctx2 = new InitialContext(env);
       NamingException ne = null;
@@ -122,7 +207,235 @@
          fail(ne.getMessage());         
       }
       
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+   }
+   
+   public void testList() throws Exception
+   {
+      log.info("Running testList()");
       
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         NamingEnumeration list = ctx2.list(ObjectBinder.SUBCONTEXT_NAME);
+         assertNotNull("NamingEnumeration returned", list);
+         assertTrue("NamingEnumeration has entry", list.hasMoreElements());
+         NameClassPair pair = (NameClassPair) list.next();
+         assertEquals(ObjectBinder.NAME, pair.getName());
+      }
+      catch (NamingException e)
+      {
+         log.error("Caught NamingException", e);
+         fail(e.getMessage());
+      }
+      
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
    }
+   
+   public void testListBindings() throws Exception
+   {
+      log.info("Running testListBindings()");
+      
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         NamingEnumeration list = ctx2.listBindings(ObjectBinder.SUBCONTEXT_NAME);
+         assertNotNull("NamingEnumeration returned", list);
+         assertTrue("NamingEnumeration has entry", list.hasMoreElements());
+         Binding binding = (Binding) list.next();
+         assertEquals(ObjectBinder.NAME, binding.getName());
+      }
+      catch (NamingException e)
+      {
+         log.error("Caught NamingException", e);
+         fail(e.getMessage());
+      }
+      
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+   }
+   
+   public void testLookupLink() throws Exception
+   {
+      log.info("Running testLookupLink()");
+      
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         Object obj = ctx2.lookupLink(ObjectBinder.NAME);
+         if (obj instanceof MarshalledValuePair)
+            obj = ((MarshalledValuePair) obj).get();
+         assertEquals(ObjectBinder.VALUE, obj);
+      }
+      catch (NamingException e)
+      {         
+         if (e.getCause() instanceof NullPointerException)
+         {
+            log.error("Caught known failure JBAS-4616", e);
+         }
+         else
+         {
+            log.error("Caught NamingException", e);
+            fail(e.getMessage());
+         }
+      }
+      
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+   }
+   
+   public void testRebind() throws Exception
+   {
+      log.info("Running testRebind()");
+      
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         ctx2.rebind(ObjectBinder.NAME, ObjectBinder.VALUE);
+      }
+      catch (NamingException e)
+      {
+         log.error("Caught NamingException", e);
+         fail(e.getMessage());
+      }
+      
+      // Confirm the original context is still good
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+   }
+   
+   public void testUnbind() throws Exception
+   {
+      log.info("Running testUnbind()");
+      
+      Properties env = createNamingEnvironment(namingPort);
+      
+      Context ctx1 = new InitialContext(env);
+      assertEquals(ObjectBinder.VALUE, ctx1.lookup(ObjectBinder.NAME));
+      
+      // HOLD ONTO REF to ctx1 so the ref to it does not get gc'ed from
+      // static map in org.jnp.interfaces.NamingContext.
+      
+      // Redeploy the naming service
+      redeployNaming();
+      
+      Context ctx2 = new InitialContext(env);
+      try
+      {
+         ctx2.unbind(ObjectBinder.NAME);
+      }
+      catch (NamingException e)
+      {
+         log.error("Caught NamingException", e);
+         fail(e.getMessage());
+      }
+      
+      // Confirm the original context is still good
+      ctx1.bind(BIND_NAME, BIND_VALUE);
+   }
+   
+   public void testBadBindingHALookup() throws Exception
+   {
+      log.info("Running testBadBindingHALookup");
+      
+      badBindingLookupTest(haNamingPort);
+   }
 
+   public void testBadBindingLookup() throws Exception
+   {
+      log.info("Running testBadBindingLookup");
+      
+      badBindingLookupTest(namingPort);
+   }
+   
+   /**
+    * Tests a lookup of an object that deliberately throws
+    * java.rmi.NoSuchObjectException when deserialized. Used 
+    * to check that this doesn't confuse the NamingContext.
+    * 
+    * @param port
+    * @throws Exception
+    */
+   private void badBindingLookupTest(String port) throws Exception
+   {
+      Properties env = createNamingEnvironment(port);
+      
+      Context ctx = new InitialContext(env);
+      try
+      {
+         ctx.lookup(ObjectBinder.BAD_BINDING);
+         fail("Did not fail in lookup of " + ObjectBinder.BAD_BINDING);
+      }
+      catch (NamingException good)
+      {
+         log.debug("Caught NamingException as expected: " + 
+                   good.getLocalizedMessage() + " -- cause: " + 
+                   good.getCause());
+      }
+      
+      // We recover from failure
+      assertEquals(ObjectBinder.VALUE, ctx.lookup(ObjectBinder.NAME));
+   }
+
+   private Properties createNamingEnvironment(String port)
+   {
+      String namingURL = getServerHost() + ":" + port;
+      Properties env = new Properties();
+      env.setProperty("java.naming.provider.url", namingURL);
+      env.setProperty("jnp.disableDiscovery", "true");
+      return env;
+   }
+
+   private void redeployNaming() throws Exception
+   {
+      deployed = false;
+      redeploy("naming-restart.sar");
+      deployed = true;
+   }
+
 }




More information about the jboss-cvs-commits mailing list