1 | package com.ether; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | |
---|
5 | import org.hibernate.HibernateException; |
---|
6 | import org.hibernate.Session; |
---|
7 | import org.hibernate.SessionFactory; |
---|
8 | import org.springframework.orm.hibernate3.HibernateCallback; |
---|
9 | import org.springframework.orm.hibernate3.HibernateTemplate; |
---|
10 | import org.springframework.transaction.annotation.Transactional; |
---|
11 | |
---|
12 | public abstract class HibernateTestHelper<BEAN> |
---|
13 | extends TestHelper<BEAN> |
---|
14 | { |
---|
15 | /** |
---|
16 | * The constructor. |
---|
17 | * |
---|
18 | * @param beanContextName the bean context name. |
---|
19 | * @param beanName the bean name. |
---|
20 | * @param otherContextNames the other context names. |
---|
21 | * @param sessionFactoryBeanName the session factory bean name. |
---|
22 | */ |
---|
23 | protected HibernateTestHelper( final String beanContextName, final String beanName, final String[] otherContextNames, final String sessionFactoryBeanName ) |
---|
24 | { |
---|
25 | super( beanContextName, beanName, otherContextNames ); |
---|
26 | _sessionFactoryBeanName = sessionFactoryBeanName; |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * Executes operations before running test. |
---|
31 | * Implemented in ServiceTestHelper class |
---|
32 | */ |
---|
33 | protected abstract void beforeHibernateTest() |
---|
34 | throws Exception; |
---|
35 | |
---|
36 | /** |
---|
37 | * Executes operations after running test. |
---|
38 | * Implemented in ServiceTestHelper class |
---|
39 | */ |
---|
40 | protected abstract void afterHibernateTest() |
---|
41 | throws Exception; |
---|
42 | |
---|
43 | @Override |
---|
44 | protected final void beforeBeanTest() |
---|
45 | throws Exception |
---|
46 | { |
---|
47 | final ArrayList tab = new ArrayList<String>(); |
---|
48 | final SessionFactory sessionFactory; |
---|
49 | |
---|
50 | if( _sessionFactoryBeanName != null ) |
---|
51 | sessionFactory = (SessionFactory) getApplicationContext().getBean( _sessionFactoryBeanName ); |
---|
52 | else |
---|
53 | sessionFactory = (SessionFactory) getApplicationContext().getBean( DEFAULT_SESSION_FACTORY_BEAN_NAME ); |
---|
54 | |
---|
55 | _hibernateTemplate = new HibernateTemplate( sessionFactory ); |
---|
56 | beforeHibernateTest(); |
---|
57 | } |
---|
58 | |
---|
59 | @Override |
---|
60 | protected final void afterBeanTest() |
---|
61 | throws Exception |
---|
62 | { |
---|
63 | afterHibernateTest(); |
---|
64 | _hibernateTemplate = null; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Delete all datas. |
---|
69 | * |
---|
70 | * @param persistentObjectClasses the persistent object classes. |
---|
71 | * @throws Exception an exception. |
---|
72 | */ |
---|
73 | @Transactional |
---|
74 | protected final void deleteAllDatas( final Class<?>[] persistentObjectClasses ) |
---|
75 | throws Exception |
---|
76 | { |
---|
77 | _hibernateTemplate.execute( new HibernateCallback() |
---|
78 | { |
---|
79 | public Object doInHibernate( final Session session ) |
---|
80 | throws HibernateException |
---|
81 | { |
---|
82 | for( final Class<?> persistentObjectClass : persistentObjectClasses ) |
---|
83 | session.createQuery( "delete from " + persistentObjectClass.getName() ).executeUpdate(); |
---|
84 | |
---|
85 | return null; |
---|
86 | } |
---|
87 | } ); |
---|
88 | } |
---|
89 | |
---|
90 | private static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory"; |
---|
91 | |
---|
92 | private String _sessionFactoryBeanName; |
---|
93 | private HibernateTemplate _hibernateTemplate; |
---|
94 | } |
---|