2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.test.jndi;
18 import static junit.framework.Assert.*;
20 import javax.naming.CompositeName;
21 import javax.naming.InitialContext;
22 import javax.naming.NameNotFoundException;
23 import javax.naming.NamingException;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.test.jndi.StubInitialContextFactory;
30 public class StubInitiaContextFactoryTest {
32 private static final String JNDI_NAME = "a/b";
37 StubInitialContextFactory.unregister();
40 @Test(expected = NamingException.class)
41 public void testLookupNotRegistered() throws Exception {
42 InitialContext ctx = new InitialContext();
43 ctx.bind(JNDI_NAME, "hallo");
46 // String based lookups.
49 public void testLookup() throws Exception {
50 StubInitialContextFactory.register();
52 InitialContext ctx = new InitialContext();
53 ctx.bind(JNDI_NAME, "hallo");
55 ctx = new InitialContext();
56 Object obj = ctx.lookup(JNDI_NAME);
58 assertEquals("hallo", obj);
61 @Test(expected = NameNotFoundException.class)
62 public void testUnbind() throws Exception {
64 InitialContext ctx = new InitialContext();
65 ctx.unbind(JNDI_NAME);
66 ctx = new InitialContext();
67 ctx.lookup(JNDI_NAME);
71 @Test(expected = NameNotFoundException.class)
72 public void testLookupFails() throws Exception {
73 StubInitialContextFactory.register();
75 InitialContext ctx = new InitialContext();
76 Object obj = ctx.lookup(JNDI_NAME);
82 public void testLookupName() throws Exception {
83 StubInitialContextFactory.register();
85 InitialContext ctx = new InitialContext();
86 ctx.bind(new CompositeName(JNDI_NAME), "hallo");
88 ctx = new InitialContext();
89 Object obj = ctx.lookup(new CompositeName(JNDI_NAME));
91 assertEquals("hallo", obj);
94 @Test(expected = NameNotFoundException.class)
95 public void testUnbindName() throws Exception {
97 InitialContext ctx = new InitialContext();
98 ctx.unbind(new CompositeName(JNDI_NAME));
99 ctx = new InitialContext();
100 ctx.lookup(new CompositeName(JNDI_NAME));
103 @Test(expected = NameNotFoundException.class)
104 public void testLookupFailsName() throws Exception {
105 StubInitialContextFactory.register();
107 InitialContext ctx = new InitialContext();
108 Object obj = ctx.lookup(JNDI_NAME);