package rename for test libraries.
[utils] / test / enterprise / src / test / java / org / wamblee / test / jndi / StubInitiaContextFactoryTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.test.jndi;
17
18 import static junit.framework.Assert.*;
19
20 import javax.naming.CompositeName;
21 import javax.naming.InitialContext;
22 import javax.naming.NameNotFoundException;
23 import javax.naming.NamingException;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.test.jndi.StubInitialContextFactory;
29
30 public class StubInitiaContextFactoryTest {
31
32     private static final String JNDI_NAME = "a/b";
33
34     @Before
35     @After
36     public void setUp() {
37         StubInitialContextFactory.unregister();
38     }
39
40     @Test(expected = NamingException.class)
41     public void testLookupNotRegistered() throws Exception {
42         InitialContext ctx = new InitialContext();
43         ctx.bind(JNDI_NAME, "hallo");
44     }
45     
46     // String based lookups.
47
48     @Test
49     public void testLookup() throws Exception {
50         StubInitialContextFactory.register();
51
52         InitialContext ctx = new InitialContext();
53         ctx.bind(JNDI_NAME, "hallo");
54
55         ctx = new InitialContext();
56         Object obj = ctx.lookup(JNDI_NAME);
57
58         assertEquals("hallo", obj);
59     }
60     
61     @Test(expected = NameNotFoundException.class)
62     public void testUnbind() throws Exception { 
63         testLookup(); 
64         InitialContext ctx = new InitialContext(); 
65         ctx.unbind(JNDI_NAME);
66         ctx = new InitialContext();
67         ctx.lookup(JNDI_NAME);
68         
69     }
70
71     @Test(expected = NameNotFoundException.class)
72     public void testLookupFails() throws Exception {
73         StubInitialContextFactory.register();
74
75         InitialContext ctx = new InitialContext();
76         Object obj = ctx.lookup(JNDI_NAME);
77     }
78     
79     // Name based lookups
80     
81     @Test
82     public void testLookupName() throws Exception {
83         StubInitialContextFactory.register();
84
85         InitialContext ctx = new InitialContext();
86         ctx.bind(new CompositeName(JNDI_NAME), "hallo");
87
88         ctx = new InitialContext();
89         Object obj = ctx.lookup(new CompositeName(JNDI_NAME));
90
91         assertEquals("hallo", obj);
92     }
93     
94     @Test(expected = NameNotFoundException.class)
95     public void testUnbindName() throws Exception { 
96         testLookup(); 
97         InitialContext ctx = new InitialContext(); 
98         ctx.unbind(new CompositeName(JNDI_NAME));
99         ctx = new InitialContext();
100         ctx.lookup(new CompositeName(JNDI_NAME));
101     }
102
103     @Test(expected = NameNotFoundException.class)
104     public void testLookupFailsName() throws Exception {
105         StubInitialContextFactory.register();
106
107         InitialContext ctx = new InitialContext();
108         Object obj = ctx.lookup(JNDI_NAME);
109     }
110 }