KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > LocalSessionFactoryBeanTests


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.orm.hibernate3;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.transaction.TransactionManager JavaDoc;
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 /**
55  * @author Juergen Hoeller
56  * @since 05.03.2005
57  */

58 public class LocalSessionFactoryBeanTests extends TestCase {
59
60     public void testLocalSessionFactoryBeanWithDataSource() throws Exception JavaDoc {
61         final DriverManagerDataSource ds = new DriverManagerDataSource();
62         final List JavaDoc invocations = new ArrayList JavaDoc();
63         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
64             protected Configuration newConfiguration() {
65                 return new Configuration() {
66                     public Configuration addInputStream(InputStream JavaDoc is) {
67                         try {
68                             is.close();
69                         }
70                         catch (IOException JavaDoc 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 JavaDoc {
92         final DriverManagerDataSource ds = new DriverManagerDataSource();
93         final List JavaDoc invocations = new ArrayList JavaDoc();
94         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
95             protected Configuration newConfiguration() {
96                 return new Configuration() {
97                     public Configuration addInputStream(InputStream JavaDoc is) {
98                         try {
99                             is.close();
100                         }
101                         catch (IOException JavaDoc 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 JavaDoc {
124         final DriverManagerDataSource ds = new DriverManagerDataSource();
125         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
126         final TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
127         final List JavaDoc invocations = new ArrayList JavaDoc();
128         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
129             protected Configuration newConfiguration() {
130                 return new Configuration() {
131                     public Configuration addInputStream(InputStream JavaDoc is) {
132                         try {
133                             is.close();
134                         }
135                         catch (IOException JavaDoc 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 JavaDoc[] {
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 JavaDoc {
166         final DriverManagerDataSource ds = new DriverManagerDataSource();
167         final Set JavaDoc invocations = new HashSet JavaDoc();
168         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
169             protected Configuration newConfiguration() {
170                 return new Configuration() {
171                     public Configuration addJar(File JavaDoc 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 JavaDoc {
196         final DriverManagerDataSource ds = new DriverManagerDataSource();
197         final Set JavaDoc invocations = new HashSet JavaDoc();
198         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
199             protected Configuration newConfiguration() {
200                 return new Configuration() {
201                     public Configuration addInputStream(InputStream JavaDoc is) {
202                         try {
203                             is.close();
204                         }
205                         catch (IOException JavaDoc 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 JavaDoc prop = new Properties JavaDoc();
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 JavaDoc {
235         final Set JavaDoc invocations = new HashSet JavaDoc();
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 JavaDoc prop = new Properties JavaDoc();
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 JavaDoc {
255         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
256         sfb.setMappingResources(new String JavaDoc[0]);
257         Properties JavaDoc prop = new Properties JavaDoc();
258         prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
259         sfb.setHibernateProperties(prop);
260         try {
261             sfb.afterPropertiesSet();
262         }
263         catch (HibernateException ex) {
264             // expected, provider class not found
265
}
266     }
267
268     public void testLocalSessionFactoryBeanWithInvalidMappings() throws Exception JavaDoc {
269         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
270         sfb.setMappingResources(new String JavaDoc[] {"mapping.hbm.xml"});
271         try {
272             sfb.afterPropertiesSet();
273         }
274         catch (IOException JavaDoc ex) {
275             // expected, mapping resource not found
276
}
277     }
278
279     public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception JavaDoc {
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 JavaDoc[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 JavaDoc {
300         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
301             protected Configuration newConfiguration() {
302                 return new Configuration() {
303                     public Configuration setInterceptor(Interceptor interceptor) {
304                         throw new IllegalArgumentException JavaDoc(interceptor.toString());
305                     }
306                 };
307             }
308         };
309         sfb.setMappingResources(new String JavaDoc[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 JavaDoc ex) {
320             // expected
321
assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString()));
322         }
323     }
324
325     public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception JavaDoc {
326         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
327             protected Configuration newConfiguration() {
328                 return new Configuration() {
329                     public Configuration setNamingStrategy(NamingStrategy namingStrategy) {
330                         throw new IllegalArgumentException JavaDoc(namingStrategy.toString());
331                     }
332                 };
333             }
334         };
335         sfb.setMappingResources(new String JavaDoc[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 JavaDoc ex) {
343             // expected
344
assertTrue("Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString()));
345         }
346     }
347
348     public void testLocalSessionFactoryBeanWithCacheStrategies() throws Exception JavaDoc {
349         final Properties JavaDoc registeredClassCache = new Properties JavaDoc();
350         final Properties JavaDoc registeredCollectionCache = new Properties JavaDoc();
351         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
352             protected Configuration newConfiguration() {
353                 return new Configuration() {
354                     public Configuration setCacheConcurrencyStrategy(String JavaDoc clazz, String JavaDoc concurrencyStrategy) {
355                         registeredClassCache.setProperty(clazz, concurrencyStrategy);
356                         return this;
357                     }
358                     public Configuration setCollectionCacheConcurrencyStrategy(String JavaDoc collectionRole, String JavaDoc 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 JavaDoc[0]);
370         sfb.setDataSource(new DriverManagerDataSource());
371         Properties JavaDoc classCache = new Properties JavaDoc();
372         classCache.setProperty("org.springframework.beans.TestBean", "read-write");
373         sfb.setEntityCacheStrategies(classCache);
374         Properties JavaDoc collectionCache = new Properties JavaDoc();
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 JavaDoc {
384         final Map JavaDoc registeredListeners = new HashMap JavaDoc();
385         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
386             protected Configuration newConfiguration() {
387                 return new Configuration() {
388                     public void setListener(String JavaDoc type, Object JavaDoc 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 JavaDoc[0]);
398         sfb.setDataSource(new DriverManagerDataSource());
399         Map JavaDoc listeners = new HashMap JavaDoc();
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 JavaDoc {
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 JavaDoc {
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 JavaDoc registeredFilterDefinitions = new LinkedList JavaDoc();
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