KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > BaseSpringTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import org.hibernate.Session;
20 import org.hibernate.SessionFactory;
21 import org.springframework.orm.hibernate3.SessionFactoryUtils;
22 import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
23
24 /**
25  * Base test class providing Hibernate sessions.
26  * <p>
27  * By default this is auto-wired by type. If a this is going to
28  * result in a conlict the use auto-wire by name. This can be done by
29  * setting populateProtectedVariables to true in the constructor and
30  * then adding protected members with the same name as the bean you require.
31  *
32  * @author Derek Hulley
33  */

34 public abstract class BaseSpringTest extends AbstractTransactionalDataSourceSpringContextTests
35 {
36     /** protected so that it gets populated if autowiring is done by variable name **/
37     protected SessionFactory sessionFactory;
38     
39     /**
40      * Constructor
41      */

42     public BaseSpringTest()
43     {
44     }
45     
46     /**
47      * Setter present for in case autowiring is done by type
48      *
49      * @param sessionFactory
50      */

51     public void setSessionFactory(SessionFactory sessionFactory)
52     {
53         this.sessionFactory = sessionFactory;
54     }
55     
56     /**
57      * @return Returns the existing session attached to the thread.
58      * A new session will <b>not</b> be created.
59      */

60     protected Session getSession()
61     {
62         return SessionFactoryUtils.getSession(sessionFactory, true);
63     }
64     
65     /**
66      * Forces the session to flush to the database (without commiting) and clear the
67      * cache. This ensures that all reads against the session are fresh instances,
68      * which gives the assurance that the DB read/write operations occur correctly.
69      */

70     protected void flushAndClear()
71     {
72         getSession().flush();
73         getSession().clear();
74     }
75
76     /**
77      * Get the config locations
78      *
79      * @return an array containing the config locations
80      */

81     protected String JavaDoc[] getConfigLocations()
82     {
83         if (logger.isDebugEnabled())
84         {
85             logger.debug("Getting config locations");
86         }
87         return ApplicationContextHelper.CONFIG_LOCATIONS;
88     }
89 }
90
Popular Tags