1 16 17 package org.springframework.orm.hibernate3; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Properties ; 29 import java.util.Set ; 30 31 import javax.transaction.TransactionManager ; 32 33 import junit.framework.TestCase; 34 import org.easymock.MockControl; 35 import org.hibernate.Hibernate; 36 import org.hibernate.HibernateException; 37 import org.hibernate.Interceptor; 38 import org.hibernate.SessionFactory; 39 import org.hibernate.cfg.Configuration; 40 import org.hibernate.cfg.Environment; 41 import org.hibernate.cfg.ImprovedNamingStrategy; 42 import org.hibernate.cfg.Mappings; 43 import org.hibernate.cfg.NamingStrategy; 44 import org.hibernate.connection.UserSuppliedConnectionProvider; 45 import org.hibernate.engine.FilterDefinition; 46 import org.hibernate.mapping.TypeDef; 47 48 import org.springframework.beans.factory.xml.XmlBeanFactory; 49 import org.springframework.core.io.ClassPathResource; 50 import org.springframework.core.io.FileSystemResource; 51 import org.springframework.core.io.Resource; 52 import org.springframework.jdbc.datasource.DriverManagerDataSource; 53 54 58 public class LocalSessionFactoryBeanTests extends TestCase { 59 60 public void testLocalSessionFactoryBeanWithDataSource() throws Exception { 61 final DriverManagerDataSource ds = new DriverManagerDataSource(); 62 final List invocations = new ArrayList (); 63 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 64 protected Configuration newConfiguration() { 65 return new Configuration() { 66 public Configuration addInputStream(InputStream is) { 67 try { 68 is.close(); 69 } 70 catch (IOException ex) { 71 } 72 invocations.add("addResource"); 73 return this; 74 } 75 }; 76 } 77 protected SessionFactory newSessionFactory(Configuration config) { 78 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 79 config.getProperty(Environment.CONNECTION_PROVIDER)); 80 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 81 invocations.add("newSessionFactory"); 82 return null; 83 } 84 }; 85 sfb.setDataSource(ds); 86 sfb.afterPropertiesSet(); 87 assertTrue(sfb.getConfiguration() != null); 88 assertEquals("newSessionFactory", invocations.get(0)); 89 } 90 91 public void testLocalSessionFactoryBeanWithTransactionAwareDataSource() throws Exception { 92 final DriverManagerDataSource ds = new DriverManagerDataSource(); 93 final List invocations = new ArrayList (); 94 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 95 protected Configuration newConfiguration() { 96 return new Configuration() { 97 public Configuration addInputStream(InputStream is) { 98 try { 99 is.close(); 100 } 101 catch (IOException ex) { 102 } 103 invocations.add("addResource"); 104 return this; 105 } 106 }; 107 } 108 protected SessionFactory newSessionFactory(Configuration config) { 109 assertEquals(TransactionAwareDataSourceConnectionProvider.class.getName(), 110 config.getProperty(Environment.CONNECTION_PROVIDER)); 111 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 112 invocations.add("newSessionFactory"); 113 return null; 114 } 115 }; 116 sfb.setDataSource(ds); 117 sfb.setUseTransactionAwareDataSource(true); 118 sfb.afterPropertiesSet(); 119 assertTrue(sfb.getConfiguration() != null); 120 assertEquals("newSessionFactory", invocations.get(0)); 121 } 122 123 public void testLocalSessionFactoryBeanWithDataSourceAndMappingResources() throws Exception { 124 final DriverManagerDataSource ds = new DriverManagerDataSource(); 125 MockControl tmControl = MockControl.createControl(TransactionManager .class); 126 final TransactionManager tm = (TransactionManager ) tmControl.getMock(); 127 final List invocations = new ArrayList (); 128 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 129 protected Configuration newConfiguration() { 130 return new Configuration() { 131 public Configuration addInputStream(InputStream is) { 132 try { 133 is.close(); 134 } 135 catch (IOException ex) { 136 } 137 invocations.add("addResource"); 138 return this; 139 } 140 }; 141 } 142 protected SessionFactory newSessionFactory(Configuration config) { 143 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 144 config.getProperty(Environment.CONNECTION_PROVIDER)); 145 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 146 assertEquals(LocalTransactionManagerLookup.class.getName(), 147 config.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY)); 148 assertEquals(tm, LocalSessionFactoryBean.getConfigTimeTransactionManager()); 149 invocations.add("newSessionFactory"); 150 return null; 151 } 152 }; 153 sfb.setMappingResources(new String [] { 154 "/org/springframework/beans/factory/xml/test.xml", 155 "/org/springframework/beans/factory/xml/child.xml"}); 156 sfb.setDataSource(ds); 157 sfb.setJtaTransactionManager(tm); 158 sfb.afterPropertiesSet(); 159 assertTrue(sfb.getConfiguration() != null); 160 assertEquals("addResource", invocations.get(0)); 161 assertEquals("addResource", invocations.get(1)); 162 assertEquals("newSessionFactory", invocations.get(2)); 163 } 164 165 public void testLocalSessionFactoryBeanWithDataSourceAndMappingJarLocations() throws Exception { 166 final DriverManagerDataSource ds = new DriverManagerDataSource(); 167 final Set invocations = new HashSet (); 168 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 169 protected Configuration newConfiguration() { 170 return new Configuration() { 171 public Configuration addJar(File file) { 172 invocations.add("addResource " + file.getPath()); 173 return this; 174 } 175 }; 176 } 177 protected SessionFactory newSessionFactory(Configuration config) { 178 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 179 config.getProperty(Environment.CONNECTION_PROVIDER)); 180 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 181 invocations.add("newSessionFactory"); 182 return null; 183 } 184 }; 185 sfb.setMappingJarLocations(new Resource[] { 186 new FileSystemResource("mapping.hbm.jar"), new FileSystemResource("mapping2.hbm.jar")}); 187 sfb.setDataSource(ds); 188 sfb.afterPropertiesSet(); 189 assertTrue(sfb.getConfiguration() != null); 190 assertTrue(invocations.contains("addResource mapping.hbm.jar")); 191 assertTrue(invocations.contains("addResource mapping2.hbm.jar")); 192 assertTrue(invocations.contains("newSessionFactory")); 193 } 194 195 public void testLocalSessionFactoryBeanWithDataSourceAndProperties() throws Exception { 196 final DriverManagerDataSource ds = new DriverManagerDataSource(); 197 final Set invocations = new HashSet (); 198 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 199 protected Configuration newConfiguration() { 200 return new Configuration() { 201 public Configuration addInputStream(InputStream is) { 202 try { 203 is.close(); 204 } 205 catch (IOException ex) { 206 } 207 invocations.add("addResource"); 208 return this; 209 } 210 }; 211 } 212 protected SessionFactory newSessionFactory(Configuration config) { 213 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 214 config.getProperty(Environment.CONNECTION_PROVIDER)); 215 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 216 assertEquals("myValue", config.getProperty("myProperty")); 217 invocations.add("newSessionFactory"); 218 return null; 219 } 220 }; 221 sfb.setMappingLocations(new Resource[] { 222 new ClassPathResource("/org/springframework/beans/factory/xml/test.xml")}); 223 sfb.setDataSource(ds); 224 Properties prop = new Properties (); 225 prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass"); 226 prop.setProperty("myProperty", "myValue"); 227 sfb.setHibernateProperties(prop); 228 sfb.afterPropertiesSet(); 229 assertTrue(sfb.getConfiguration() != null); 230 assertTrue(invocations.contains("addResource")); 231 assertTrue(invocations.contains("newSessionFactory")); 232 } 233 234 public void testLocalSessionFactoryBeanWithValidProperties() throws Exception { 235 final Set invocations = new HashSet (); 236 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 237 protected SessionFactory newSessionFactory(Configuration config) { 238 assertEquals(UserSuppliedConnectionProvider.class.getName(), 239 config.getProperty(Environment.CONNECTION_PROVIDER)); 240 assertEquals("myValue", config.getProperty("myProperty")); 241 invocations.add("newSessionFactory"); 242 return null; 243 } 244 }; 245 Properties prop = new Properties (); 246 prop.setProperty(Environment.CONNECTION_PROVIDER, UserSuppliedConnectionProvider.class.getName()); 247 prop.setProperty("myProperty", "myValue"); 248 sfb.setHibernateProperties(prop); 249 sfb.afterPropertiesSet(); 250 assertTrue(sfb.getConfiguration() != null); 251 assertTrue(invocations.contains("newSessionFactory")); 252 } 253 254 public void testLocalSessionFactoryBeanWithInvalidProperties() throws Exception { 255 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean(); 256 sfb.setMappingResources(new String [0]); 257 Properties prop = new Properties (); 258 prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass"); 259 sfb.setHibernateProperties(prop); 260 try { 261 sfb.afterPropertiesSet(); 262 } 263 catch (HibernateException ex) { 264 } 266 } 267 268 public void testLocalSessionFactoryBeanWithInvalidMappings() throws Exception { 269 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean(); 270 sfb.setMappingResources(new String [] {"mapping.hbm.xml"}); 271 try { 272 sfb.afterPropertiesSet(); 273 } 274 catch (IOException ex) { 275 } 277 } 278 279 public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception { 280 MockControl factoryControl = MockControl.createControl(SessionFactory.class); 281 final SessionFactory sessionFactory = (SessionFactory) factoryControl.getMock(); 282 sessionFactory.close(); 283 factoryControl.setVoidCallable(1); 284 factoryControl.replay(); 285 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 286 protected SessionFactory newSessionFactory(Configuration config) { 287 return sessionFactory; 288 } 289 }; 290 sfb.setMappingResources(new String [0]); 291 sfb.setDataSource(new DriverManagerDataSource()); 292 sfb.setExposeTransactionAwareSessionFactory(false); 293 sfb.afterPropertiesSet(); 294 assertTrue(sessionFactory.equals(sfb.getObject())); 295 sfb.destroy(); 296 factoryControl.verify(); 297 } 298 299 public void testLocalSessionFactoryBeanWithEntityInterceptor() throws Exception { 300 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 301 protected Configuration newConfiguration() { 302 return new Configuration() { 303 public Configuration setInterceptor(Interceptor interceptor) { 304 throw new IllegalArgumentException (interceptor.toString()); 305 } 306 }; 307 } 308 }; 309 sfb.setMappingResources(new String [0]); 310 sfb.setDataSource(new DriverManagerDataSource()); 311 MockControl interceptorControl = MockControl.createControl(Interceptor.class); 312 Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock(); 313 interceptorControl.replay(); 314 sfb.setEntityInterceptor(entityInterceptor); 315 try { 316 sfb.afterPropertiesSet(); 317 fail("Should have thrown IllegalArgumentException"); 318 } 319 catch (IllegalArgumentException ex) { 320 assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString())); 322 } 323 } 324 325 public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception { 326 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 327 protected Configuration newConfiguration() { 328 return new Configuration() { 329 public Configuration setNamingStrategy(NamingStrategy namingStrategy) { 330 throw new IllegalArgumentException (namingStrategy.toString()); 331 } 332 }; 333 } 334 }; 335 sfb.setMappingResources(new String [0]); 336 sfb.setDataSource(new DriverManagerDataSource()); 337 sfb.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); 338 try { 339 sfb.afterPropertiesSet(); 340 fail("Should have thrown IllegalArgumentException"); 341 } 342 catch (IllegalArgumentException ex) { 343 assertTrue("Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString())); 345 } 346 } 347 348 public void testLocalSessionFactoryBeanWithCacheStrategies() throws Exception { 349 final Properties registeredClassCache = new Properties (); 350 final Properties registeredCollectionCache = new Properties (); 351 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 352 protected Configuration newConfiguration() { 353 return new Configuration() { 354 public Configuration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy) { 355 registeredClassCache.setProperty(clazz, concurrencyStrategy); 356 return this; 357 } 358 public Configuration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) { 359 registeredCollectionCache.setProperty(collectionRole, concurrencyStrategy); 360 return this; 361 } 362 }; 363 } 364 protected SessionFactory newSessionFactory(Configuration config) { 365 return null; 366 } 367 }; 368 369 sfb.setMappingResources(new String [0]); 370 sfb.setDataSource(new DriverManagerDataSource()); 371 Properties classCache = new Properties (); 372 classCache.setProperty("org.springframework.beans.TestBean", "read-write"); 373 sfb.setEntityCacheStrategies(classCache); 374 Properties collectionCache = new Properties (); 375 collectionCache.setProperty("org.springframework.beans.TestBean.friends", "read-only"); 376 sfb.setCollectionCacheStrategies(collectionCache); 377 sfb.afterPropertiesSet(); 378 379 assertEquals(classCache, registeredClassCache); 380 assertEquals(collectionCache, registeredCollectionCache); 381 } 382 383 public void testLocalSessionFactoryBeanWithEventListeners() throws Exception { 384 final Map registeredListeners = new HashMap (); 385 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 386 protected Configuration newConfiguration() { 387 return new Configuration() { 388 public void setListener(String type, Object listener) { 389 registeredListeners.put(type, listener); 390 } 391 }; 392 } 393 protected SessionFactory newSessionFactory(Configuration config) { 394 return null; 395 } 396 }; 397 sfb.setMappingResources(new String [0]); 398 sfb.setDataSource(new DriverManagerDataSource()); 399 Map listeners = new HashMap (); 400 listeners.put("flush", "myListener"); 401 listeners.put("create", "yourListener"); 402 sfb.setEventListeners(listeners); 403 sfb.afterPropertiesSet(); 404 assertEquals(listeners, registeredListeners); 405 } 406 407 public void testLocalSessionFactoryBeanWithFilterDefinitions() throws Exception { 408 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("filterDefinitions.xml", getClass())); 409 FilterTestLocalSessionFactoryBean sf = (FilterTestLocalSessionFactoryBean) xbf.getBean("&sessionFactory"); 410 assertEquals(2, sf.registeredFilterDefinitions.size()); 411 FilterDefinition filter1 = (FilterDefinition) sf.registeredFilterDefinitions.get(0); 412 FilterDefinition filter2 = (FilterDefinition) sf.registeredFilterDefinitions.get(1); 413 414 assertEquals("filter1", filter1.getFilterName()); 415 assertEquals(2, filter1.getParameterNames().size()); 416 assertEquals(Hibernate.STRING, filter1.getParameterType("param1")); 417 assertEquals(Hibernate.LONG, filter1.getParameterType("otherParam")); 418 assertEquals("someCondition", filter1.getDefaultFilterCondition()); 419 420 assertEquals("filter2", filter2.getFilterName()); 421 assertEquals(1, filter2.getParameterNames().size()); 422 assertEquals(Hibernate.INTEGER, filter2.getParameterType("myParam")); 423 } 424 425 public void testLocalSessionFactoryBeanWithTypeDefinitions() throws Exception { 426 XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("typeDefinitions.xml", getClass())); 427 TypeTestLocalSessionFactoryBean sf = (TypeTestLocalSessionFactoryBean) xbf.getBean("&sessionFactory"); 428 TypeDef type1 = (TypeDef) sf.mappings.getTypeDef("type1"); 429 TypeDef type2 = (TypeDef) sf.mappings.getTypeDef("type2"); 430 431 assertEquals("mypackage.MyTypeClass", type1.getTypeClass()); 432 assertEquals(2, type1.getParameters().size()); 433 assertEquals("value1", type1.getParameters().getProperty("param1")); 434 assertEquals("othervalue", type1.getParameters().getProperty("otherParam")); 435 436 assertEquals("mypackage.MyOtherTypeClass", type2.getTypeClass()); 437 assertEquals(1, type2.getParameters().size()); 438 assertEquals("myvalue", type2.getParameters().getProperty("myParam")); 439 } 440 441 442 public static class FilterTestLocalSessionFactoryBean extends LocalSessionFactoryBean { 443 444 public List registeredFilterDefinitions = new LinkedList (); 445 446 protected Configuration newConfiguration() throws HibernateException { 447 return new Configuration() { 448 public void addFilterDefinition(FilterDefinition definition) { 449 registeredFilterDefinitions.add(definition); 450 } 451 }; 452 } 453 protected SessionFactory newSessionFactory(Configuration config) { 454 return null; 455 } 456 } 457 458 459 public static class TypeTestLocalSessionFactoryBean extends LocalSessionFactoryBean { 460 461 public Mappings mappings; 462 463 protected SessionFactory newSessionFactory(Configuration config) { 464 this.mappings = config.createMappings(); 465 return null; 466 } 467 } 468 469 } 470 | Popular Tags |