1 2 17 18 package org.springframework.jndi; 19 20 import javax.naming.NamingException ; 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 32 public class JndiObjectFactoryBeanTests extends TestCase { 33 34 public void testNoJndiName() throws NamingException { 35 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 36 try { 37 jof.afterPropertiesSet(); 38 fail("Should have thrown IllegalArgumentException"); 39 } 40 catch (IllegalArgumentException ex) { 41 } 42 } 43 44 public void testLookupWithFullNameAndResourceRefTrue() throws Exception { 45 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 46 Object o = new Object (); 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 { 55 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 56 Object o = new Object (); 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 { 65 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 66 Object o = new Object (); 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 { 75 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 76 Object o = new Object (); 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 { 85 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 86 Object o = new Object (); 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 { 95 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 96 Object o = new Object (); 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 ex) { 105 } 107 } 108 109 public void testLookupWithArbitraryNameAndResourceRefFalse() throws Exception { 110 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 111 Object o = new Object (); 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 { 120 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 121 String s = new String (); 122 jof.setJndiTemplate(new ExpectedLookupTemplate("foo", s)); 123 jof.setJndiName("foo"); 124 jof.setExpectedType(String .class); 125 jof.afterPropertiesSet(); 126 assertTrue(jof.getObject() == s); 127 } 128 129 public void testLookupWithExpectedTypeAndNoMatch() throws Exception { 130 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 131 Object o = new Object (); 132 jof.setJndiTemplate(new ExpectedLookupTemplate("foo", o)); 133 jof.setJndiName("foo"); 134 jof.setExpectedType(String .class); 135 try { 136 jof.afterPropertiesSet(); 137 fail("Should have thrown NamingException"); 138 } 139 catch (NamingException ex) { 140 assertTrue(ex.getMessage().indexOf("java.lang.String") != -1); 141 } 142 } 143 144 public void testLookupWithProxyInterface() throws Exception { 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 { 159 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 160 final TestBean tb = new TestBean(); 161 jof.setJndiTemplate(new JndiTemplate() { 162 public Object lookup(String 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 { 184 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 185 final TestBean tb = new TestBean(); 186 jof.setJndiTemplate(new JndiTemplate() { 187 public Object lookup(String 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 { 211 JndiObjectFactoryBean jof = new JndiObjectFactoryBean(); 212 final TestBean tb = new TestBean(); 213 jof.setJndiTemplate(new JndiTemplate() { 214 public Object lookup(String 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 { 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 ex) { 250 } 252 } 253 254 public void testNotCacheWithoutProxyInterface() throws NamingException { 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 ex) { 263 } 265 } 266 267 public void testLookupWithProxyInterfaceAndExpectedTypeAndMatch() throws Exception { 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 { 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 ex) { 294 assertTrue(ex.getMessage().indexOf("org.springframework.beans.DerivedTestBean") != -1); 295 } 296 } 297 298 } 299 | Popular Tags |