[CVS spice] added tests to verify fix for SPICE-47: Handling of subcontexts broken

tanderson-yCVjj/[email protected] 24 Jun 2005 15:13:52 -0000
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
<html>
<head>
<style><!--
  body {background-color:#ffffff;}
  .file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
  .pathname {font-family:monospace; float:right;}
  .fileheader {margin-bottom:.5em;}
  .diff {margin:0;}
  .tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
  .tasklist ul {margin-top:0;margin-bottom:0;}
  tr.alt {background-color:#eeeeee}
  #added {background-color:#ddffdd;}
  #addedchars {background-color:#99ff99;font-weight:bolder;}
  tr.alt #added {background-color:#ccf7cc;}
  #removed {background-color:#ffdddd;}
  #removedchars {background-color:#ff9999;font-weight:bolder;}
  tr.alt #removed {background-color:#f7cccc;}
  #info {color:#888888;}
  #context {background-color:#eeeeee;}
  td {padding-left:.3em;padding-right:.3em;}
  tr.head {border-bottom-width:1px;border-bottom-style:solid;}
  tr.head td {padding:0;padding-top:.2em;}
  .task {background-color:#ffff00;}
  .comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
  .error {color:red;}
  hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>spice/components/jndikit/src/test/org/codehaus/spice/jndikit/test</tt></b><span id="info"> on MAIN</span></td></tr>
<tr><td><tt><a href="#file1">AbstractContextTestCase.java</a></tt></td><td align="right" id="added">+194</td><td align="right" id="removed">-1</td><td nowrap="nowrap" align="center">1.1 -&gt; 1.2</td></tr>
</table>
<pre class="comment">
added tests to verify fix for SPICE-47: Handling of subcontexts broken
</pre>
<hr /><a name="file1" /><div class="file">
<span class="pathname">spice/components/jndikit/src/test/org/codehaus/spice/jndikit/test<br /></span>
<div class="fileheader"><big><b>AbstractContextTestCase.java</b></big> <small id="info">1.1 -&gt; 1.2</small></div>
<pre class="diff"><small id="info">diff -u -r1.1 -r1.2
--- AbstractContextTestCase.java	27 Nov 2003 00:50:48 -0000	1.1
+++ AbstractContextTestCase.java	24 Jun 2005 15:13:52 -0000	1.2
@@ -8,8 +8,16 @@
</small></pre><pre class="diff" id="context"> package org.codehaus.spice.jndikit.test;
 
 import java.util.Enumeration;
</pre><pre class="diff" id="added">+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import javax.naming.Binding;
</pre><pre class="diff" id="context"> import javax.naming.Context;
</pre><pre class="diff" id="added">+import javax.naming.NameClassPair;
</pre><pre class="diff" id="context"> import javax.naming.NameNotFoundException;
</pre><pre class="diff" id="added">+import javax.naming.NamingEnumeration;
</pre><pre class="diff" id="context"> import javax.naming.NamingException;
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
</pre><pre class="diff"><small id="info">@@ -18,7 +26,7 @@
</small></pre><pre class="diff" id="context">  * Unit testing for JNDI system
  *
  * @author Peter Donald
</pre><pre class="diff" id="removed">- * @version $Revision: 1.<span id="removedchars">1</span> $
</pre><pre class="diff" id="added">+ * @version $Revision: 1.<span id="addedchars">2</span> $
</pre><pre class="diff" id="context">  */
 public abstract class AbstractContextTestCase
     extends TestCase
</pre><pre class="diff"><small id="info">@@ -527,6 +535,191 @@
</small></pre><pre class="diff" id="context">         }
         catch( final NamingException ne )
         {
</pre><pre class="diff" id="added">+        }
+    }
+
+    /**
+     * Create a subcontext, bind to it, and verify that the objects
+     * can be looked up from it
+     */
+    public void testSubcontextBindAndLookup() throws AssertionFailedError {
+        try {
+            m_context.createSubcontext("x");
+            Context context = (Context) m_context.lookup("x");
+            
+            context.bind("o1", O1);
+            
+            assertTrue("Make sure lookup o1 returns correct object",
+                       context.lookup("o1").equals(O1));
+            assertTrue("Make sure lookup o1 from root returns correct object",
+                        m_context.lookup("x/o1").equals(O1));
+        } catch (final NamingException ne) {
+            throw new AssertionFailedError( ne.toString());
+        }
+    }
+
+    /**
+     * Create a subcontext, bind to it, and verify that:
+     * &lt;ul&gt;
+     *   &lt;li&gt;can list the binding names from the root context&lt;/li&gt;
+     *   &lt;li&gt;can list the binding names from the subcontext&lt;/li&gt;
+     * &lt;/ul&gt; 
+     */
+    public void testSubcontextBindAndList1() throws AssertionFailedError {
+        Map map = new HashMap();
+        map.put("o1", O1);
+        map.put("o2", O2);
+        map.put("o3", O3);
+        Set names;
+
+        try {
+            m_context.createSubcontext("x");
+            Context context = (Context) m_context.lookup("x");
+            
+            Iterator entries = map.entrySet().iterator();
+            while (entries.hasNext()) {
+                Map.Entry entry = (Map.Entry) entries.next();
+                context.bind((String) entry.getKey(), entry.getValue());
+            }
+            names = listNames(m_context, "x");
+            assertEquals("Make sure can list subcontext names from root",
+                         map.keySet(), names);
+
+            names = listNames(context, "");
+            assertEquals("Make sure can list subcontext names",
+                         map.keySet(), names);
+        } catch (final NamingException ne) {
+            throw new AssertionFailedError( ne.toString());
+        }
+    }
+
+    /**
+     * Create a subcontext, bind to it via the root context, and verify that:
+     * &lt;ul&gt;
+     *   &lt;li&gt;can list the binding names from the root context&lt;/li&gt;
+     *   &lt;li&gt;can list the binding names from the subcontext&lt;/li&gt;
+     * &lt;/ul&gt;
+     */
+    public void testSubcontextBindAndList2() throws AssertionFailedError {
+        Map map = new HashMap();
+        map.put("o1", O1);
+        map.put("o2", O2);
+        map.put("o3", O3);
+        Set names;
+
+        try {
+            m_context.createSubcontext("x");
+
+            Iterator entries = map.entrySet().iterator();
+            while (entries.hasNext()) {
+                Map.Entry entry = (Map.Entry) entries.next();
+                String name = "x/" + entry.getKey();
+                m_context.bind(name, entry.getValue());
+            }
+            names = listNames(m_context, "x");
+            assertEquals("Make sure can list subcontext names from root",
+                         map.keySet(), names);
+            
+            Context context = (Context) m_context.lookup("x");
+            names = listNames(context, "");
+            assertEquals("Make sure can list subcontext names",
+                         map.keySet(), names);
+        } catch (final NamingException ne) {
+            throw new AssertionFailedError( ne.toString());
+        }
+    }
+
+    public void testSubcontextListBindings() throws AssertionFailedError {
+        Map map = new HashMap();
+        map.put("o1", O1);
+        map.put("o2", O2);
+        map.put("o3", O3);
+        Set names = new HashSet();
+
+        try {
+            m_context.createSubcontext( "x" );
+            Context context = (Context) m_context.lookup("x");
+            Iterator entries = map.entrySet().iterator();
+            while (entries.hasNext()) {
+                Map.Entry entry = (Map.Entry) entries.next();
+                context.bind((String) entry.getKey(), entry.getValue());
+            }
+            NamingEnumeration bindings = context.listBindings("");
+            while (bindings.hasMore()) {
+                Binding binding = (Binding) bindings.next();
+                String name = binding.getName();
+                Object expected = map.get(name);
+                if (expected == null) {
+                    throw new AssertionFailedError(
+                        "Invalid binding: name=" + name + ", classname="
+                        + binding.getClassName() + ", object="
+                        + binding.getObject());
+                }
+                assertEquals(expected, binding.getObject());
+                names.add(name);
+            }
+        } catch (final NamingException ne) {
+            throw new AssertionFailedError( ne.toString());
+        }
+
+        assertEquals(map.keySet(), names);
+    }
+
+    /**
+     * Bind a tree of subcontexts, and ensure they can be listed
+     * @throws AssertionFailedError
+     */
+    public void testRecursiveListBindings() throws AssertionFailedError {
+        Map expected = new HashMap();
+        expected.put("o1", O1);
+        expected.put("o2", O2);
+        expected.put("x/o3", O3);
+        expected.put("x/o4", O4);
+        expected.put("x/y/o5", O5);
+        Map result = new HashMap();
+
+        try {
+            m_context.bind("o1", O1);
+            m_context.bind("o2", O2);
+            m_context.createSubcontext( "x" );
+            Context context = (Context) m_context.lookup("x");
+            context.bind("o3", O3);
+            context.bind("o4", O4);
+            m_context.createSubcontext( "x/y" );
+            context = (Context) m_context.lookup("x/y");
+            context.bind("o5", O5);
+
+            listRecursive("", m_context, result);
+        } catch (final NamingException ne) {
+            throw new AssertionFailedError( ne.toString());
+        }
+
+        assertEquals(expected, result);
+    }
+
+    private Set listNames(Context context, String name)
+        throws NamingException {
+        Set result = new HashSet();
+        NamingEnumeration names = context.list(name);
+        while (names.hasMore()) {
+            NameClassPair pair = (NameClassPair) names.next();
+            result.add(pair.getName());
+        }
+        return result;
+    }
+
+    private void listRecursive(String name, Context context, Map result) throws NamingException {
+        NamingEnumeration bindings = context.listBindings("");
+        while (bindings.hasMore()) {
+            Binding binding = (Binding) bindings.next();
+            Object object = binding.getObject();
+            String subName = (name.length() == 0) ? binding.getName() :
+                    name + "/" + binding.getName();
+            if (object instanceof Context) {
+                listRecursive(subName, (Context) object, result);
+            } else {
+                result.put(subName, object);
+            }
</pre><pre class="diff" id="context">         }
     }
 
</pre></div>
<center><small><a href="http://www.badgers-in-foil.co.uk/projects/cvsspam/" title="commit -&gt; email">CVSspam</a> 0.2.8</small></center>
</body></html>