1
// ========================================================================
2
// $Id: TestJNDI.java 3680 2008-09-21 10:37:13Z janb $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
16
package org.mortbay.naming.java;
20
import java.net.URLClassLoader;
21
import java.util.HashMap;
22
import java.util.Hashtable;
24
import javax.naming.Context;
25
import javax.naming.InitialContext;
26
import javax.naming.LinkRef;
27
import javax.naming.Name;
28
import javax.naming.NameAlreadyBoundException;
29
import javax.naming.NameClassPair;
30
import javax.naming.NameNotFoundException;
31
import javax.naming.NamingEnumeration;
32
import javax.naming.NamingException;
33
import javax.naming.Reference;
34
import javax.naming.StringRefAddr;
35
import javax.naming.spi.ObjectFactory;
37
import junit.framework.Test;
38
import junit.framework.TestCase;
39
import junit.framework.TestSuite;
41
import org.mortbay.log.Log;
42
import org.mortbay.naming.NamingContext;
46
public class TestJNDI extends TestCase
50
public static class MyObjectFactory implements ObjectFactory
52
public static String myString = "xxx";
54
public Object getObjectInstance(Object obj,
57
Hashtable environment)
64
public TestJNDI (String name)
70
public static Test suite ()
72
return new TestSuite (TestJNDI.class);
81
public void tearDown ()
91
//set up some classloaders
92
Thread currentThread = Thread.currentThread();
93
ClassLoader currentLoader = currentThread.getContextClassLoader();
94
ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader);
95
ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader);
97
//set the current thread's classloader
98
currentThread.setContextClassLoader(childLoader1);
100
InitialContext initCtxA = new InitialContext();
101
initCtxA.bind ("blah", "123");
102
assertEquals ("123", initCtxA.lookup("blah"));
107
InitialContext initCtx = new InitialContext();
108
Context sub0 = (Context)initCtx.lookup("java:");
110
if(Log.isDebugEnabled())Log.debug("------ Looked up java: --------------");
112
Name n = sub0.getNameParser("").parse("/red/green/");
115
if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0));
116
if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1));
118
if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n);
119
if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0));
120
if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1));
122
if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n);
123
if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0));
124
if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1));
126
if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n);
128
n = sub0.getNameParser("").parse("pink/purple/");
129
if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0));
130
if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1));
132
if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n);
133
if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0));
134
if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1));
136
NamingContext ncontext = (NamingContext)sub0;
138
Name nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue/"));
139
Log.debug(nn.toString());
140
assertEquals (2, nn.size());
142
nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue"));
143
Log.debug(nn.toString());
144
assertEquals (2, nn.size());
146
nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/"));
147
if(Log.isDebugEnabled())Log.debug("/ parses as: "+nn+" with size="+nn.size());
148
Log.debug(nn.toString());
149
assertEquals (1, nn.size());
151
nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse(""));
152
Log.debug(nn.toString());
153
assertEquals (0, nn.size());
155
Context fee = ncontext.createSubcontext("fee");
156
fee.bind ("fi", "88");
157
assertEquals("88", initCtxA.lookup("java:/fee/fi"));
158
assertEquals("88", initCtxA.lookup("java:/fee/fi/"));
159
assertTrue (initCtxA.lookup("java:/fee/") instanceof javax.naming.Context);
163
Context sub1 = sub0.createSubcontext ("comp");
164
fail("Comp should already be bound");
166
catch (NameAlreadyBoundException e)
173
//check bindings at comp
174
Context sub1 = (Context)initCtx.lookup("java:comp");
176
Context sub2 = sub1.createSubcontext ("env");
178
initCtx.bind ("java:comp/env/rubbish", "abc");
179
assertEquals ("abc", (String)initCtx.lookup("java:comp/env/rubbish"));
183
//check binding LinkRefs
184
LinkRef link = new LinkRef ("java:comp/env/rubbish");
185
initCtx.bind ("java:comp/env/poubelle", link);
186
assertEquals ("abc", (String)initCtx.lookup("java:comp/env/poubelle"));
188
//check binding References
189
StringRefAddr addr = new StringRefAddr("blah", "myReferenceable");
190
Reference ref = new Reference (java.lang.String.class.getName(),
192
MyObjectFactory.class.getName(),
195
initCtx.bind ("java:comp/env/quatsch", ref);
196
assertEquals (MyObjectFactory.myString, (String)initCtx.lookup("java:comp/env/quatsch"));
198
//test binding something at java:
199
Context sub3 = initCtx.createSubcontext("java:zero");
200
initCtx.bind ("java:zero/one", "ONE");
201
assertEquals ("ONE", initCtx.lookup("java:zero/one"));
206
//change the current thread's classloader to check distinct naming
207
currentThread.setContextClassLoader(childLoader2);
209
Context otherSub1 = (Context)initCtx.lookup("java:comp");
210
assertTrue (!(sub1 == otherSub1));
213
initCtx.lookup("java:comp/env/rubbish");
215
catch (NameNotFoundException e)
221
//put the thread's classloader back
222
currentThread.setContextClassLoader(childLoader1);
224
//test rebind with existing binding
225
initCtx.rebind("java:comp/env/rubbish", "xyz");
226
assertEquals ("xyz", initCtx.lookup("java:comp/env/rubbish"));
228
//test rebind with no existing binding
229
initCtx.rebind ("java:comp/env/mullheim", "hij");
230
assertEquals ("hij", initCtx.lookup("java:comp/env/mullheim"));
232
//test that the other bindings are already there
233
assertEquals ("xyz", (String)initCtx.lookup("java:comp/env/poubelle"));
235
//test java:/comp/env/stuff
236
assertEquals ("xyz", (String)initCtx.lookup("java:/comp/env/poubelle/"));
239
NamingEnumeration nenum = initCtx.list ("java:comp/env");
240
HashMap results = new HashMap();
241
while (nenum.hasMore())
243
NameClassPair ncp = (NameClassPair)nenum.next();
244
results.put (ncp.getName(), ncp.getClassName());
247
assertEquals (4, results.size());
249
assertEquals ("java.lang.String", (String)results.get("rubbish"));
250
assertEquals ("javax.naming.LinkRef", (String)results.get("poubelle"));
251
assertEquals ("java.lang.String", (String)results.get("mullheim"));
252
assertEquals ("javax.naming.Reference", (String)results.get("quatsch"));
255
NamingEnumeration benum = initCtx.list("java:comp/env");
256
assertEquals (4, results.size());
258
//test NameInNamespace
259
assertEquals ("comp/env", sub2.getNameInNamespace());
261
//test close does nothing
262
Context closeCtx = (Context)initCtx.lookup("java:comp/env");
266
//test what happens when you close an initial context
267
InitialContext closeInit = new InitialContext();
272
//check locking the context
273
Context ectx = (Context)initCtx.lookup("java:comp");
274
ectx.bind("crud", "xxx");
275
ectx.addToEnvironment("org.mortbay.jndi.immutable", "TRUE");
276
assertEquals ("xxx", (String)initCtx.lookup("java:comp/crud"));
279
ectx.bind("crud2", "xxx2");
281
catch (NamingException ne)
283
//expected failure to modify immutable context
286
//test what happens when you close an initial context that was used
291
InitialContext ic = new InitialContext();
292
Context comp = (Context)ic.lookup("java:comp");
293
comp.destroySubcontext("env");