KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jndi > JndiTemplateTests


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.jndi;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.NamingException JavaDoc;
21
22 import junit.framework.TestCase;
23 import org.easymock.MockControl;
24
25 /**
26  * @author Rod Johnson
27  * @author Juergen Hoeller
28  * @since 08.07.2003
29  */

30 public class JndiTemplateTests extends TestCase {
31
32     public void testLookupSucceeds() throws Exception JavaDoc {
33         Object JavaDoc o = new Object JavaDoc();
34         String JavaDoc name = "foo";
35         MockControl mc = MockControl.createControl(Context JavaDoc.class);
36         final Context JavaDoc mock = (Context JavaDoc) mc.getMock();
37         mock.lookup(name);
38         mc.setReturnValue(o);
39         mock.close();
40         mc.setVoidCallable(1);
41         mc.replay();
42
43         JndiTemplate jt = new JndiTemplate() {
44             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
45                 return mock;
46             }
47         };
48
49         Object JavaDoc o2 = jt.lookup(name);
50         assertEquals(o, o2);
51         mc.verify();
52     }
53     
54     public void testLookupFails() throws Exception JavaDoc {
55         NamingException JavaDoc ne = new NamingException JavaDoc();
56         String JavaDoc name = "foo";
57         MockControl mc = MockControl.createControl(Context JavaDoc.class);
58         final Context JavaDoc mock = (Context JavaDoc) mc.getMock();
59         mock.lookup(name);
60         mc.setThrowable(ne);
61         mock.close();
62         mc.setVoidCallable(1);
63         mc.replay();
64
65         JndiTemplate jt = new JndiTemplate() {
66             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
67                 return mock;
68             }
69         };
70
71         try {
72             jt.lookup(name);
73             fail("Should have thrown NamingException");
74         }
75         catch (NamingException JavaDoc ex) {
76             // Ok
77
}
78         mc.verify();
79     }
80     
81     public void testBind() throws Exception JavaDoc {
82         Object JavaDoc o = new Object JavaDoc();
83         String JavaDoc name = "foo";
84         MockControl mc = MockControl.createControl(Context JavaDoc.class);
85         final Context JavaDoc mock = (Context JavaDoc) mc.getMock();
86         mock.bind(name, o);
87         mc.setVoidCallable(1);
88         mock.close();
89         mc.setVoidCallable(1);
90         mc.replay();
91         
92         JndiTemplate jt = new JndiTemplate() {
93             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
94                 return mock;
95             }
96         };
97         
98         jt.bind(name, o);
99         mc.verify();
100     }
101     
102     public void testRebind() throws Exception JavaDoc {
103         Object JavaDoc o = new Object JavaDoc();
104         String JavaDoc name = "foo";
105         MockControl mc = MockControl.createControl(Context JavaDoc.class);
106         final Context JavaDoc mock = (Context JavaDoc) mc.getMock();
107         mock.rebind(name, o);
108         mc.setVoidCallable(1);
109         mock.close();
110         mc.setVoidCallable(1);
111         mc.replay();
112
113         JndiTemplate jt = new JndiTemplate() {
114             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
115                 return mock;
116             }
117         };
118
119         jt.rebind(name, o);
120         mc.verify();
121     }
122
123     public void testUnbind() throws Exception JavaDoc {
124         String JavaDoc name = "something";
125         MockControl mc = MockControl.createControl(Context JavaDoc.class);
126         final Context JavaDoc mock = (Context JavaDoc) mc.getMock();
127         mock.unbind(name);
128         mc.setVoidCallable(1);
129         mock.close();
130         mc.setVoidCallable(1);
131         mc.replay();
132     
133         JndiTemplate jt = new JndiTemplate() {
134             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
135                 return mock;
136             }
137         };
138     
139         jt.unbind(name);
140         mc.verify();
141     }
142
143 }
144
Popular Tags