~ubuntu-branches/ubuntu/utopic/jetty/utopic-proposed

« back to all changes in this revision

Viewing changes to modules/naming/src/test/java/org/mortbay/naming/java/TestJNDI.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2009-08-09 08:48:10 UTC
  • Revision ID: james.westby@ubuntu.com-20090809084810-k522b97ind2robyd
ImportĀ upstreamĀ versionĀ 6.1.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// ========================================================================
 
15
 
 
16
package org.mortbay.naming.java;
 
17
 
 
18
 
 
19
import java.net.URL;
 
20
import java.net.URLClassLoader;
 
21
import java.util.HashMap;
 
22
import java.util.Hashtable;
 
23
 
 
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;
 
36
 
 
37
import junit.framework.Test;
 
38
import junit.framework.TestCase;
 
39
import junit.framework.TestSuite;
 
40
 
 
41
import org.mortbay.log.Log;
 
42
import org.mortbay.naming.NamingContext;
 
43
 
 
44
 
 
45
 
 
46
public class TestJNDI extends TestCase
 
47
{
 
48
 
 
49
 
 
50
    public static class MyObjectFactory implements ObjectFactory
 
51
    {
 
52
        public static String myString = "xxx";
 
53
 
 
54
        public Object getObjectInstance(Object obj,
 
55
                                        Name name,
 
56
                                        Context nameCtx,
 
57
                                        Hashtable environment)
 
58
            throws Exception
 
59
            {
 
60
                return myString;
 
61
            }
 
62
    }
 
63
 
 
64
    public TestJNDI (String name)
 
65
    {
 
66
        super (name);
 
67
    }
 
68
 
 
69
 
 
70
    public static Test suite ()
 
71
    {
 
72
        return new TestSuite (TestJNDI.class);
 
73
    }
 
74
 
 
75
    public void setUp ()
 
76
        throws Exception
 
77
    {
 
78
    }
 
79
 
 
80
 
 
81
    public void tearDown ()
 
82
        throws Exception
 
83
    {
 
84
    }
 
85
 
 
86
    public void testIt ()
 
87
    throws Exception
 
88
    {
 
89
        try
 
90
        {
 
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);
 
96
 
 
97
            //set the current thread's classloader
 
98
            currentThread.setContextClassLoader(childLoader1);
 
99
 
 
100
            InitialContext initCtxA = new InitialContext();
 
101
            initCtxA.bind ("blah", "123");
 
102
            assertEquals ("123", initCtxA.lookup("blah"));
 
103
 
 
104
 
 
105
 
 
106
 
 
107
            InitialContext initCtx = new InitialContext();
 
108
            Context sub0 = (Context)initCtx.lookup("java:");
 
109
 
 
110
            if(Log.isDebugEnabled())Log.debug("------ Looked up java: --------------");
 
111
 
 
112
            Name n = sub0.getNameParser("").parse("/red/green/");
 
113
 
 
114
 
 
115
            if(Log.isDebugEnabled())Log.debug("get(0)="+n.get(0));
 
116
            if(Log.isDebugEnabled())Log.debug("getPrefix(1)="+n.getPrefix(1));
 
117
            n = n.getSuffix(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));
 
121
            n = n.getSuffix(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));
 
125
            n = n.getSuffix(1);
 
126
            if(Log.isDebugEnabled())Log.debug("getSuffix(1)="+n);
 
127
 
 
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));
 
131
            n = n.getSuffix(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));
 
135
 
 
136
            NamingContext ncontext = (NamingContext)sub0;
 
137
 
 
138
            Name nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue/"));
 
139
            Log.debug(nn.toString());
 
140
            assertEquals (2, nn.size());
 
141
 
 
142
            nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse("/yellow/blue"));
 
143
            Log.debug(nn.toString());
 
144
            assertEquals (2, nn.size());
 
145
 
 
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());
 
150
 
 
151
            nn = ncontext.toCanonicalName(ncontext.getNameParser("").parse(""));
 
152
            Log.debug(nn.toString());
 
153
            assertEquals (0, nn.size());
 
154
 
 
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);
 
160
 
 
161
            try
 
162
            {
 
163
                Context sub1 = sub0.createSubcontext ("comp");
 
164
                fail("Comp should already be bound");
 
165
            }
 
166
            catch (NameAlreadyBoundException e)
 
167
            {
 
168
                //expected exception
 
169
            }
 
170
 
 
171
 
 
172
 
 
173
            //check bindings at comp
 
174
            Context sub1 = (Context)initCtx.lookup("java:comp");
 
175
 
 
176
            Context sub2 = sub1.createSubcontext ("env");
 
177
 
 
178
            initCtx.bind ("java:comp/env/rubbish", "abc");
 
179
            assertEquals ("abc", (String)initCtx.lookup("java:comp/env/rubbish"));
 
180
 
 
181
 
 
182
 
 
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"));
 
187
 
 
188
            //check binding References
 
189
            StringRefAddr addr = new StringRefAddr("blah", "myReferenceable");
 
190
            Reference ref = new Reference (java.lang.String.class.getName(),
 
191
                    addr,
 
192
                    MyObjectFactory.class.getName(),
 
193
                    (String)null);
 
194
 
 
195
            initCtx.bind ("java:comp/env/quatsch", ref);
 
196
            assertEquals (MyObjectFactory.myString, (String)initCtx.lookup("java:comp/env/quatsch"));
 
197
 
 
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"));
 
202
 
 
203
 
 
204
 
 
205
 
 
206
            //change the current thread's classloader to check distinct naming                                              
 
207
            currentThread.setContextClassLoader(childLoader2);
 
208
 
 
209
            Context otherSub1 = (Context)initCtx.lookup("java:comp");
 
210
            assertTrue (!(sub1 == otherSub1));
 
211
            try
 
212
            {
 
213
                initCtx.lookup("java:comp/env/rubbish");
 
214
            }
 
215
            catch (NameNotFoundException e)
 
216
            {
 
217
                //expected
 
218
            }
 
219
 
 
220
 
 
221
            //put the thread's classloader back
 
222
            currentThread.setContextClassLoader(childLoader1);
 
223
 
 
224
            //test rebind with existing binding
 
225
            initCtx.rebind("java:comp/env/rubbish", "xyz");
 
226
            assertEquals ("xyz", initCtx.lookup("java:comp/env/rubbish"));
 
227
 
 
228
            //test rebind with no existing binding
 
229
            initCtx.rebind ("java:comp/env/mullheim", "hij");
 
230
            assertEquals ("hij", initCtx.lookup("java:comp/env/mullheim"));
 
231
 
 
232
            //test that the other bindings are already there       
 
233
            assertEquals ("xyz", (String)initCtx.lookup("java:comp/env/poubelle"));
 
234
 
 
235
            //test java:/comp/env/stuff
 
236
            assertEquals ("xyz", (String)initCtx.lookup("java:/comp/env/poubelle/"));
 
237
 
 
238
            //test list Names
 
239
            NamingEnumeration nenum = initCtx.list ("java:comp/env");
 
240
            HashMap results = new HashMap();
 
241
            while (nenum.hasMore())
 
242
            {
 
243
                NameClassPair ncp = (NameClassPair)nenum.next();
 
244
                results.put (ncp.getName(), ncp.getClassName());
 
245
            }
 
246
 
 
247
            assertEquals (4, results.size());
 
248
 
 
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"));
 
253
 
 
254
            //test list Bindings
 
255
            NamingEnumeration benum = initCtx.list("java:comp/env");
 
256
            assertEquals (4, results.size());
 
257
 
 
258
            //test NameInNamespace
 
259
            assertEquals ("comp/env", sub2.getNameInNamespace());
 
260
 
 
261
            //test close does nothing
 
262
            Context closeCtx = (Context)initCtx.lookup("java:comp/env");
 
263
            closeCtx.close();
 
264
 
 
265
 
 
266
            //test what happens when you close an initial context
 
267
            InitialContext closeInit = new InitialContext();
 
268
            closeInit.close();
 
269
 
 
270
 
 
271
 
 
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"));
 
277
            try
 
278
            {
 
279
                ectx.bind("crud2", "xxx2");
 
280
            }
 
281
            catch (NamingException ne)
 
282
            {
 
283
                //expected failure to modify immutable context
 
284
            }
 
285
 
 
286
            //test what happens when you close an initial context that was used
 
287
            initCtx.close();   
 
288
        }
 
289
        finally
 
290
        {
 
291
            InitialContext ic = new InitialContext();
 
292
            Context comp = (Context)ic.lookup("java:comp");
 
293
            comp.destroySubcontext("env");
 
294
        }
 
295
    } 
 
296
 
 
297
}