Add junit test that verifies the behaviour IvmContext.list()

Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/97afaeb0
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/97afaeb0
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/97afaeb0

Branch: refs/heads/master
Commit: 97afaeb01f454ecbccb21c09f8a87fc9f2caa141
Parents: 6379457
Author: Svetlin Zarev <svetlin.za...@sap.com>
Authored: Tue Jul 11 12:39:40 2017 +0300
Committer: Svetlin Zarev <svetlin.za...@sap.com>
Committed: Tue Jul 11 13:36:15 2017 +0300

----------------------------------------------------------------------
 .../openejb/ivm/naming/IvmContextTest.java      | 184 ++++++++++++++++++-
 1 file changed, 178 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/97afaeb0/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
----------------------------------------------------------------------
diff --git 
a/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
 
b/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
index 47cef26..e694791 100644
--- 
a/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
+++ 
b/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
@@ -21,13 +21,14 @@ import org.apache.openejb.core.ivm.naming.NameNode;
 import org.apache.openejb.util.Contexts;
 import org.junit.Test;
 
-import javax.naming.Context;
-import javax.naming.NamingException;
+import javax.naming.*;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintWriter;
 import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
 
 public class IvmContextTest {
     @Test
@@ -105,7 +106,7 @@ public class IvmContextTest {
 
         try {
             ((Context) ((Context) context.lookup("global")).lookup("foo"))
-                .lookup("Bar");
+                    .lookup("Bar");
             fail();
         } catch (final NamingException ne) {
             // ok
@@ -161,4 +162,175 @@ public class IvmContextTest {
             throw new AssertionError();
         }
     }
+
+    /*
+     * verify that list() will return only the subcontexts
+     */
+    @Test
+    public void testContextList_rootContexts() throws NamingException {
+        final Map<String, String> expected = new HashMap<>();
+        expected.put("global", "global");
+        expected.put("app", "app");
+        expected.put("module", "module");
+
+        verifyListedContent(expected, "");
+    }
+
+    /*
+     * verify that list() will return the properties from
+     * both the normal and federated contexts
+     */
+    @Test
+    public void testContextList_moduleEnvProperties() throws NamingException {
+        final Map<String, String> expected = new HashMap<>();
+        expected.put("federated-prop-1", "federated-prop-1");
+        expected.put("federated-prop-2", "federated-prop-2");
+        expected.put("federated-prop-3", "federated-prop-3");
+        expected.put("prop-1", "prop-1");
+        expected.put("prop-2", "prop-2");
+        expected.put("prop-3", "prop-3");
+
+        verifyListedContent(expected, "module/env/properties");
+    }
+
+    /*
+     * verify that list() will return the correct number of contexts and 
properties
+     * Some of the contexts appear twice -> i.e there is a federated and 
ordinary "configurations"
+     */
+    @Test
+    public void testContextList_moduleEnv() throws NamingException {
+        final Map<String, String> expected = new HashMap<>();
+        expected.put("properties", "properties");
+        expected.put("configurations", "configurations");
+        expected.put("env-1", "env-1");
+        expected.put("env-2", "env-2");
+        expected.put("env-3", "env-3");
+
+        verifyListedContent(expected, "module/env");
+    }
+
+    public void verifyListedContent(Map<String, String> expected, String 
address) throws NamingException {
+        final IvmContext root = createTestIvmContext();
+        final NamingEnumeration<NameClassPair> resultSet = root.list(address);
+
+        int numberOfListedItems = 0;
+        while (resultSet.hasMoreElements()) {
+            final NameClassPair nameClassPair = resultSet.nextElement();
+            final String name = nameClassPair.getName();
+            final Object expectedObject = expected.get(name);
+            assertNotNull("The expected set does not contain object with name: 
" + name, expectedObject);
+
+            //Intentionally use the whole address and lookup from the root 
node: TOMEE-2087
+            final Object actualObject = root.lookup(address + "/" + name);
+
+            if (actualObject instanceof IvmContext) {
+                assertEquals(expectedObject, ((IvmContext) 
actualObject).mynode.getAtomicName());
+            } else {
+                assertEquals(expectedObject, actualObject);
+            }
+            numberOfListedItems++;
+        }
+
+        if (numberOfListedItems != expected.size()) {
+            fail("IvmContext.list() returned fifferent number of elements than 
expected. " +
+                    "Expected=" + expected.size() + "\tActual=" + 
numberOfListedItems);
+        }
+    }
+
+    @Test
+    public void testContextList() throws NamingException {
+        final IvmContext root = createTestIvmContext();
+        final ByteArrayOutputStream logBuffer = new ByteArrayOutputStream();
+        final PrintWriter logWriter = new PrintWriter(logBuffer);
+
+        final boolean hasErrors = listContext(root, "", logWriter);
+        logWriter.flush();
+
+        assertFalse(logBuffer.toString(), hasErrors);
+    }
+
+    private IvmContext createTestIvmContext() throws NamingException {
+        final IvmContext root = IvmContext.createRootContext();
+        root.createSubcontext("global");
+        root.createSubcontext("module");
+        root.createSubcontext("app");
+
+        root.bind("global/GlobalBean-1", "GlobalBean-1");
+        root.bind("global/GlobalBean-2", "GlobalBean-2");
+        root.bind("app/AppBean-1", "AppBean-1");
+        root.bind("app/AppBean-2", "AppBean-2");
+
+        root.bind("module/env/properties/prop-1", "prop-1");
+        root.bind("module/env/properties/prop-2", "prop-2");
+        root.bind("module/env/properties/prop-3", "prop-3");
+
+        final IvmContext federatedProperties = new IvmContext();
+        root.bind("module/env/properties", federatedProperties);
+        federatedProperties.bind("federated-prop-1", "federated-prop-1");
+        federatedProperties.bind("federated-prop-2", "federated-prop-2");
+        federatedProperties.bind("federated-prop-3", "federated-prop-3");
+
+        final IvmContext federatedConfigurations = new IvmContext();
+        root.bind("module/env/configurations", federatedConfigurations);
+        federatedConfigurations.bind("federated-conf-1", "federated-conf-1");
+        federatedConfigurations.bind("federated-conf-2", "federated-conf-2");
+        federatedConfigurations.bind("federated-conf-3", "federated-conf-3");
+
+        root.bind("module/env/configurations/conf-1", "conf-1");
+        root.bind("module/env/configurations/conf-2", "conf-2");
+        root.bind("module/env/configurations/conf-3", "conf-3");
+
+        root.bind("module/env/env-1", "env-1");
+        root.bind("module/env/env-2", "env-2");
+        root.bind("module/env/env-3", "env-3");
+
+        root.bind("module/Bean-1", "Bean-1");
+        root.bind("module/Bean-2", "Bean-2");
+        root.bind("module/Bean-3", "Bean-3");
+
+        return root;
+    }
+
+    private static boolean listContext(Context context, String ctxName, 
PrintWriter writer) throws javax.naming.NamingException {
+        writer.println("\n### Context: " + ctxName);
+        boolean hasErrors = false;
+
+        final Map<Context, String> subContexts = new HashMap<>();
+        final NamingEnumeration<? extends NameClassPair> content = 
context.list("");
+
+        while (content.hasMoreElements()) {
+            final NameClassPair nameClassPair = content.nextElement();
+            final String name = nameClassPair.getName();
+            final String className = nameClassPair.getClassName();
+
+            writer.print("Name: " + name);
+            writer.print("\tClass=" + className);
+            writer.print("\t[looking up: " + (ctxName + "/" + name) + "]");
+
+            try {
+                final Object object = context.lookup(name);
+                if (object instanceof Context) {
+                    subContexts.put((Context) object, ctxName + "/" + name);
+                }
+
+                if (className.endsWith("Reference")) {
+                    writer.print("\t[Reference]: " + object);
+                } else {
+                    writer.print("\t[Value]: " + object);
+                }
+            } catch (Exception ex) {
+                writer.print("Failed to lookup: " + ctxName + "/" + name + 
"\tError: " + ex);
+                hasErrors = true;
+            }
+
+            writer.println();
+        }
+        writer.println();
+
+        for (Context subContext : subContexts.keySet()) {
+            hasErrors |= listContext(subContext, subContexts.get(subContext), 
writer);
+        }
+
+        return hasErrors;
+    }
 }

Reply via email to