KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > GenericDatabaseTests


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: GenericDatabaseTests.java,v 1.15 2007/01/28 06:54:55 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;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import junit.extensions.TestSetup;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.opensubsystems.core.persist.db.DatabaseTestSetup;
34 import org.opensubsystems.core.persist.db.DatabaseTestSuite;
35 import org.opensubsystems.core.util.Config;
36 import org.opensubsystems.core.util.Log;
37
38 /**
39  * This class collects all generic database tests, which are then executed
40  * for a specific database.
41  *
42  * @version $Id: GenericDatabaseTests.java,v 1.15 2007/01/28 06:54:55 bastafidli Exp $
43  * @author Miro Halas
44  * @code.reviewer Miro Halas
45  * @code.reviewed 1.8 2005/02/12 23:14:05 bastafidli
46  */

47 public class GenericDatabaseTests
48 {
49    // Cached values ////////////////////////////////////////////////////////////
50

51    /**
52     * Logger for this class
53     */

54    private static Logger JavaDoc s_logger = Log.getInstance(GenericDatabaseTests.class);
55
56    // Constructors /////////////////////////////////////////////////////////////
57

58    /**
59     * Private constructor since this class cannot be instantiated
60     */

61    protected GenericDatabaseTests(
62    )
63    {
64       super();
65    }
66
67    // Public methods ///////////////////////////////////////////////////////////
68

69    /**
70     * Create suite of all tests which should be run for this database.
71     *
72     * @return Test - suite of tests to run for this database
73     */

74    public static Test suite(
75    )
76    {
77       Config.getInstance().setPropertyFileName("osstest.properties");
78       TestSuite suite = new DatabaseTestSuite("Test for database setup in property file.");
79       addGenericTests(suite);
80       TestSetup wrapper = new DatabaseTestSetup(suite);
81
82       return wrapper;
83    }
84
85    // Helper methods ///////////////////////////////////////////////////////////
86

87    /**
88     * Add all tests which should be executed for some specific database
89     *
90     * @param suite - suite to add tests to
91     */

92    protected static void addGenericTests(
93       TestSuite suite
94    )
95    {
96       // We cannot do this
97
// SecurityTests.addGenericTests(suite);
98
// since if the security component is packaged separately and it is not
99
// shipped with core, then these tests couldn't be found. We have to add
100
// the tests dynamically
101
addGenericTestsFromTestSuite(suite, "org.opensubsystems.portal.PortalTests");
102       addGenericTestsFromTestSuite(suite, "org.opensubsystems.inventory.InventorySubsystemTests");
103       addGenericTestsFromTestSuite(suite, "org.opensubsystems.search.SearchTests");
104       addGenericTestsFromTestSuite(suite, "org.opensubsystems.preferences.PreferencesTests");
105       addGenericTestsFromTestSuite(suite, "org.opensubsystems.security.SecurityTests");
106       addGenericTestsFromTestSuite(suite, "org.opensubsystems.core.CoreTests");
107
108 // TODO: For Miro: Enable these test once refactored
109
// WebLogTests.addGenericTests(suite);
110
// FileTests.addGenericTests(suite);
111
// TemplateTests.addGenericTests(suite);
112
// WorkflowTests.addGenericTests(suite);
113
// TimingTests.addGenericTests(suite);
114
// CommunicationTests.addGenericTests(suite);
115
// ShoppingTests.addGenericTests(suite);
116
// ToolcribTests.addGenericTests(suite);
117

118       // TODO: Everyone: Add tests for your modules here
119
}
120    
121    /**
122     * Add tests from a suite specified by a class name to a specified suite.
123     *
124     * @param suiteToAddTo - suite to add tests to
125     * @param strSuiteClassToAddFrom - class for a suite to add tests from
126     */

127    protected static void addGenericTestsFromTestSuite(
128       TestSuite suiteToAddTo,
129       String JavaDoc strSuiteClassToAddFrom
130    )
131    {
132       try
133       {
134          Class JavaDoc suiteToAddFrom;
135          Method JavaDoc genericTestsMethod;
136          
137          suiteToAddFrom = Class.forName(strSuiteClassToAddFrom);
138          genericTestsMethod = suiteToAddFrom.getMethod("addGenericTests",
139                                                        new Class JavaDoc[] {TestSuite.class});
140          genericTestsMethod.invoke(null, new Object JavaDoc[] {suiteToAddTo});
141          s_logger.fine("Test from suite " + strSuiteClassToAddFrom
142                        + " were successfully added.");
143       }
144       catch (ClassNotFoundException JavaDoc eNoClass)
145       {
146          // Log this just as fine with no exception since this is expected if
147
// components are packed individually
148
s_logger.fine("Cannot find class " + strSuiteClassToAddFrom
149                        + " therefore no tests will be added from this suite.");
150       }
151       catch (SecurityException JavaDoc eSecurity)
152       {
153          s_logger.log(Level.SEVERE,
154                       "Cannot add tests from suite " + strSuiteClassToAddFrom
155                       + " since method addGenericTests cannot be accessed.",
156                       eSecurity);
157       }
158       catch (NoSuchMethodException JavaDoc eNoMethod)
159       {
160          s_logger.log(Level.SEVERE,
161                       "Cannot add tests from suite " + strSuiteClassToAddFrom
162                       + " since method addGenericTests cannot be found.",
163                       eNoMethod);
164       }
165       catch (IllegalArgumentException JavaDoc eBadArg)
166       {
167          s_logger.log(Level.SEVERE,
168                       "Cannot add tests from suite " + strSuiteClassToAddFrom
169                       + " since method addGenericTests doesn't take TestSuite"
170                       + " as a parameter.", eBadArg);
171       }
172       catch (IllegalAccessException JavaDoc eBadAccess)
173       {
174          s_logger.log(Level.SEVERE,
175                       "Cannot add tests from suite " + strSuiteClassToAddFrom
176                       + " since method addGenericTests cannot be accessed.",
177                       eBadAccess);
178       }
179       catch (InvocationTargetException JavaDoc eCantInvoke)
180       {
181          s_logger.log(Level.SEVERE,
182                       "Cannot add tests from suite " + strSuiteClassToAddFrom
183                       + " since an error has occured invoking method.",
184                       eCantInvoke);
185       }
186    }
187 }
188
Popular Tags