code style improvements.
[utils] / test / enterprise / src / test / java / org / wamblee / support / 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.support.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
29 public class StubInitiaContextFactoryTest {
30
31     private static final String JNDI_NAME = "a/b";
32
33     @Before
34     @After
35     public void setUp() {
36         StubInitialContextFactory.unregister();
37     }
38
39     @Test(expected = NamingException.class)
40     public void testLookupNotRegistered() throws Exception {
41         InitialContext ctx = new InitialContext();
42         ctx.bind(JNDI_NAME, "hallo");
43     }
44     
45     // String based lookups.
46
47     @Test
48     public void testLookup() throws Exception {
49         StubInitialContextFactory.register();
50
51         InitialContext ctx = new InitialContext();
52         ctx.bind(JNDI_NAME, "hallo");
53
54         ctx = new InitialContext();
55         Object obj = ctx.lookup(JNDI_NAME);
56
57         assertEquals("hallo", obj);
58     }
59     
60     @Test(expected = NameNotFoundException.class)
61     public void testUnbind() throws Exception { 
62         testLookup(); 
63         InitialContext ctx = new InitialContext(); 
64         ctx.unbind(JNDI_NAME);
65         ctx = new InitialContext();
66         ctx.lookup(JNDI_NAME);
67         
68     }
69
70     @Test(expected = NameNotFoundException.class)
71     public void testLookupFails() throws Exception {
72         StubInitialContextFactory.register();
73
74         InitialContext ctx = new InitialContext();
75         Object obj = ctx.lookup(JNDI_NAME);
76     }
77     
78     // Name based lookups
79     
80     @Test
81     public void testLookupName() throws Exception {
82         StubInitialContextFactory.register();
83
84         InitialContext ctx = new InitialContext();
85         ctx.bind(new CompositeName(JNDI_NAME), "hallo");
86
87         ctx = new InitialContext();
88         Object obj = ctx.lookup(new CompositeName(JNDI_NAME));
89
90         assertEquals("hallo", obj);
91     }
92     
93     @Test(expected = NameNotFoundException.class)
94     public void testUnbindName() throws Exception { 
95         testLookup(); 
96         InitialContext ctx = new InitialContext(); 
97         ctx.unbind(new CompositeName(JNDI_NAME));
98         ctx = new InitialContext();
99         ctx.lookup(new CompositeName(JNDI_NAME));
100     }
101
102     @Test(expected = NameNotFoundException.class)
103     public void testLookupFailsName() throws Exception {
104         StubInitialContextFactory.register();
105
106         InitialContext ctx = new InitialContext();
107         Object obj = ctx.lookup(JNDI_NAME);
108     }
109 }