1 17 package org.apache.geronimo.gjndi; 18 19 import junit.framework.TestCase; 20 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 23 import javax.naming.Name ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NameClassPair ; 26 import javax.naming.Binding ; 27 import java.util.Map ; 28 import java.util.Iterator ; 29 import java.util.TreeSet ; 30 import java.util.HashMap ; 31 32 import org.apache.xbean.naming.context.ContextUtil; 33 34 37 public abstract class AbstractContextTest extends TestCase { 38 public static void assertEq(Map expected, Context actual) throws NamingException { 39 AbstractContextTest.assertEq(ContextUtil.buildMapTree(expected), actual, actual, null); 40 } 41 42 public static void assertEq(Map expected, String pathInExpected, Context actual) throws NamingException { 43 ContextUtil.Node node = ContextUtil.buildMapTree(expected); 44 Name parsedName = actual.getNameParser("").parse(pathInExpected); 45 for (int i = 0; i < parsedName.size(); i++) { 46 String part = parsedName.get(i); 47 Object value = node.get(part); 48 if (value == null) { 49 throw new NamingException ("look for " + parsedName.getPrefix(i+1) + " in node tree is null "); 50 } 51 node = (ContextUtil.Node) value; 52 } 53 54 AbstractContextTest.assertEq(node, actual, actual, null); 55 } 56 57 private static void assertEq(ContextUtil.Node node, Context rootContext, Context currentContext, String path) throws NamingException { 58 for (Iterator iterator = node.entrySet().iterator(); iterator.hasNext();) { 59 Map.Entry entry = (Map.Entry ) iterator.next(); 60 String expectedName = (String ) entry.getKey(); 61 Object expectedValue = entry.getValue(); 62 63 String fullName = path == null ? expectedName : path + "/" + expectedName; 64 65 Object value = AbstractContextTest.assertLookup(expectedValue, currentContext, expectedName); 67 Object absoluteValue = AbstractContextTest.assertLookup(expectedValue, rootContext, fullName); 68 assertSame(fullName, value, absoluteValue); 69 70 if (expectedValue instanceof ContextUtil.Node) { 71 ContextUtil.Node expectedNode = (ContextUtil.Node) expectedValue; 72 73 AbstractContextTest.assertList(expectedNode, currentContext, expectedName); 75 AbstractContextTest.assertList(expectedNode, rootContext, fullName); 76 77 AbstractContextTest.assertEq(expectedNode, rootContext, (Context ) value, fullName); 78 } 79 } 80 } 81 82 public static Object assertLookup(Object expectedValue, Context context, String name) throws NamingException { 83 Object value = context.lookup(name); 84 85 String contextName = context.getNameInNamespace(); 86 if (contextName == null || contextName.length() == 0) contextName = "<root>"; 87 88 assertNotNull("lookup of " + name + " on " + contextName + " returned null", value); 89 90 if (expectedValue instanceof ContextUtil.Node) { 91 assertTrue("Expected lookup of " + name + " on " + contextName + " to return a Context, but got a " + value.getClass().getName(), 92 value instanceof Context ); 93 } else { 94 assertEquals("lookup of " + name + " on " + contextName, expectedValue, value); 95 } 96 97 Name parsedName = context.getNameParser("").parse(name); 98 Object valueFromParsedName = context.lookup(parsedName); 99 assertSame("lookup of " + name + " on " + contextName + " using a parsed name", value, valueFromParsedName); 100 101 return value; 102 } 103 104 public static void assertList(ContextUtil.Node node, Context context, String name) throws NamingException { 105 String contextName = context.getNameInNamespace(); 106 if (contextName == null || contextName.length() == 0) contextName = "<root>"; 107 108 AbstractContextTest.assertListResults(node, context.list(name), contextName, name, false); 109 AbstractContextTest.assertListResults(node, context.listBindings(name), contextName, name, true); 110 111 Name parsedName = context.getNameParser("").parse(name); 112 AbstractContextTest.assertListResults(node, context.list(parsedName), contextName, "parsed name " + name, false); 113 AbstractContextTest.assertListResults(node, context.listBindings(parsedName), contextName, "parsed name " + name, true); 114 } 115 116 public static void assertListResults(ContextUtil.Node node, NamingEnumeration enumeration, String contextName, String name, boolean wasListBinding) { 117 Map actualValues; 118 if (wasListBinding) { 119 actualValues = AbstractContextTest.toListBindingResults(enumeration); 120 } else { 121 actualValues = AbstractContextTest.toListResults(enumeration); 122 } 123 124 for (Iterator iterator = node.entrySet().iterator(); iterator.hasNext();) { 125 Map.Entry entry = (Map.Entry ) iterator.next(); 126 String expectedName = (String ) entry.getKey(); 127 Object expectedValue = entry.getValue(); 128 129 Object actualValue = actualValues.get(expectedName); 130 131 assertNotNull("list of " + name + " on " + contextName + " did not find value for " + expectedName, actualValue); 132 if (wasListBinding) { 133 if (expectedValue instanceof ContextUtil.Node) { 134 assertTrue("Expected list of " + name + " on " + contextName + " result value for " + expectedName + " to return a Context, but got a " + actualValue.getClass().getName(), 135 actualValue instanceof Context ); 136 } else { 137 assertEquals("list of " + name + " on " + contextName + " for value for " + expectedName, expectedValue, actualValue); 138 } 139 } else { 140 if (!(expectedValue instanceof ContextUtil.Node)) { 141 assertEquals("list of " + name + " on " + contextName + " for value for " + expectedName, expectedValue.getClass().getName(), actualValue); 142 } else { 143 } 145 } 146 } 147 148 TreeSet extraNames = new TreeSet (actualValues.keySet()); 149 extraNames.removeAll(node.keySet()); 150 if (!extraNames.isEmpty()) { 151 fail("list of " + name + " on " + contextName + " found extra values: " + extraNames); 152 } 153 } 154 155 private static Map toListResults(NamingEnumeration enumeration) { 156 Map result = new HashMap (); 157 while (enumeration.hasMoreElements()) { 158 NameClassPair nameClassPair = (NameClassPair ) enumeration.nextElement(); 159 String name = nameClassPair.getName(); 160 assertFalse(result.containsKey(name)); 161 result.put(name, nameClassPair.getClassName()); 162 } 163 return result; 164 } 165 166 private static Map toListBindingResults(NamingEnumeration enumeration) { 167 Map result = new HashMap (); 168 while (enumeration.hasMoreElements()) { 169 Binding binding = (Binding ) enumeration.nextElement(); 170 String name = binding.getName(); 171 assertFalse(result.containsKey(name)); 172 result.put(name, binding.getObject()); 173 } 174 return result; 175 } 176 } 177 | Popular Tags |