| 1 23 24 package com.rift.coad.lib.naming.cos; 25 26 import junit.framework.*; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import javax.naming.Context ; 30 import javax.naming.InvalidNameException ; 31 import javax.naming.Name ; 32 import javax.naming.NameAlreadyBoundException ; 33 import javax.naming.NameParser ; 34 import javax.naming.NamingEnumeration ; 35 import javax.naming.NamingException ; 36 import javax.naming.NameNotFoundException ; 37 38 42 public class MemoryContextTest extends TestCase { 43 44 public MemoryContextTest(String testName) { 45 super(testName); 46 } 47 48 protected void setUp() throws Exception { 49 } 50 51 protected void tearDown() throws Exception { 52 } 53 54 public static Test suite() { 55 TestSuite suite = new TestSuite(MemoryContextTest.class); 56 57 return suite; 58 } 59 60 63 public void testMemoryContext() throws Exception { 64 System.out.println("MemoryContext"); 65 66 Hashtable env = new Hashtable (); 67 MemoryContext memoryContext = new MemoryContext(env, new NamingParser(). 68 parse("")); 69 memoryContext.addToEnvironment("test env1","test env1"); 70 memoryContext.addToEnvironment("test env2","test env2"); 71 memoryContext.addToEnvironment("test env3","test env3"); 72 if (memoryContext.getEnvironment().size() != 3) { 73 fail("The environment should contain 3 entries"); 74 } 75 memoryContext.removeFromEnvironment("test env3"); 76 if (memoryContext.getEnvironment().size() != 2) { 77 fail("The environment should contain 2 entries"); 78 } 79 80 memoryContext.bind("java:comp/test/test1","test value1"); 81 memoryContext.bind("java:comp/test/test2","test value2"); 82 memoryContext.bind("java:comp/test/sub/test1","test value3"); 83 memoryContext.bind("java:comp/test/sub/sub2/test1","test value4"); 84 85 if (!(memoryContext.lookup("java:comp/test") instanceof MemoryContext)) { 86 fail("Failed to lookup a sub context"); 87 } 88 if (!memoryContext.lookup("java:comp/test/test1").equals("test value1")) { 89 fail("Failed to find value"); 90 } 91 if (!memoryContext.lookup("java:comp/test/sub/sub2/test1"). 92 equals("test value4")) { 93 fail("Failed to find value"); 94 } 95 96 try { 97 memoryContext.bind("java:comp/test/sub/sub2/test1","test value4"); 98 fail("Could bind a value again."); 99 } catch (NamingException ex) { 100 } 102 memoryContext.rebind("java:comp/test/sub/sub2/test1","test value5"); 103 104 if (!memoryContext.lookup("java:comp/test/sub/sub2/test1"). 105 equals("test value5")) { 106 fail("Failed to find value"); 107 } 108 109 memoryContext.rename("java:comp/test/sub/test1","java:comp/test/sub/sub3/test1"); 110 111 try { 112 memoryContext.lookup("java:comp/test/sub/test1"); 113 fail("Could still find value."); 114 } catch (NamingException ex) { 115 } 117 if (!memoryContext.lookup("java:comp/test/sub/sub3/test1"). 118 equals("test value3")) { 119 fail("Failed to find value"); 120 } 121 122 memoryContext.unbind("java:comp/test/test2"); 123 memoryContext.bind("java:comp/test/test2","test value6"); 124 125 NamingEnumeration enumer = 126 memoryContext.listBindings("java:comp/test"); 127 int found = 0; 128 while(enumer.hasMore()) { 129 Object result = enumer.next(); 130 if (result.equals("test value1")) { 131 found++; 132 } else if (result.equals("test value6")) { 133 found++; 134 } else if (result instanceof MemoryContext) { 135 found++; 136 } else { 137 fail("Un-recognised entry"); 138 } 139 } 140 if (found != 3) { 141 fail("List bindings return [" + found + "] rather than 3"); 142 } 143 144 String composedName = memoryContext. 145 composeName("test/bob","java:comp/test"); 146 if (!composedName.equals("java:comp/test/test/bob")) { 147 fail("Failed to compose name properly got [" + 148 composedName.toString() + "]"); 149 } 150 Name name = new NamingParser().parse("java:comp/test/test2"); 151 memoryContext.bind("java:comp/link/linkvalue",name); 152 if (!memoryContext.lookupLink("java:comp/link/linkvalue"). 153 equals("test value6")) { 154 fail("Failed to use the lookup link"); 155 } 156 157 } 158 159 } 160 | Popular Tags |