KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > naming > cos > MemoryContextTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * MemoryContextTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.naming.cos;
25
26 import junit.framework.*;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InvalidNameException JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NameAlreadyBoundException JavaDoc;
33 import javax.naming.NameParser JavaDoc;
34 import javax.naming.NamingEnumeration JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.naming.NameNotFoundException JavaDoc;
37
38 /**
39  *
40  * @author mincemeat
41  */

42 public class MemoryContextTest extends TestCase {
43     
44     public MemoryContextTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52     }
53
54     public static Test suite() {
55         TestSuite suite = new TestSuite(MemoryContextTest.class);
56         
57         return suite;
58     }
59
60     /**
61      * Test of addToEnvironment method, of class com.rift.coad.lib.naming.cos.MemoryContext.
62      */

63     public void testMemoryContext() throws Exception JavaDoc {
64         System.out.println("MemoryContext");
65         
66         Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc ex) {
100             // ignore
101
}
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 JavaDoc ex) {
115             // ignore
116
}
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 JavaDoc enumer =
126                 memoryContext.listBindings("java:comp/test");
127         int found = 0;
128         while(enumer.hasMore()) {
129             Object JavaDoc 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 JavaDoc 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 JavaDoc 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