001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gjndi;
017:
018: import junit.framework.TestCase;
019:
020: import javax.naming.NamingException;
021: import javax.naming.CompositeName;
022: import javax.naming.CompoundName;
023: import javax.naming.Context;
024: import javax.naming.NamingEnumeration;
025: import javax.naming.NameClassPair;
026: import javax.naming.Binding;
027: import javax.naming.LinkRef;
028: import javax.naming.InitialContext;
029: import java.util.Map;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.NoSuchElementException;
033: import java.util.Properties;
034: import java.util.Collections;
035:
036: import org.apache.geronimo.naming.java.RootContext;
037: import org.apache.xbean.naming.context.ImmutableContext;
038: import org.apache.xbean.naming.context.WritableContext;
039: import org.apache.xbean.naming.context.ContextAccess;
040: import org.apache.xbean.naming.global.GlobalContextManager;
041:
042: /**
043: * @version $Rev$ $Date$
044: */
045: public class JavaCompContextTest extends TestCase {
046: protected Context readOnlyContext;
047: protected Properties syntax;
048: protected Map envBinding;
049: protected Context initialContext;
050: protected Context compContext;
051: protected Context envContext;
052:
053: public void testInitialContext() throws NamingException {
054: assertEquals("Hello", initialContext
055: .lookup("java:comp/env/hello"));
056: assertEquals("Hello", initialContext.lookup(new CompositeName(
057: "java:comp/env/hello")));
058: }
059:
060: public void testLookup() throws NamingException {
061: assertEquals("Hello", envContext.lookup("hello"));
062: assertEquals("Hello", compContext.lookup("env/hello"));
063: try {
064: envContext.lookup("foo");
065: fail();
066: } catch (NamingException e) {
067: // OK
068: }
069: assertEquals("Hello", envContext.lookup(new CompositeName(
070: "hello")));
071: assertEquals("Hello", compContext.lookup(new CompositeName(
072: "env/hello")));
073: assertEquals("Hello", envContext.lookup(new CompoundName(
074: "hello", syntax)));
075: assertEquals("Hello", compContext.lookup(new CompoundName(
076: "env/hello", syntax)));
077:
078: assertEquals(envContext, envContext.lookup(""));
079: }
080:
081: public void testSubContext() throws NamingException {
082: assertEquals("long name", initialContext
083: .lookup("java:comp/env/here/there/anywhere"));
084: Context intermediate = (Context) initialContext
085: .lookup("java:comp/env/here/there");
086: assertNotNull(intermediate);
087: assertEquals("long name", intermediate.lookup("anywhere"));
088: }
089:
090: // public void testSchemeLookup() throws NamingException {
091: // // envContext.lookup("dns:apache.org");
092: // assertEquals("Hello", envContext.lookup("java:comp/env/hello"));
093: // assertEquals("Hello", compContext.lookup("java:comp/env/hello"));
094: // }
095:
096: public void testLookupLink() throws NamingException {
097: assertEquals("Hello", envContext.lookup("link"));
098: }
099:
100: public void testComposeName() throws NamingException {
101: assertEquals("org/research/user/jane", envContext.composeName(
102: "user/jane", "org/research"));
103: assertEquals("research/user/jane", envContext.composeName(
104: "user/jane", "research"));
105: assertEquals(new CompositeName("org/research/user/jane"),
106: envContext.composeName(new CompositeName("user/jane"),
107: new CompositeName("org/research")));
108: assertEquals(new CompositeName("research/user/jane"),
109: envContext.composeName(new CompositeName("user/jane"),
110: new CompositeName("research")));
111: }
112:
113: public void testList() throws NamingException {
114: NamingEnumeration ne;
115: Map expected;
116: Map result;
117:
118: expected = new HashMap();
119: for (Iterator i = envBinding.entrySet().iterator(); i.hasNext();) {
120: Map.Entry entry = (Map.Entry) i.next();
121: expected.put(entry.getKey(), entry.getValue().getClass()
122: .getName());
123: }
124: ne = envContext.list("");
125: result = new HashMap();
126: while (ne.hasMore()) {
127: NameClassPair pair = (NameClassPair) ne.next();
128: result.put(pair.getName(), pair.getClassName());
129: }
130: assertEquals(expected, result);
131:
132: try {
133: ne.next();
134: fail();
135: } catch (NoSuchElementException e) {
136: // ok
137: }
138: try {
139: ne.nextElement();
140: fail();
141: } catch (NoSuchElementException e) {
142: // ok
143: }
144: }
145:
146: public void testListBindings() throws NamingException {
147: NamingEnumeration ne;
148: ne = envContext.listBindings("");
149: int count = 0;
150: while (ne.hasMore()) {
151: count++;
152: Binding pair = (Binding) ne.next();
153: assertTrue(envBinding.containsKey(pair.getName()));
154: if (!(envBinding.get(pair.getName()) instanceof Context)) {
155: assertEquals(pair.getObject(), envBinding.get(pair
156: .getName()));
157: }
158: }
159: assertEquals(envBinding.size(), count);
160:
161: try {
162: ne.next();
163: fail();
164: } catch (NoSuchElementException e) {
165: // ok
166: }
167: try {
168: ne.nextElement();
169: fail();
170: } catch (NoSuchElementException e) {
171: // ok
172: }
173: }
174:
175: public void testSpeed() throws NamingException {
176: Context comp = (Context) initialContext.lookup("java:comp");
177:
178: long start = System.currentTimeMillis();
179: for (int i = 0; i < 1000000; i++) {
180: // initialContext.lookup("java:comp/hello"); // this is sloooow due to scheme resolution
181: // envContext.lookup("hello");
182: comp.lookup("env/hello");
183: }
184:
185: long end = System.currentTimeMillis();
186: System.out.println("lookup(String) milliseconds: "
187: + (end - start));
188: }
189:
190: protected void setUp() throws Exception {
191: super .setUp();
192: System.setProperty("java.naming.factory.initial",
193: GlobalContextManager.class.getName());
194: System.setProperty("java.naming.factory.url.pkgs",
195: "org.apache.geronimo.knaming");
196:
197: LinkRef link = new LinkRef("java:comp/env/hello");
198:
199: Map bindings = new HashMap();
200: bindings.put("env/hello", "Hello");
201: bindings.put("env/world", "Hello World");
202: bindings.put("env/here/there/anywhere", "long name");
203: bindings.put("env/link", link);
204:
205: readOnlyContext = new WritableContext("", bindings,
206: ContextAccess.UNMODIFIABLE);
207:
208: envBinding = new HashMap();
209: envBinding.put("hello", "Hello");
210: envBinding.put("world", "Hello World");
211: envBinding.put("here", readOnlyContext.lookup("env/here"));
212: envBinding.put("link", link);
213:
214: RootContext.setComponentContext(readOnlyContext);
215:
216: Context javaCompContext = new JavaCompContextGBean();
217: Context globalContext = new ImmutableContext(Collections
218: .singletonMap(javaCompContext.getNameInNamespace(),
219: javaCompContext));
220: GlobalContextManager.setGlobalContext(globalContext);
221:
222: initialContext = new InitialContext();
223: compContext = (Context) initialContext.lookup("java:comp");
224: envContext = (Context) initialContext.lookup("java:comp/env");
225:
226: syntax = new Properties();
227: }
228: }
|