KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > DatabaseTestSetup


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseTestSetup.java,v 1.3 2007/01/07 06:14:20 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db;
23
24 import junit.extensions.TestSetup;
25 import junit.framework.Test;
26
27 import org.opensubsystems.core.error.OSSException;
28 import org.opensubsystems.core.util.Config;
29
30 /**
31  * This class makes sure that the database is propertly initialized before the
32  * tests are executed and that the database is properly shutdown when all
33  * tests are finished. ALL TESTCASES DERIVED FROM DatabaseTest CLASS AND ALL
34  * TEST SUITES INCLUDING SUCH TEST CASES SHOULD FOLLOW THE DIRECTION BELLOW.
35  *
36  * To use this setup in your class define your tests as
37  *
38  * class MyTest
39  * {
40  * public static Test suite(
41  * )
42  * {
43  * TestSuite suite = new DatabaseTestSuite("MyTest");
44  * suite.addTestSuite(MyTestInternal.class);
45  * TestSetup wrapper = new DatabaseTestSetup(suite);
46  *
47  * return wrapper;
48  * }
49  * public static class MyTestInternal extends TestCase
50  * {
51  * // add tests here
52  * }
53  * }
54  *
55  * This way if the top level test gets executed, it will be executed as a suite
56  * and this setup will be invoked for all tests in the suite and therefore
57  * database will be properly initialized and shutdown.
58  * If you want to include these tests in another suite, DO NOT ADD MyTest
59  * but instead add MyTest.MyTestInternal. This way the suite which includes
60  * this test can invoke the setup once for all included tests.
61  *
62  * To create a suite of suites, define your suite as
63  *
64  * public final class MyTests
65  * {
66  * public static Test suite(
67  * )
68  * {
69  * TestSuite suite = new DatabaseTestSuite("My tests");
70  * addGenericTests(suite);
71  * TestSetup wrapper = new DatabaseTestSetup(suite);
72  *
73  * return wrapper;
74  * }
75  *
76  * public static void addGenericTests(
77  * TestSuite suite
78  * )
79  * {
80  * // Here we are adding single test case class using the inner internal class
81  * suite.addTestSuite(MyTestInternal.class);
82  * // Here we are including tests from another suite without including the
83  * // DatabaseTestSetup again
84  * MyOtherTests.addGenericTests(suite);
85  * }
86  * }
87  *
88  * @version $Id: DatabaseTestSetup.java,v 1.3 2007/01/07 06:14:20 bastafidli Exp $
89  * @author Miro Halas
90  * @code.reviewer
91  * @code.reviewed TODO: Review this code
92  */

93 public class DatabaseTestSetup extends TestSetup
94 {
95    // Constants ////////////////////////////////////////////////////////////////
96

97    /**
98     * Default property file used to run tests.
99     */

100    protected static final String JavaDoc DEFAULT_PROPERTY_FILE = "osstest.properties";
101
102    // Constructors /////////////////////////////////////////////////////////////
103

104    /**
105     * Static initializer
106     */

107    static
108    {
109       if (Config.getInstance().getPropertyFileName() == null)
110       {
111          Config.getInstance().setPropertyFileName(DEFAULT_PROPERTY_FILE);
112       }
113    }
114
115    /**
116     * Create new DatabaseTestSetup.
117     *
118     * @param test - test to run
119     */

120    public DatabaseTestSetup(
121       Test test
122    )
123    {
124       super(test);
125    }
126
127    /**
128     * Set up environment for the test cases.
129     *
130     * @throws Exception - an error has occured while setting up tests
131     */

132    protected void setUp(
133    ) throws Exception JavaDoc
134    {
135       super.setUp();
136       
137       try
138       {
139          // This will start database if it is not started
140
// Try to initialize the default database if any
141
Database dbDefaultDB;
142          
143          dbDefaultDB = DatabaseImpl.getInstance();
144          if (dbDefaultDB != null)
145          {
146             dbDefaultDB.start();
147          }
148       }
149       catch (OSSException osseExc)
150       {
151          throw new Exception JavaDoc("Cannot start database.", osseExc);
152       }
153    }
154
155    /**
156     * Restore original environment after all the tests were run.
157     *
158     * @throws Exception - an error has occured while tearing down tests
159     */

160    protected void tearDown(
161    ) throws Exception JavaDoc
162    {
163       try
164       {
165          // Some databases for example HSQLDB require to explicitely stop the
166
// database so this gives us opportunity to do it
167
Database dbDefaultDB;
168          
169          dbDefaultDB = DatabaseImpl.getInstance();
170          if (dbDefaultDB != null)
171          {
172             dbDefaultDB.stop();
173          }
174       }
175       catch (OSSException bfeExc)
176       {
177          throw new RuntimeException JavaDoc("Cannot stop database.", bfeExc);
178       }
179
180       super.tearDown();
181    }
182 }
183
Popular Tags