geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject cvs commit: incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/jmx ContextTest.java
Date Mon, 12 Jan 2004 06:19:53 GMT
djencks     2004/01/11 22:19:53

  Modified:    modules/core/src/test/org/apache/geronimo/naming/java
                        BasicContextTest.java ContextBuilderTest.java
                        ThreadContextTest.java
               modules/core/src/java/org/apache/geronimo/naming/java
                        ComponentContextBuilder.java ReadOnlyContext.java
               modules/core/src/test/org/apache/geronimo/naming/jmx
                        ContextTest.java
  Added:       modules/core/src/test/org/apache/geronimo/naming/java
                        AbstractContextTest.java
  Log:
  create all subcontexts when binding a compound name.  Requires reworking binding scheme
  
  Revision  Changes    Path
  1.7       +16 -35    incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/BasicContextTest.java
  
  Index: BasicContextTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/BasicContextTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BasicContextTest.java	19 Oct 2003 01:56:15 -0000	1.6
  +++ BasicContextTest.java	12 Jan 2004 06:19:52 -0000	1.7
  @@ -58,9 +58,7 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Map;
  -import java.util.Properties;
   import java.util.NoSuchElementException;
  -import java.util.Hashtable;
   
   import javax.naming.Binding;
   import javax.naming.CompositeName;
  @@ -70,24 +68,15 @@
   import javax.naming.NameClassPair;
   import javax.naming.NamingEnumeration;
   import javax.naming.NamingException;
  -import javax.naming.LinkRef;
   
   import org.apache.geronimo.common.StopWatch;
   
  -import junit.framework.TestCase;
  -
   /**
   * Unit tests for basic ops on an {@link InitialContext}.
    *
    * @version $Revision$ $Date$
    */
  -public class BasicContextTest extends TestCase {
  -    private Properties syntax;
  -    private Map compBinding;
  -    private Map envBinding;
  -    private Context initialContext;
  -    private Context compContext;
  -    private Context envContext;
  +public class BasicContextTest extends AbstractContextTest {
   
       public void testInitialContext() throws NamingException {
           assertEquals("Hello", initialContext.lookup("java:comp/env/hello"));
  @@ -111,6 +100,13 @@
           assertEquals(envContext, envContext.lookup(""));
       }
   
  +    public void testSubContext() throws NamingException {
  +        assertEquals("long name", initialContext.lookup("java:comp/env/here/there/anywhere"));
  +        Context intermediate = (Context)initialContext.lookup("java:comp/env/here/there");
  +        assertNotNull(intermediate);
  +        assertEquals("long name", intermediate.lookup("anywhere"));
  +    }
  +
       public void testSchemeLookup() throws NamingException {
   //        envContext.lookup("dns:apache.org");
           assertEquals("Hello", envContext.lookup("java:comp/env/hello"));
  @@ -162,14 +158,17 @@
   
       public void testListBindings() throws NamingException {
           NamingEnumeration enum;
  -        Map result;
           enum = envContext.listBindings("");
  -        result = new HashMap();
  +        int count = 0;
           while (enum.hasMore()) {
  +            count ++;
               Binding pair = (Binding) enum.next();
  -            result.put(pair.getName(), pair.getObject());
  +            assertTrue(envBinding.containsKey(pair.getName()));
  +            if (! (envBinding.get(pair.getName()) instanceof ReadOnlyContext)) {
  +                assertEquals(pair.getObject(), envBinding.get(pair.getName()));
  +            }
           }
  -        assertEquals(envBinding, result);
  +        assertEquals(envBinding.size(), count);
   
           try {
               enum.next();
  @@ -199,22 +198,4 @@
           System.out.println("lookup(String): " + watch.toDuration());
       }
   
  -    protected void setUp() throws Exception {
  -        super.setUp();
  -        initialContext = new InitialContext();
  -
  -        compBinding = new HashMap();
  -
  -        envBinding = new HashMap();
  -        envBinding.put("hello", "Hello");
  -        envBinding.put("world", "Hello World");
  -        envBinding.put("link", new LinkRef("java:comp/env/hello"));
  -        compBinding.put("env", new ReadOnlyContext(envBinding));
  -        RootContext.setComponentContext(new ReadOnlyContext(compBinding));
  -
  -        compContext = (Context) initialContext.lookup("java:comp");
  -        envContext = (Context) initialContext.lookup("java:comp/env");
  -
  -        syntax = new Properties();
  -    }
   }
  
  
  
  1.12      +10 -2     incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java
  
  Index: ContextBuilderTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/ContextBuilderTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ContextBuilderTest.java	30 Dec 2003 08:28:57 -0000	1.11
  +++ ContextBuilderTest.java	12 Jan 2004 06:19:52 -0000	1.12
  @@ -110,6 +110,10 @@
           intEntry.setEnvEntryName("int");
           intEntry.setEnvEntryType("java.lang.Integer");
           intEntry.setEnvEntryValue("12345");
  +        EnvEntry compoundEntry = new EnvEntry();
  +        compoundEntry.setEnvEntryName("this/is/a/compound/name");
  +        compoundEntry.setEnvEntryType("java.lang.String");
  +        compoundEntry.setEnvEntryValue("long name");
   
           EjbRef ejbRef = new EjbRef();
           ejbRef.setEJBRefName("here/there/EJB1");
  @@ -140,7 +144,7 @@
           cfRef.setResRefName("DefaultCF");
           cfRef.setJndiName(objectName1);
   
  -        client.setEnvEntry(new EnvEntry[] { stringEntry, intEntry });
  +        client.setEnvEntry(new EnvEntry[] { stringEntry, intEntry, compoundEntry });
           session.setEnvEntry(client.getEnvEntry());
           client.setEJBRef(new EjbRef[] {ejbRef, ejbLinkRef});
           session.setEJBRef(client.getEJBRef());
  @@ -155,6 +159,10 @@
           assertEquals("Hello World", compCtx.lookup("env/string"));
           assertEquals(new Integer(12345), compCtx.lookup("env/int"));
           assertEquals(new URL("http://localhost/path"), compCtx.lookup("env/url/testURL"));
  +        assertEquals("long name", compCtx.lookup("env/this/is/a/compound/name"));
  +        Context intermediate = (Context)compCtx.lookup("env/this/is/a");
  +        assertNotNull(intermediate);
  +        assertEquals("long name", intermediate.lookup("compound/name"));
       }
   
       public void testUserTransaction() throws Exception {
  
  
  
  1.2       +14 -33    incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/ThreadContextTest.java
  
  Index: ThreadContextTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/ThreadContextTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ThreadContextTest.java	30 Oct 2003 02:00:07 -0000	1.1
  +++ ThreadContextTest.java	12 Jan 2004 06:19:52 -0000	1.2
  @@ -55,62 +55,43 @@
    */
   package org.apache.geronimo.naming.java;
   
  -import java.util.HashMap;
  -import java.util.Iterator;
  -import java.util.Map;
  -import java.util.Properties;
  -import java.util.NoSuchElementException;
  -import java.util.Hashtable;
  -
  -import javax.naming.Binding;
  -import javax.naming.CompositeName;
  -import javax.naming.CompoundName;
  -import javax.naming.Context;
   import javax.naming.InitialContext;
  -import javax.naming.NameClassPair;
  -import javax.naming.NamingEnumeration;
  -import javax.naming.NamingException;
   import javax.naming.LinkRef;
   
  -import org.apache.geronimo.common.StopWatch;
  -
   import junit.framework.TestCase;
   
   /**
    * Test component context can be inherited by Threads spawned by
    * a component. This is required for Application Client and Servlets;
  - * it is not applicable to EJBs as they are not allowed to create Threads. 
  + * it is not applicable to EJBs as they are not allowed to create Threads.
    *
    * @version $Revision$ $Date$
    */
   public class ThreadContextTest extends TestCase {
  -    private Map compBinding;
  -    private Map envBinding;
   
  -    public void testThreadInheritence() throws Exception {
  -        RootContext.setComponentContext(new ReadOnlyContext(compBinding));
  +    private Throwable failure = null;
  +    public void testThreadInheritence() throws Throwable {
           Thread worker = new Thread() {
               public void run() {
                   try {
                       assertEquals("Hello", new InitialContext().lookup("java:comp/env/hello"));
  -                } catch (NamingException e) {
  -                    fail(e.getMessage());
  +                } catch (Throwable e) {
  +                    failure = e;
                   }
               }
           };
           worker.start();
           worker.join();
  +        if (failure != null) {
  +            throw failure;
  +        }
       }
   
       protected void setUp() throws Exception {
  -        super.setUp();
  -
  -        compBinding = new HashMap();
  -
  -        envBinding = new HashMap();
  -        envBinding.put("hello", "Hello");
  -        envBinding.put("world", "Hello World");
  -        envBinding.put("link", new LinkRef("java:comp/env/hello"));
  -        compBinding.put("env", new ReadOnlyContext(envBinding));
  +        ReadOnlyContext readOnlyContext = new ReadOnlyContext();
  +        readOnlyContext.internalBind("env/hello", "Hello");
  +        readOnlyContext.internalBind("env/world", "Hello World");
  +        readOnlyContext.internalBind("env/link", new LinkRef("java:comp/env/hello"));
  +        RootContext.setComponentContext(readOnlyContext);
       }
   }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/java/AbstractContextTest.java
  
  Index: AbstractContextTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.naming.java;
  
  import java.util.HashMap;
  import java.util.Properties;
  import java.util.Map;
  
  import javax.naming.InitialContext;
  import javax.naming.LinkRef;
  import javax.naming.Context;
  import javax.naming.NamingException;
  
  import junit.framework.TestCase;
  
  /**
   *
   *
   * @version $Revision: 1.1 $ $Date: 2004/01/12 06:19:52 $
   *
   * */
  public class AbstractContextTest extends TestCase {
      protected ReadOnlyContext readOnlyContext;
      protected Properties syntax;
      protected Map envBinding;
      protected Context initialContext;
      protected Context compContext;
      protected Context envContext;
  
      protected void setUp() throws Exception {
          initialContext = new InitialContext();
  
          readOnlyContext = new ReadOnlyContext();
  
          envBinding = new HashMap();
          readOnlyContext.internalBind("env/hello", "Hello");
          envBinding.put("hello", "Hello");
          bind("env/world", "Hello World");
          envBinding.put("world", "Hello World");
          bind("env/here/there/anywhere", "long name");
          envBinding.put("here", readOnlyContext.lookup("env/here"));
          LinkRef link = new LinkRef("java:comp/env/hello");
          bind("env/link", link);
          envBinding.put("link", link);
  
          RootContext.setComponentContext(readOnlyContext);
  
          compContext = (Context) initialContext.lookup("java:comp");
          envContext = (Context) initialContext.lookup("java:comp/env");
  
          syntax = new Properties();
      }
  
      protected void bind(String name, Object value) throws NamingException {
          readOnlyContext.internalBind(name, value);
      }
  }
  
  
  
  1.11      +36 -28    incubator-geronimo/modules/core/src/java/org/apache/geronimo/naming/java/ComponentContextBuilder.java
  
  Index: ComponentContextBuilder.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/naming/java/ComponentContextBuilder.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ComponentContextBuilder.java	16 Nov 2003 05:24:38 -0000	1.10
  +++ ComponentContextBuilder.java	12 Jan 2004 06:19:52 -0000	1.11
  @@ -57,18 +57,15 @@
   
   import java.net.MalformedURLException;
   import java.net.URL;
  -import java.util.HashMap;
  -import java.util.Map;
  -import javax.naming.LinkRef;
  -import javax.naming.Reference;
  +
   import javax.naming.NamingException;
  +import javax.naming.Reference;
   import javax.transaction.UserTransaction;
   
  +import org.apache.geronimo.deployment.model.geronimo.j2ee.EjbLocalRef;
   import org.apache.geronimo.deployment.model.geronimo.j2ee.EjbRef;
   import org.apache.geronimo.deployment.model.geronimo.j2ee.JNDIEnvironmentRefs;
   import org.apache.geronimo.deployment.model.geronimo.j2ee.ResourceRef;
  -import org.apache.geronimo.deployment.model.geronimo.j2ee.JNDILocator;
  -import org.apache.geronimo.deployment.model.geronimo.j2ee.EjbLocalRef;
   import org.apache.geronimo.deployment.model.j2ee.EnvEntry;
   import org.apache.geronimo.kernel.deployment.DeploymentException;
   
  @@ -81,6 +78,7 @@
   
       private final ReferenceFactory referenceFactory;
       private final UserTransaction userTransaction;
  +    private static final String ENV = "env/";
   
       public ComponentContextBuilder(ReferenceFactory referenceFactory, UserTransaction userTransaction)
{
           this.userTransaction = userTransaction;
  @@ -94,21 +92,23 @@
        * @return a Context that can be bound to java:comp
        */
       public ReadOnlyContext buildContext(JNDIEnvironmentRefs refs) throws DeploymentException
{
  -        Map envMap = new HashMap();
  -        buildEnvEntries(envMap, refs.getEnvEntry());
  -        buildEJBRefs(envMap, refs.getGeronimoEJBRef());
  -        buildEJBLocalRefs(envMap, refs.getGeronimoEJBLocalRef());
  -        buildResourceRefs(envMap, refs.getGeronimoResourceRef());
  +        ReadOnlyContext readOnlyContext = new ReadOnlyContext();
  +        buildEnvEntries(readOnlyContext, refs.getEnvEntry());
  +        buildEJBRefs(readOnlyContext, refs.getGeronimoEJBRef());
  +        buildEJBLocalRefs(readOnlyContext, refs.getGeronimoEJBLocalRef());
  +        buildResourceRefs(readOnlyContext, refs.getGeronimoResourceRef());
   
  -        Map compMap = new HashMap();
  -        compMap.put("env", new ReadOnlyContext(envMap));
           if (userTransaction != null) {
  -            compMap.put("UserTransaction", userTransaction);
  +            try {
  +                readOnlyContext.internalBind("UserTransaction", userTransaction);
  +            } catch (NamingException e) {
  +                throw new DeploymentException("could not bind UserTransaction", e);
  +            }
           }
  -        return new ReadOnlyContext(compMap);
  +        return readOnlyContext;
       }
   
  -    private static void buildEnvEntries(Map envMap, EnvEntry[] envEntries) throws DeploymentException
{
  +    private static void buildEnvEntries(ReadOnlyContext readOnlyContext, EnvEntry[] envEntries)
throws DeploymentException {
           for (int i = 0; i < envEntries.length; i++) {
               EnvEntry entry = envEntries[i];
               String name = entry.getEnvEntryName();
  @@ -142,13 +142,15 @@
               } catch (NumberFormatException e) {
                   throw new DeploymentException("Invalid numeric value for env-entry " +
name + ", value=" + value);
               }
  -            if (envMap.put(name, mapEntry) != null) {
  -                throw new AssertionError("Duplicate entry for env-entry " + name);
  +            try {
  +                readOnlyContext.internalBind(ENV + name, mapEntry);
  +            } catch (NamingException e) {
  +                throw new DeploymentException("Could not bind", e);
               }
           }
       }
   
  -    private void buildEJBRefs(Map envMap, EjbRef[] ejbRefs) throws DeploymentException
{
  +    private void buildEJBRefs(ReadOnlyContext readOnlyContext, EjbRef[] ejbRefs) throws
DeploymentException {
           for (int i = 0; i < ejbRefs.length; i++) {
               EjbRef ejbRef = ejbRefs[i];
               String name = ejbRef.getEJBRefName();
  @@ -158,13 +160,15 @@
               } catch (NamingException e) {
                   throw new DeploymentException("Could not construct reference to " + ejbRef.getJndiName()
+ ", " + e.getMessage());
               }
  -            if (envMap.put(name, ref) != null) {
  -                throw new DeploymentException("Duplicate entry for env-entry " + name);
  +            try {
  +                readOnlyContext.internalBind(ENV + name, ref);
  +            } catch (NamingException e) {
  +                throw new DeploymentException("could not bind", e);
               }
           }
       }
   
  -    private void buildEJBLocalRefs(Map envMap, EjbLocalRef[] ejbLocalRefs) throws DeploymentException
{
  +    private void buildEJBLocalRefs(ReadOnlyContext readOnlyContext, EjbLocalRef[] ejbLocalRefs)
throws DeploymentException {
           for (int i = 0; i < ejbLocalRefs.length; i++) {
               EjbLocalRef ejbLocalRef = ejbLocalRefs[i];
               String name = ejbLocalRef.getEJBRefName();
  @@ -174,13 +178,15 @@
               } catch (NamingException e) {
                   throw new DeploymentException("Could not construct reference to " + ejbLocalRef.getJndiName()
+ ", " + e.getMessage());
               }
  -            if (envMap.put(name, ref) != null) {
  -                throw new DeploymentException("Duplicate entry for env-entry " + name);
  +            try {
  +                readOnlyContext.internalBind(ENV + name, ref);
  +            } catch (NamingException e) {
  +                throw new DeploymentException("Could not bind", e);
               }
           }
       }
   
  -    private void buildResourceRefs(Map envMap, ResourceRef[] resRefs) throws DeploymentException
{
  +    private void buildResourceRefs(ReadOnlyContext readOnlyContext, ResourceRef[] resRefs)
throws DeploymentException {
           for (int i=0; i < resRefs.length; i++) {
               ResourceRef resRef = resRefs[i];
               String name = resRef.getResRefName();
  @@ -199,8 +205,10 @@
                       throw new DeploymentException("Could not construct reference to " +
resRef.getJndiName());
                   }
               }
  -            if (envMap.put(name, ref) != null) {
  -                throw new AssertionError("Duplicate entry for resource-ref " + name);
  +            try {
  +                readOnlyContext.internalBind(ENV + name, ref);
  +            } catch (NamingException e) {
  +                throw new DeploymentException("Could not bind", e);
               }
           }
       }
  
  
  
  1.8       +57 -22    incubator-geronimo/modules/core/src/java/org/apache/geronimo/naming/java/ReadOnlyContext.java
  
  Index: ReadOnlyContext.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/naming/java/ReadOnlyContext.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ReadOnlyContext.java	9 Dec 2003 05:03:49 -0000	1.7
  +++ ReadOnlyContext.java	12 Jan 2004 06:19:52 -0000	1.8
  @@ -60,6 +60,7 @@
   import java.util.Hashtable;
   import java.util.Iterator;
   import java.util.Map;
  +
   import javax.naming.Binding;
   import javax.naming.CompositeName;
   import javax.naming.Context;
  @@ -99,26 +100,11 @@
       private final Map bindings;         // bindings at my level
       private final Map treeBindings;     // all bindings under me
   
  -    /**
  -     * Construct a ReadOnlyContext with the bindings as defined by the Map
  -     * @param bindings the bindings for this level; may include sub-contexts
  -     */
  -    public ReadOnlyContext(Map bindings) {
  -        this.env = new Hashtable();
  -        this.bindings = new HashMap(bindings);
  -        treeBindings = new HashMap(bindings);
  -        for (Iterator i = bindings.entrySet().iterator(); i.hasNext();) {
  -            Map.Entry entry = (Map.Entry) i.next();
  -            Object value = entry.getValue();
  -            if (value instanceof ReadOnlyContext) {
  -                String key = (String) entry.getKey() + '/';
  -                Map subBindings = ((ReadOnlyContext) value).treeBindings;
  -                for (Iterator j = subBindings.entrySet().iterator(); j.hasNext();) {
  -                    Map.Entry subEntry = (Map.Entry) j.next();
  -                    treeBindings.put(key + subEntry.getKey(), subEntry.getValue());
  -                }
  -            }
  -        }
  +
  +    ReadOnlyContext() {
  +        env = new Hashtable();
  +        bindings = new HashMap();
  +        treeBindings = new HashMap();
       }
   
       ReadOnlyContext(Hashtable env) {
  @@ -137,6 +123,56 @@
           this.env = new Hashtable(env);
       }
   
  +    /**
  +     * internalBind is intended for use only during setup or possibly by suitably synchronized
superclasses.
  +     * It binds every possible lookup into a map in each context.  To do this, each context
  +     * strips off one name segment and if necessary creates a new context for it. Then
it asks that context
  +     * to bind the remaining name.  It returns a map containing all the bindings from the
next context, plus
  +     * the context it just created (if it in fact created it). (the names are suitably
extended by the segment
  +     * originally lopped off).
  +     * @param name
  +     * @param value
  +     * @return
  +     * @throws NamingException
  +     */
  +    Map internalBind(String name, Object value) throws NamingException {
  +        assert name != null;
  +        assert !name.equals("");
  +        Map newBindings = new HashMap();
  +        int pos = name.indexOf('/');
  +        if (pos == -1) {
  +            if (treeBindings.put(name, value) != null) {
  +                throw new NamingException("Something already bound at " + name);
  +            }
  +            bindings.put(name, value);
  +            newBindings.put(name, value);
  +        } else {
  +            String segment = name.substring(0, pos);
  +            assert segment != null;
  +            assert !segment.equals("");
  +            Object o = treeBindings.get(segment);
  +            if (o == null) {
  +                o = new ReadOnlyContext();
  +                treeBindings.put(segment, o);
  +                bindings.put(segment, o);
  +                newBindings.put(segment, o);
  +            } else if (!(o instanceof ReadOnlyContext)) {
  +                throw new NamingException("Something already bound where a subcontext should
go");
  +            }
  +            ReadOnlyContext readOnlyContext = (ReadOnlyContext)o;
  +            String remainder = name.substring(pos + 1);
  +            Map subBindings = readOnlyContext.internalBind(remainder, value);
  +            for (Iterator iterator = subBindings.entrySet().iterator(); iterator.hasNext();)
{
  +                Map.Entry entry = (Map.Entry) iterator.next();
  +                String subName = segment + "/" + (String)entry.getKey();
  +                Object bound = entry.getValue();
  +                treeBindings.put(subName, bound);
  +                newBindings.put(subName, bound);
  +            }
  +        }
  +        return newBindings;
  +    }
  +
       public Object addToEnvironment(String propName, Object propVal) throws NamingException
{
           return env.put(propName, propVal);
       }
  @@ -309,7 +345,6 @@
       public void unbind(String name) throws NamingException {
           throw new OperationNotSupportedException();
       }
  -
   
       private abstract class LocalNamingEnumeration implements NamingEnumeration {
           private Iterator i = bindings.entrySet().iterator();
  
  
  
  1.6       +13 -20    incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/jmx/ContextTest.java
  
  Index: ContextTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/core/src/test/org/apache/geronimo/naming/jmx/ContextTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ContextTest.java	19 Nov 2003 02:08:42 -0000	1.5
  +++ ContextTest.java	12 Jan 2004 06:19:52 -0000	1.6
  @@ -56,8 +56,8 @@
   
   package org.apache.geronimo.naming.jmx;
   
  -import java.util.HashMap;
   import java.util.Hashtable;
  +
   import javax.management.MBeanServer;
   import javax.management.ObjectName;
   import javax.naming.CompositeName;
  @@ -66,11 +66,10 @@
   import javax.naming.LinkRef;
   import javax.naming.NamingException;
   
  -import junit.framework.TestCase;
   import org.apache.geronimo.kernel.jmx.JMXKernel;
  -import org.apache.geronimo.kernel.service.GeronimoMBeanInfo;
   import org.apache.geronimo.kernel.service.GeronimoMBean;
  -import org.apache.geronimo.naming.java.ReadOnlyContext;
  +import org.apache.geronimo.kernel.service.GeronimoMBeanInfo;
  +import org.apache.geronimo.naming.java.AbstractContextTest;
   import org.apache.geronimo.naming.java.RootContext;
   import org.apache.geronimo.test.util.ServerUtil;
   
  @@ -80,7 +79,7 @@
    * @version $Revision$ $Date$
    *
    * */
  -public class ContextTest extends TestCase {
  +public class ContextTest extends AbstractContextTest {
   
       private final static String on1 = "geronimo.test:name=test,role=first";
       private final static String mn1 = "getEJBHome";
  @@ -91,6 +90,7 @@
       private TestObject mbean;
   
       protected void setUp() throws Exception {
  +        super.setUp();
           server = ServerUtil.newLocalServer();
   
           agentId = JMXKernel.getMBeanServerId(server);
  @@ -145,34 +145,27 @@
            }
       }
   
  -    public void XtestJmxURLContextFactory() throws Exception {
  +    public void testJmxURLContextFactory() throws Exception {
           jmxURLContextFactory contextFactory = new jmxURLContextFactory();
           Context context = (Context)contextFactory.getObjectInstance(null, null, null, new
Hashtable());
           Object result = context.lookup(JMXContext.encode(agentId, on1, mn1));
           assertTrue("Expected the supplied object back", result == mbean.getEJBHome());
       }
   
  +    public void testLinkRefToJMX() throws Exception {
  +        bind("env/link2", new LinkRef(JMXContext.encode(agentId, on1, mn1)));
  +        RootContext.setComponentContext(readOnlyContext);
   
  -    public void XtestLinkRefToJMX() throws Exception {
           InitialContext initialContext = new InitialContext();
  -
  -        HashMap compBinding = new HashMap();
  -
  -        HashMap envBinding = new HashMap();
  -        envBinding.put("link", new LinkRef(JMXContext.encode(agentId, on1, mn1)));
  -        compBinding.put("env", new ReadOnlyContext(envBinding));
  -        RootContext.setComponentContext(new ReadOnlyContext(compBinding));
  -
           Context compContext = (Context) initialContext.lookup("java:comp");
           Context envContext = (Context) initialContext.lookup("java:comp/env");
   
  -        Object result = initialContext.lookup("java:comp/env/link");
  +        Object result = initialContext.lookup("java:comp/env/link2");
           assertTrue("Expected the supplied object back", result == mbean.getEJBHome());
  -        result = compContext.lookup("env/link");
  +        result = compContext.lookup("env/link2");
           assertTrue("Expected the supplied object back", result == mbean.getEJBHome());
  -        result = envContext.lookup("link");
  +        result = envContext.lookup("link2");
           assertTrue("Expected the supplied object back", result == mbean.getEJBHome());
       }
  -
   
   }
  
  
  

Mime
View raw message