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.support.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;
29 public class StubInitiaContextFactoryTest {
31 private static final String JNDI_NAME = "a/b";
36 StubInitialContextFactory.unregister();
39 @Test(expected = NamingException.class)
40 public void testLookupNotRegistered() throws Exception {
41 InitialContext ctx = new InitialContext();
42 ctx.bind(JNDI_NAME, "hallo");
45 // String based lookups.
48 public void testLookup() throws Exception {
49 StubInitialContextFactory.register();
51 InitialContext ctx = new InitialContext();
52 ctx.bind(JNDI_NAME, "hallo");
54 ctx = new InitialContext();
55 Object obj = ctx.lookup(JNDI_NAME);
57 assertEquals("hallo", obj);
60 @Test(expected = NameNotFoundException.class)
61 public void testUnbind() throws Exception {
63 InitialContext ctx = new InitialContext();
64 ctx.unbind(JNDI_NAME);
65 ctx = new InitialContext();
66 ctx.lookup(JNDI_NAME);
70 @Test(expected = NameNotFoundException.class)
71 public void testLookupFails() throws Exception {
72 StubInitialContextFactory.register();
74 InitialContext ctx = new InitialContext();
75 Object obj = ctx.lookup(JNDI_NAME);
81 public void testLookupName() throws Exception {
82 StubInitialContextFactory.register();
84 InitialContext ctx = new InitialContext();
85 ctx.bind(new CompositeName(JNDI_NAME), "hallo");
87 ctx = new InitialContext();
88 Object obj = ctx.lookup(new CompositeName(JNDI_NAME));
90 assertEquals("hallo", obj);
93 @Test(expected = NameNotFoundException.class)
94 public void testUnbindName() throws Exception {
96 InitialContext ctx = new InitialContext();
97 ctx.unbind(new CompositeName(JNDI_NAME));
98 ctx = new InitialContext();
99 ctx.lookup(new CompositeName(JNDI_NAME));
102 @Test(expected = NameNotFoundException.class)
103 public void testLookupFailsName() throws Exception {
104 StubInitialContextFactory.register();
106 InitialContext ctx = new InitialContext();
107 Object obj = ctx.lookup(JNDI_NAME);