KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > 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.hibernate;
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.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.transaction.TransactionManager JavaDoc;
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 /**
47  * @author Juergen Hoeller
48  */

49 public class LocalSessionFactoryBeanTests extends TestCase {
50
51     public void testLocalSessionFactoryBeanWithDataSource() throws Exception JavaDoc {
52         final DriverManagerDataSource ds = new DriverManagerDataSource();
53         final List JavaDoc invocations = new ArrayList JavaDoc();
54         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
55             protected Configuration newConfiguration() {
56                 return new Configuration() {
57                     public Configuration addInputStream(InputStream JavaDoc is) {
58                         try {
59                             is.close();
60                         }
61                         catch (IOException JavaDoc 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 JavaDoc {
83         final DriverManagerDataSource ds = new DriverManagerDataSource();
84         final List JavaDoc invocations = new ArrayList JavaDoc();
85         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
86             protected Configuration newConfiguration() {
87                 return new Configuration() {
88                     public Configuration addInputStream(InputStream JavaDoc is) {
89                         try {
90                             is.close();
91                         }
92                         catch (IOException JavaDoc 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 JavaDoc {
115         final DriverManagerDataSource ds = new DriverManagerDataSource();
116         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
117         final TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
118         final List JavaDoc invocations = new ArrayList JavaDoc();
119         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
120             protected Configuration newConfiguration() {
121                 return new Configuration() {
122                     public Configuration addInputStream(InputStream JavaDoc is) {
123                         try {
124                             is.close();
125                         }
126                         catch (IOException JavaDoc 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 JavaDoc[] {
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 JavaDoc {
157         final DriverManagerDataSource ds = new DriverManagerDataSource();
158         final Set JavaDoc invocations = new HashSet JavaDoc();
159         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
160             protected Configuration newConfiguration() {
161                 return new Configuration() {
162                     public Configuration addJar(File JavaDoc 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 JavaDoc {
187         final DriverManagerDataSource ds = new DriverManagerDataSource();
188         final Set JavaDoc invocations = new HashSet JavaDoc();
189         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
190             protected Configuration newConfiguration() {
191                 return new Configuration() {
192                     public Configuration addInputStream(InputStream JavaDoc is) {
193                         try {
194                             is.close();
195                         }
196                         catch (IOException JavaDoc 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 JavaDoc prop = new Properties JavaDoc();
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 JavaDoc {
226         final Set JavaDoc invocations = new HashSet JavaDoc();
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 JavaDoc prop = new Properties JavaDoc();
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 JavaDoc {
246         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
247         sfb.setMappingResources(new String JavaDoc[0]);
248         Properties JavaDoc prop = new Properties JavaDoc();
249         prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
250         sfb.setHibernateProperties(prop);
251         try {
252             sfb.afterPropertiesSet();
253         }
254         catch (HibernateException ex) {
255             // expected, provider class not found
256
}
257     }
258
259     public void testLocalSessionFactoryBeanWithInvalidMappings() throws Exception JavaDoc {
260         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
261         sfb.setMappingResources(new String JavaDoc[] {"mapping.hbm.xml"});
262         try {
263             sfb.afterPropertiesSet();
264         }
265         catch (IOException JavaDoc ex) {
266             // expected, mapping resource not found
267
}
268     }
269
270     public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception JavaDoc {
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 JavaDoc[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 JavaDoc {
290         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
291             protected Configuration newConfiguration() {
292                 return new Configuration() {
293                     public Configuration setInterceptor(Interceptor interceptor) {
294                         throw new IllegalArgumentException JavaDoc(interceptor.toString());
295                     }
296                 };
297             }
298         };
299         sfb.setMappingResources(new String JavaDoc[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 JavaDoc ex) {
310             // expected
311
assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString()));
312         }
313     }
314
315     public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception JavaDoc {
316         LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
317             protected Configuration newConfiguration() {
318                 return new Configuration() {
319                     public Configuration setNamingStrategy(NamingStrategy namingStrategy) {
320                         throw new IllegalArgumentException JavaDoc(namingStrategy.toString());
321                     }
322                 };
323             }
324         };
325         sfb.setMappingResources(new String JavaDoc[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 JavaDoc ex) {
333             // expected
334
assertTrue("Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString()));
335         }
336     }
337
338 }
339
Popular Tags