KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > test > OpenCmsTestProperties


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/test/OpenCmsTestProperties.java,v $
3  * Date : $Date: 2005/06/23 14:27:27 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.test;
33
34 import org.opencms.util.CmsFileUtil;
35 import org.opencms.util.CmsPropertyUtils;
36
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import org.apache.commons.collections.ExtendedProperties;
41
42 /**
43  * Reads and manages the test.properties file.<p>
44  *
45  * @author Michael Moossen
46  *
47  * @version $Revision: 1.12 $
48  *
49  * @since 6.0.0
50  */

51 public final class OpenCmsTestProperties {
52
53     private static ExtendedProperties m_configuration;
54
55     /**
56      * the singleton instance.
57      */

58     private static OpenCmsTestProperties m_testSingleton;
59
60     /**
61      * the path to the test.properties file.
62      */

63     private String JavaDoc m_basePath;
64
65     /**
66      * the database to use.
67      */

68     private String JavaDoc m_dbProduct;
69
70     /**
71      * the path to the data test folder.
72      */

73     private String JavaDoc m_testDataPath;
74
75     /**
76      * the path to the webapp test folder.
77      */

78     private String JavaDoc m_testWebappPath;
79
80     /**
81      * private default constructor.
82      */

83     private OpenCmsTestProperties() {
84
85         // noop
86
}
87
88     /**
89      * @return the singleton instance
90      */

91     public static OpenCmsTestProperties getInstance() {
92
93         if (m_testSingleton == null) {
94             throw new RuntimeException JavaDoc("You have to initialize the test properties.");
95         }
96         return m_testSingleton;
97     }
98
99     /**
100      * Reads property file test.properties and fills singleton members.<p>
101      *
102      * @param basePath the path where to find the test.properties file
103      */

104     public static void initialize(String JavaDoc basePath) {
105
106         if (m_testSingleton != null) {
107             return;
108         }
109
110         String JavaDoc testPropPath;
111         m_testSingleton = new OpenCmsTestProperties();
112
113         m_testSingleton.m_basePath = basePath;
114         if (!m_testSingleton.m_basePath.endsWith("/")) {
115             m_testSingleton.m_basePath += "/";
116         }
117
118         try {
119             testPropPath = CmsFileUtil.getResourcePathFromClassloader("test.properties");
120             if (testPropPath == null) {
121                 throw new RuntimeException JavaDoc(
122                     "Test property file ('test.properties') could not be found by context Classloader.");
123             }
124             File JavaDoc f = new File JavaDoc(testPropPath);
125             if (!f.exists()) {
126                 throw new RuntimeException JavaDoc(
127                     "Test property file ('test.properties') could not be found. Context Classloader suggested location: "
128                         + testPropPath);
129             }
130             m_configuration = CmsPropertyUtils.loadProperties(testPropPath);
131         } catch (IOException JavaDoc e) {
132             e.printStackTrace(System.out);
133             throw new RuntimeException JavaDoc(e);
134         }
135
136         m_testSingleton.m_testDataPath = m_configuration.getString("test.data.path");
137         m_testSingleton.m_testWebappPath = m_configuration.getString("test.webapp.path");
138         m_testSingleton.m_dbProduct = m_configuration.getString("db.product");
139
140     }
141
142     /**
143      * @return Returns the path to the test.properties file
144      */

145     public String JavaDoc getBasePath() {
146
147         return m_basePath;
148     }
149
150     /**
151      * @return the parsed configuration file ('test.properties')
152      */

153
154     public ExtendedProperties getConfiguration() {
155
156         return m_configuration;
157     }
158
159     /**
160      *
161      * @return a String identifying the db.product property value of the 'test.properties' value.
162      */

163     public String JavaDoc getDbProduct() {
164
165         return m_dbProduct;
166     }
167
168     /**
169      * @return the path to the data test directory
170      */

171     public String JavaDoc getTestDataPath() {
172
173         return m_testDataPath;
174     }
175
176     /**
177      * @return the path to the webapp test directory
178      */

179     public String JavaDoc getTestWebappPath() {
180
181         return m_testWebappPath;
182     }
183
184 }
Popular Tags