1 16 17 package org.springframework.orm.hibernate; 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.HashSet ; 24 import java.util.List ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 28 import javax.transaction.TransactionManager ; 29 30 import junit.framework.TestCase; 31 import net.sf.hibernate.HibernateException; 32 import net.sf.hibernate.Interceptor; 33 import net.sf.hibernate.SessionFactory; 34 import net.sf.hibernate.cfg.Configuration; 35 import net.sf.hibernate.cfg.Environment; 36 import net.sf.hibernate.cfg.ImprovedNamingStrategy; 37 import net.sf.hibernate.cfg.NamingStrategy; 38 import net.sf.hibernate.connection.UserSuppliedConnectionProvider; 39 import org.easymock.MockControl; 40 41 import org.springframework.core.io.ClassPathResource; 42 import org.springframework.core.io.FileSystemResource; 43 import org.springframework.core.io.Resource; 44 import org.springframework.jdbc.datasource.DriverManagerDataSource; 45 46 49 public class LocalSessionFactoryBeanTests extends TestCase { 50 51 public void testLocalSessionFactoryBeanWithDataSource() throws Exception { 52 final DriverManagerDataSource ds = new DriverManagerDataSource(); 53 final List invocations = new ArrayList (); 54 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 55 protected Configuration newConfiguration() { 56 return new Configuration() { 57 public Configuration addInputStream(InputStream is) { 58 try { 59 is.close(); 60 } 61 catch (IOException ex) { 62 } 63 invocations.add("addResource"); 64 return this; 65 } 66 }; 67 } 68 protected SessionFactory newSessionFactory(Configuration config) { 69 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 70 config.getProperty(Environment.CONNECTION_PROVIDER)); 71 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 72 invocations.add("newSessionFactory"); 73 return null; 74 } 75 }; 76 sfb.setDataSource(ds); 77 sfb.afterPropertiesSet(); 78 assertTrue(sfb.getConfiguration() != null); 79 assertEquals("newSessionFactory", invocations.get(0)); 80 } 81 82 public void testLocalSessionFactoryBeanWithTransactionAwareDataSource() throws Exception { 83 final DriverManagerDataSource ds = new DriverManagerDataSource(); 84 final List invocations = new ArrayList (); 85 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 86 protected Configuration newConfiguration() { 87 return new Configuration() { 88 public Configuration addInputStream(InputStream is) { 89 try { 90 is.close(); 91 } 92 catch (IOException ex) { 93 } 94 invocations.add("addResource"); 95 return this; 96 } 97 }; 98 } 99 protected SessionFactory newSessionFactory(Configuration config) { 100 assertEquals(TransactionAwareDataSourceConnectionProvider.class.getName(), 101 config.getProperty(Environment.CONNECTION_PROVIDER)); 102 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 103 invocations.add("newSessionFactory"); 104 return null; 105 } 106 }; 107 sfb.setDataSource(ds); 108 sfb.setUseTransactionAwareDataSource(true); 109 sfb.afterPropertiesSet(); 110 assertTrue(sfb.getConfiguration() != null); 111 assertEquals("newSessionFactory", invocations.get(0)); 112 } 113 114 public void testLocalSessionFactoryBeanWithDataSourceAndMappingResources() throws Exception { 115 final DriverManagerDataSource ds = new DriverManagerDataSource(); 116 MockControl tmControl = MockControl.createControl(TransactionManager .class); 117 final TransactionManager tm = (TransactionManager ) tmControl.getMock(); 118 final List invocations = new ArrayList (); 119 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 120 protected Configuration newConfiguration() { 121 return new Configuration() { 122 public Configuration addInputStream(InputStream is) { 123 try { 124 is.close(); 125 } 126 catch (IOException ex) { 127 } 128 invocations.add("addResource"); 129 return this; 130 } 131 }; 132 } 133 protected SessionFactory newSessionFactory(Configuration config) { 134 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 135 config.getProperty(Environment.CONNECTION_PROVIDER)); 136 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 137 assertEquals(LocalTransactionManagerLookup.class.getName(), 138 config.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY)); 139 assertEquals(tm, LocalSessionFactoryBean.getConfigTimeTransactionManager()); 140 invocations.add("newSessionFactory"); 141 return null; 142 } 143 }; 144 sfb.setMappingResources(new String [] { 145 "/org/springframework/beans/factory/xml/test.xml", 146 "/org/springframework/beans/factory/xml/child.xml"}); 147 sfb.setDataSource(ds); 148 sfb.setJtaTransactionManager(tm); 149 sfb.afterPropertiesSet(); 150 assertTrue(sfb.getConfiguration() != null); 151 assertEquals("addResource", invocations.get(0)); 152 assertEquals("addResource", invocations.get(1)); 153 assertEquals("newSessionFactory", invocations.get(2)); 154 } 155 156 public void testLocalSessionFactoryBeanWithDataSourceAndMappingJarLocations() throws Exception { 157 final DriverManagerDataSource ds = new DriverManagerDataSource(); 158 final Set invocations = new HashSet (); 159 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 160 protected Configuration newConfiguration() { 161 return new Configuration() { 162 public Configuration addJar(File file) { 163 invocations.add("addResource " + file.getPath()); 164 return this; 165 } 166 }; 167 } 168 protected SessionFactory newSessionFactory(Configuration config) { 169 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 170 config.getProperty(Environment.CONNECTION_PROVIDER)); 171 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 172 invocations.add("newSessionFactory"); 173 return null; 174 } 175 }; 176 sfb.setMappingJarLocations(new Resource[] { 177 new FileSystemResource("mapping.hbm.jar"), new FileSystemResource("mapping2.hbm.jar")}); 178 sfb.setDataSource(ds); 179 sfb.afterPropertiesSet(); 180 assertTrue(sfb.getConfiguration() != null); 181 assertTrue(invocations.contains("addResource mapping.hbm.jar")); 182 assertTrue(invocations.contains("addResource mapping2.hbm.jar")); 183 assertTrue(invocations.contains("newSessionFactory")); 184 } 185 186 public void testLocalSessionFactoryBeanWithDataSourceAndProperties() throws Exception { 187 final DriverManagerDataSource ds = new DriverManagerDataSource(); 188 final Set invocations = new HashSet (); 189 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 190 protected Configuration newConfiguration() { 191 return new Configuration() { 192 public Configuration addInputStream(InputStream is) { 193 try { 194 is.close(); 195 } 196 catch (IOException ex) { 197 } 198 invocations.add("addResource"); 199 return this; 200 } 201 }; 202 } 203 protected SessionFactory newSessionFactory(Configuration config) { 204 assertEquals(LocalDataSourceConnectionProvider.class.getName(), 205 config.getProperty(Environment.CONNECTION_PROVIDER)); 206 assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource()); 207 assertEquals("myValue", config.getProperty("myProperty")); 208 invocations.add("newSessionFactory"); 209 return null; 210 } 211 }; 212 sfb.setMappingLocations(new Resource[] { 213 new ClassPathResource("/org/springframework/beans/factory/xml/test.xml")}); 214 sfb.setDataSource(ds); 215 Properties prop = new Properties (); 216 prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass"); 217 prop.setProperty("myProperty", "myValue"); 218 sfb.setHibernateProperties(prop); 219 sfb.afterPropertiesSet(); 220 assertTrue(sfb.getConfiguration() != null); 221 assertTrue(invocations.contains("addResource")); 222 assertTrue(invocations.contains("newSessionFactory")); 223 } 224 225 public void testLocalSessionFactoryBeanWithValidProperties() throws Exception { 226 final Set invocations = new HashSet (); 227 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 228 protected SessionFactory newSessionFactory(Configuration config) { 229 assertEquals(UserSuppliedConnectionProvider.class.getName(), 230 config.getProperty(Environment.CONNECTION_PROVIDER)); 231 assertEquals("myValue", config.getProperty("myProperty")); 232 invocations.add("newSessionFactory"); 233 return null; 234 } 235 }; 236 Properties prop = new Properties (); 237 prop.setProperty(Environment.CONNECTION_PROVIDER, UserSuppliedConnectionProvider.class.getName()); 238 prop.setProperty("myProperty", "myValue"); 239 sfb.setHibernateProperties(prop); 240 sfb.afterPropertiesSet(); 241 assertTrue(sfb.getConfiguration() != null); 242 assertTrue(invocations.contains("newSessionFactory")); 243 } 244 245 public void testLocalSessionFactoryBeanWithInvalidProperties() throws Exception { 246 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean(); 247 sfb.setMappingResources(new String [0]); 248 Properties prop = new Properties (); 249 prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass"); 250 sfb.setHibernateProperties(prop); 251 try { 252 sfb.afterPropertiesSet(); 253 } 254 catch (HibernateException ex) { 255 } 257 } 258 259 public void testLocalSessionFactoryBeanWithInvalidMappings() throws Exception { 260 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean(); 261 sfb.setMappingResources(new String [] {"mapping.hbm.xml"}); 262 try { 263 sfb.afterPropertiesSet(); 264 } 265 catch (IOException ex) { 266 } 268 } 269 270 public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception { 271 MockControl factoryControl = MockControl.createControl(SessionFactory.class); 272 final SessionFactory sessionFactory = (SessionFactory) factoryControl.getMock(); 273 sessionFactory.close(); 274 factoryControl.setVoidCallable(1); 275 factoryControl.replay(); 276 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 277 protected SessionFactory newSessionFactory(Configuration config) { 278 return sessionFactory; 279 } 280 }; 281 sfb.setMappingResources(new String [0]); 282 sfb.setDataSource(new DriverManagerDataSource()); 283 sfb.afterPropertiesSet(); 284 assertTrue(sessionFactory.equals(sfb.getObject())); 285 sfb.destroy(); 286 factoryControl.verify(); 287 } 288 289 public void testLocalSessionFactoryBeanWithEntityInterceptor() throws Exception { 290 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 291 protected Configuration newConfiguration() { 292 return new Configuration() { 293 public Configuration setInterceptor(Interceptor interceptor) { 294 throw new IllegalArgumentException (interceptor.toString()); 295 } 296 }; 297 } 298 }; 299 sfb.setMappingResources(new String [0]); 300 sfb.setDataSource(new DriverManagerDataSource()); 301 MockControl interceptorControl = MockControl.createControl(Interceptor.class); 302 Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock(); 303 interceptorControl.replay(); 304 sfb.setEntityInterceptor(entityInterceptor); 305 try { 306 sfb.afterPropertiesSet(); 307 fail("Should have thrown IllegalArgumentException"); 308 } 309 catch (IllegalArgumentException ex) { 310 assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString())); 312 } 313 } 314 315 public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception { 316 LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { 317 protected Configuration newConfiguration() { 318 return new Configuration() { 319 public Configuration setNamingStrategy(NamingStrategy namingStrategy) { 320 throw new IllegalArgumentException (namingStrategy.toString()); 321 } 322 }; 323 } 324 }; 325 sfb.setMappingResources(new String [0]); 326 sfb.setDataSource(new DriverManagerDataSource()); 327 sfb.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); 328 try { 329 sfb.afterPropertiesSet(); 330 fail("Should have thrown IllegalArgumentException"); 331 } 332 catch (IllegalArgumentException ex) { 333 assertTrue("Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString())); 335 } 336 } 337 338 } 339 | Popular Tags |