KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /*
3  * Copyright 2002-2005 the original author or authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.springframework.jndi;
19
20 import javax.naming.NamingException JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.springframework.beans.DerivedTestBean;
25 import org.springframework.beans.ITestBean;
26 import org.springframework.beans.TestBean;
27 import org.springframework.mock.jndi.ExpectedLookupTemplate;
28
29 /**
30  * @author Rod Johnson
31  */

32 public class JndiObjectFactoryBeanTests extends TestCase {
33
34     public void testNoJndiName() throws NamingException JavaDoc {
35         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
36         try {
37             jof.afterPropertiesSet();
38             fail("Should have thrown IllegalArgumentException");
39         }
40         catch (IllegalArgumentException JavaDoc ex) {
41         }
42     }
43     
44     public void testLookupWithFullNameAndResourceRefTrue() throws Exception JavaDoc {
45         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
46         Object JavaDoc o = new Object JavaDoc();
47         jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
48         jof.setJndiName("java:comp/env/foo");
49         jof.setResourceRef(true);
50         jof.afterPropertiesSet();
51         assertTrue(jof.getObject() == o);
52     }
53
54     public void testLookupWithFullNameAndResourceRefFalse() throws Exception JavaDoc {
55         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
56         Object JavaDoc o = new Object JavaDoc();
57         jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
58         jof.setJndiName("java:comp/env/foo");
59         jof.setResourceRef(false);
60         jof.afterPropertiesSet();
61         assertTrue(jof.getObject() == o);
62     }
63
64     public void testLookupWithSchemeNameAndResourceRefTrue() throws Exception JavaDoc {
65         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
66         Object JavaDoc o = new Object JavaDoc();
67         jof.setJndiTemplate(new ExpectedLookupTemplate("java:foo", o));
68         jof.setJndiName("java:foo");
69         jof.setResourceRef(true);
70         jof.afterPropertiesSet();
71         assertTrue(jof.getObject() == o);
72     }
73
74     public void testLookupWithSchemeNameAndResourceRefFalse() throws Exception JavaDoc {
75         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
76         Object JavaDoc o = new Object JavaDoc();
77         jof.setJndiTemplate(new ExpectedLookupTemplate("java:foo", o));
78         jof.setJndiName("java:foo");
79         jof.setResourceRef(false);
80         jof.afterPropertiesSet();
81         assertTrue(jof.getObject() == o);
82     }
83
84     public void testLookupWithShortNameAndResourceRefTrue() throws Exception JavaDoc {
85         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
86         Object JavaDoc o = new Object JavaDoc();
87         jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
88         jof.setJndiName("foo");
89         jof.setResourceRef(true);
90         jof.afterPropertiesSet();
91         assertTrue(jof.getObject() == o);
92     }
93
94     public void testLookupWithShortNameAndResourceRefFalse() throws Exception JavaDoc {
95         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
96         Object JavaDoc o = new Object JavaDoc();
97         jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
98         jof.setJndiName("foo");
99         jof.setResourceRef(false);
100         try {
101             jof.afterPropertiesSet();
102             fail("Should have thrown NamingException");
103         }
104         catch (NamingException JavaDoc ex) {
105             // expected
106
}
107     }
108
109     public void testLookupWithArbitraryNameAndResourceRefFalse() throws Exception JavaDoc {
110         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
111         Object JavaDoc o = new Object JavaDoc();
112         jof.setJndiTemplate(new ExpectedLookupTemplate("foo", o));
113         jof.setJndiName("foo");
114         jof.setResourceRef(false);
115         jof.afterPropertiesSet();
116         assertTrue(jof.getObject() == o);
117     }
118
119     public void testLookupWithExpectedTypeAndMatch() throws Exception JavaDoc {
120         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
121         String JavaDoc s = new String JavaDoc();
122         jof.setJndiTemplate(new ExpectedLookupTemplate("foo", s));
123         jof.setJndiName("foo");
124         jof.setExpectedType(String JavaDoc.class);
125         jof.afterPropertiesSet();
126         assertTrue(jof.getObject() == s);
127     }
128
129     public void testLookupWithExpectedTypeAndNoMatch() throws Exception JavaDoc {
130         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
131         Object JavaDoc o = new Object JavaDoc();
132         jof.setJndiTemplate(new ExpectedLookupTemplate("foo", o));
133         jof.setJndiName("foo");
134         jof.setExpectedType(String JavaDoc.class);
135         try {
136             jof.afterPropertiesSet();
137             fail("Should have thrown NamingException");
138         }
139         catch (NamingException JavaDoc ex) {
140             assertTrue(ex.getMessage().indexOf("java.lang.String") != -1);
141         }
142     }
143
144     public void testLookupWithProxyInterface() throws Exception JavaDoc {
145         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
146         TestBean tb = new TestBean();
147         jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
148         jof.setJndiName("foo");
149         jof.setProxyInterface(ITestBean.class);
150         jof.afterPropertiesSet();
151         assertTrue(jof.getObject() instanceof ITestBean);
152         ITestBean proxy = (ITestBean) jof.getObject();
153         assertEquals(0, tb.getAge());
154         proxy.setAge(99);
155         assertEquals(99, tb.getAge());
156     }
157
158     public void testLookupWithProxyInterfaceAndLazyLookup() throws Exception JavaDoc {
159         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
160         final TestBean tb = new TestBean();
161         jof.setJndiTemplate(new JndiTemplate() {
162             public Object JavaDoc lookup(String JavaDoc name) {
163                 if ("foo".equals(name)) {
164                     tb.setName("tb");
165                     return tb;
166                 }
167                 return null;
168             }
169         });
170         jof.setJndiName("foo");
171         jof.setProxyInterface(ITestBean.class);
172         jof.setLookupOnStartup(false);
173         jof.afterPropertiesSet();
174         assertTrue(jof.getObject() instanceof ITestBean);
175         ITestBean proxy = (ITestBean) jof.getObject();
176         assertNull(tb.getName());
177         assertEquals(0, tb.getAge());
178         proxy.setAge(99);
179         assertEquals("tb", tb.getName());
180         assertEquals(99, tb.getAge());
181     }
182
183     public void testLookupWithProxyInterfaceWithNotCache() throws Exception JavaDoc {
184         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
185         final TestBean tb = new TestBean();
186         jof.setJndiTemplate(new JndiTemplate() {
187             public Object JavaDoc lookup(String JavaDoc name) {
188                 if ("foo".equals(name)) {
189                     tb.setName("tb");
190                     tb.setAge(tb.getAge() + 1);
191                     return tb;
192                 }
193                 return null;
194             }
195         });
196         jof.setJndiName("foo");
197         jof.setProxyInterface(ITestBean.class);
198         jof.setCache(false);
199         jof.afterPropertiesSet();
200         assertTrue(jof.getObject() instanceof ITestBean);
201         ITestBean proxy = (ITestBean) jof.getObject();
202         assertEquals("tb", tb.getName());
203         assertEquals(1, tb.getAge());
204         proxy.returnsThis();
205         assertEquals(2, tb.getAge());
206         proxy.haveBirthday();
207         assertEquals(4, tb.getAge());
208     }
209
210     public void testLookupWithProxyInterfaceWithLazyLookupAndNotCache() throws Exception JavaDoc {
211         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
212         final TestBean tb = new TestBean();
213         jof.setJndiTemplate(new JndiTemplate() {
214             public Object JavaDoc lookup(String JavaDoc name) {
215                 if ("foo".equals(name)) {
216                     tb.setName("tb");
217                     tb.setAge(tb.getAge() + 1);
218                     return tb;
219                 }
220                 return null;
221             }
222         });
223         jof.setJndiName("foo");
224         jof.setProxyInterface(ITestBean.class);
225         jof.setLookupOnStartup(false);
226         jof.setCache(false);
227         jof.afterPropertiesSet();
228         assertTrue(jof.getObject() instanceof ITestBean);
229         ITestBean proxy = (ITestBean) jof.getObject();
230         assertNull(tb.getName());
231         assertEquals(0, tb.getAge());
232         proxy.returnsThis();
233         assertEquals("tb", tb.getName());
234         assertEquals(1, tb.getAge());
235         proxy.returnsThis();
236         assertEquals(2, tb.getAge());
237         proxy.haveBirthday();
238         assertEquals(4, tb.getAge());
239     }
240
241     public void testLazyLookupWithoutProxyInterface() throws NamingException JavaDoc {
242         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
243         jof.setJndiName("foo");
244         jof.setLookupOnStartup(false);
245         try {
246             jof.afterPropertiesSet();
247             fail("Should have thrown IllegalArgumentException");
248         }
249         catch (IllegalArgumentException JavaDoc ex) {
250             // expected
251
}
252     }
253
254     public void testNotCacheWithoutProxyInterface() throws NamingException JavaDoc {
255         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
256         jof.setJndiName("foo");
257         jof.setCache(false);
258         try {
259             jof.afterPropertiesSet();
260             fail("Should have thrown IllegalArgumentException");
261         }
262         catch (IllegalArgumentException JavaDoc ex) {
263             // expected
264
}
265     }
266
267     public void testLookupWithProxyInterfaceAndExpectedTypeAndMatch() throws Exception JavaDoc {
268         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
269         TestBean tb = new TestBean();
270         jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
271         jof.setJndiName("foo");
272         jof.setExpectedType(TestBean.class);
273         jof.setProxyInterface(ITestBean.class);
274         jof.afterPropertiesSet();
275         assertTrue(jof.getObject() instanceof ITestBean);
276         ITestBean proxy = (ITestBean) jof.getObject();
277         assertEquals(0, tb.getAge());
278         proxy.setAge(99);
279         assertEquals(99, tb.getAge());
280     }
281
282     public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() throws Exception JavaDoc {
283         JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
284         TestBean tb = new TestBean();
285         jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
286         jof.setJndiName("foo");
287         jof.setExpectedType(DerivedTestBean.class);
288         jof.setProxyInterface(ITestBean.class);
289         try {
290             jof.afterPropertiesSet();
291             fail("Should have thrown NamingException");
292         }
293         catch (NamingException JavaDoc ex) {
294             assertTrue(ex.getMessage().indexOf("org.springframework.beans.DerivedTestBean") != -1);
295         }
296     }
297
298 }
299
Popular Tags