001: // ========================================================================
002: // $Id: TestJNDI.java 478 2006-04-23 22:00:17Z gregw $
003: // Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.naming.java;
017:
018: import java.net.URL;
019: import java.net.URLClassLoader;
020: import java.util.HashMap;
021: import java.util.Hashtable;
022:
023: import javax.naming.Context;
024: import javax.naming.InitialContext;
025: import javax.naming.LinkRef;
026: import javax.naming.Name;
027: import javax.naming.NameAlreadyBoundException;
028: import javax.naming.NameClassPair;
029: import javax.naming.NameNotFoundException;
030: import javax.naming.NamingEnumeration;
031: import javax.naming.NamingException;
032: import javax.naming.Reference;
033: import javax.naming.StringRefAddr;
034: import javax.naming.spi.ObjectFactory;
035:
036: import junit.framework.Test;
037: import junit.framework.TestCase;
038: import junit.framework.TestSuite;
039:
040: import org.mortbay.log.Log;
041: import org.mortbay.naming.NamingContext;
042:
043: public class TestJNDI extends TestCase {
044:
045: public static class MyObjectFactory implements ObjectFactory {
046: public static String myString = "xxx";
047:
048: public Object getObjectInstance(Object obj, Name name,
049: Context nameCtx, Hashtable environment)
050: throws Exception {
051: return myString;
052: }
053: }
054:
055: public TestJNDI(String name) {
056: super (name);
057: }
058:
059: public static Test suite() {
060: return new TestSuite(TestJNDI.class);
061: }
062:
063: public void setUp() throws Exception {
064: }
065:
066: public void tearDown() throws Exception {
067: }
068:
069: public void testIt() throws Exception {
070: //set up some classloaders
071: Thread currentThread = Thread.currentThread();
072: ClassLoader currentLoader = currentThread
073: .getContextClassLoader();
074: ClassLoader childLoader1 = new URLClassLoader(new URL[0],
075: currentLoader);
076: ClassLoader childLoader2 = new URLClassLoader(new URL[0],
077: currentLoader);
078:
079: //set the current thread's classloader
080: currentThread.setContextClassLoader(childLoader1);
081:
082: InitialContext initCtxA = new InitialContext();
083: initCtxA.bind("blah", "123");
084: assertEquals("123", initCtxA.lookup("blah"));
085:
086: InitialContext initCtx = new InitialContext();
087: Context sub0 = (Context) initCtx.lookup("java:");
088:
089: if (Log.isDebugEnabled())
090: Log.debug("------ Looked up java: --------------");
091:
092: Name n = sub0.getNameParser("").parse("/red/green/");
093:
094: if (Log.isDebugEnabled())
095: Log.debug("get(0)=" + n.get(0));
096: if (Log.isDebugEnabled())
097: Log.debug("getPrefix(1)=" + n.getPrefix(1));
098: n = n.getSuffix(1);
099: if (Log.isDebugEnabled())
100: Log.debug("getSuffix(1)=" + n);
101: if (Log.isDebugEnabled())
102: Log.debug("get(0)=" + n.get(0));
103: if (Log.isDebugEnabled())
104: Log.debug("getPrefix(1)=" + n.getPrefix(1));
105: n = n.getSuffix(1);
106: if (Log.isDebugEnabled())
107: Log.debug("getSuffix(1)=" + n);
108: if (Log.isDebugEnabled())
109: Log.debug("get(0)=" + n.get(0));
110: if (Log.isDebugEnabled())
111: Log.debug("getPrefix(1)=" + n.getPrefix(1));
112: n = n.getSuffix(1);
113: if (Log.isDebugEnabled())
114: Log.debug("getSuffix(1)=" + n);
115:
116: n = sub0.getNameParser("").parse("pink/purple/");
117: if (Log.isDebugEnabled())
118: Log.debug("get(0)=" + n.get(0));
119: if (Log.isDebugEnabled())
120: Log.debug("getPrefix(1)=" + n.getPrefix(1));
121: n = n.getSuffix(1);
122: if (Log.isDebugEnabled())
123: Log.debug("getSuffix(1)=" + n);
124: if (Log.isDebugEnabled())
125: Log.debug("get(0)=" + n.get(0));
126: if (Log.isDebugEnabled())
127: Log.debug("getPrefix(1)=" + n.getPrefix(1));
128:
129: NamingContext ncontext = (NamingContext) sub0;
130:
131: Name nn = ncontext.toCanonicalName(ncontext.getNameParser("")
132: .parse("/yellow/blue/"));
133: Log.debug(nn.toString());
134: assertEquals(2, nn.size());
135:
136: nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse(
137: "/yellow/blue"));
138: Log.debug(nn.toString());
139: assertEquals(2, nn.size());
140:
141: nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse(
142: "/"));
143: if (Log.isDebugEnabled())
144: Log.debug("/ parses as: " + nn + " with size=" + nn.size());
145: Log.debug(nn.toString());
146: assertEquals(1, nn.size());
147:
148: nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse(
149: ""));
150: Log.debug(nn.toString());
151: assertEquals(0, nn.size());
152:
153: Context fee = ncontext.createSubcontext("fee");
154: fee.bind("fi", "88");
155: assertEquals("88", initCtxA.lookup("java:/fee/fi"));
156: assertEquals("88", initCtxA.lookup("java:/fee/fi/"));
157: assertTrue(initCtxA.lookup("java:/fee/") instanceof javax.naming.Context);
158:
159: try {
160: Context sub1 = sub0.createSubcontext("comp");
161: fail("Comp should already be bound");
162: } catch (NameAlreadyBoundException e) {
163: //expected exception
164: }
165:
166: //check bindings at comp
167: Context sub1 = (Context) initCtx.lookup("java:comp");
168:
169: Context sub2 = sub1.createSubcontext("env");
170:
171: initCtx.bind("java:comp/env/rubbish", "abc");
172: assertEquals("abc", (String) initCtx
173: .lookup("java:comp/env/rubbish"));
174:
175: //check binding LinkRefs
176: LinkRef link = new LinkRef("java:comp/env/rubbish");
177: initCtx.bind("java:comp/env/poubelle", link);
178: assertEquals("abc", (String) initCtx
179: .lookup("java:comp/env/poubelle"));
180:
181: //check binding References
182: StringRefAddr addr = new StringRefAddr("blah",
183: "myReferenceable");
184: Reference ref = new Reference(java.lang.String.class.getName(),
185: addr, MyObjectFactory.class.getName(), (String) null);
186:
187: initCtx.bind("java:comp/env/quatsch", ref);
188: assertEquals(MyObjectFactory.myString, (String) initCtx
189: .lookup("java:comp/env/quatsch"));
190:
191: //test binding something at java:
192: Context sub3 = initCtx.createSubcontext("java:zero");
193: initCtx.bind("java:zero/one", "ONE");
194: assertEquals("ONE", initCtx.lookup("java:zero/one"));
195:
196: //change the current thread's classloader to check distinct naming
197: currentThread.setContextClassLoader(childLoader2);
198:
199: Context otherSub1 = (Context) initCtx.lookup("java:comp");
200: assertTrue(!(sub1 == otherSub1));
201: try {
202: initCtx.lookup("java:comp/env/rubbish");
203: } catch (NameNotFoundException e) {
204: //expected
205: }
206:
207: //put the thread's classloader back
208: currentThread.setContextClassLoader(childLoader1);
209:
210: //test rebind with existing binding
211: initCtx.rebind("java:comp/env/rubbish", "xyz");
212: assertEquals("xyz", initCtx.lookup("java:comp/env/rubbish"));
213:
214: //test rebind with no existing binding
215: initCtx.rebind("java:comp/env/mullheim", "hij");
216: assertEquals("hij", initCtx.lookup("java:comp/env/mullheim"));
217:
218: //test that the other bindings are already there
219: assertEquals("xyz", (String) initCtx
220: .lookup("java:comp/env/poubelle"));
221:
222: //test java:/comp/env/stuff
223: assertEquals("xyz", (String) initCtx
224: .lookup("java:/comp/env/poubelle/"));
225:
226: //test list Names
227: NamingEnumeration nenum = initCtx.list("java:comp/env");
228: HashMap results = new HashMap();
229: while (nenum.hasMore()) {
230: NameClassPair ncp = (NameClassPair) nenum.next();
231: results.put(ncp.getName(), ncp.getClassName());
232: }
233:
234: assertEquals(4, results.size());
235:
236: assertEquals("java.lang.String", (String) results
237: .get("rubbish"));
238: assertEquals("javax.naming.LinkRef", (String) results
239: .get("poubelle"));
240: assertEquals("java.lang.String", (String) results
241: .get("mullheim"));
242: assertEquals("javax.naming.Reference", (String) results
243: .get("quatsch"));
244:
245: //test list Bindings
246: NamingEnumeration benum = initCtx.list("java:comp/env");
247: assertEquals(4, results.size());
248:
249: //test NameInNamespace
250: assertEquals("comp/env", sub2.getNameInNamespace());
251:
252: //test close does nothing
253: Context closeCtx = (Context) initCtx.lookup("java:comp/env");
254: closeCtx.close();
255:
256: //test what happens when you close an initial context
257: InitialContext closeInit = new InitialContext();
258: closeInit.close();
259:
260: //check locking the context
261: Context ectx = (Context) initCtx.lookup("java:comp");
262: ectx.bind("crud", "xxx");
263: ectx.addToEnvironment("org.mortbay.jndi.immutable", "TRUE");
264: assertEquals("xxx", (String) initCtx.lookup("java:comp/crud"));
265: try {
266: ectx.bind("crud2", "xxx2");
267: } catch (NamingException ne) {
268: //expected failure to modify immutable context
269: }
270:
271: //test what happens when you close an initial context that was used
272: initCtx.close();
273:
274: }
275:
276: }
|