1 package com.sourceforge.jpatterns.utils.junit;
2
3 import com.sourceforge.jpatterns.core.JPConstants;
4 import com.sourceforge.jpatterns.core.configuration.PropertiesBasedFactory;
5 import com.sourceforge.jpatterns.core.configuration.PropertiesManagerImpl;
6 import com.sourceforge.jpatterns.schema.JPatternsConfig;
7 import com.sourceforge.jpatterns.utils.CastorUtils;
8 import com.zmicer.utils.InputArgumentUtils;
9 import com.zmicer.utils.junit.SystemPropsUtils;
10 import com.zmicer.utils.junit.ZmicerTestUtils;
11 import org.apache.log4j.Logger;
12
13 import java.io.File;
14
15 /**
16 * This class stores the common functionality for organizing the JPatterns framework test environment.
17 * <p/>
18 * $Author:: zmicer $<br/>
19 * $Rev:: 67 $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
20 */
21 public class JPatternsTestUtils
22 {
23 /**
24 * Logger instance.
25 */
26 final public static Logger LOG = Logger.getLogger(JPatternsTestUtils.class);
27
28 /**
29 * reload the instances of property related classes depends on changed before settings
30 */
31 protected static void reloadPropertiesManager()
32 {
33 PropertiesBasedFactory propsFactory;
34 propsFactory = PropertiesBasedFactory.getInstance();
35 PropertiesManagerImpl propManagerImpl;
36 propManagerImpl = (PropertiesManagerImpl)propsFactory.getPropertiesManager();
37 propManagerImpl.initConfigs();
38 PropertiesBasedFactory.getInstance().cleanImplementations();
39 }
40
41 /**
42 * Get the <code>JPatternsConfig</code> using the test case folder name and jpatterns config file name.
43 * <br/>
44 * note [zmicer]: see {@link com.zmicer.utils.junit.ZmicerTestUtils#getFolderWithTestData()}
45 *
46 * @param classBaseName JPatterns test case folder name - it defines the folder at the test data root folder where the
47 * required xml is stored.
48 * Can be null - in this case we would check the root folder itself.
49 * @param jPatternsConfigFileName JPatterns required config file name
50 * Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
51 *
52 * @return the castor object - JPatterns
53 */
54 public static JPatternsConfig getConfig(final String classBaseName, final String jPatternsConfigFileName)
55 {
56 InputArgumentUtils.checkStrings(true, jPatternsConfigFileName);
57 String fullFilePath = ZmicerTestUtils.getFullTestDataFileName(classBaseName, jPatternsConfigFileName);
58 File file = new File(fullFilePath);
59 if (!file.isFile())
60 {
61 throw new IllegalArgumentException("Invalid data is passed: the following path doesn't define file [" + fullFilePath + "]");
62 }
63 return CastorUtils.getJPatternsConfig(file);
64 }
65
66 /**
67 * Get config using the provided Class instance (it is necessary for the obraining test case folder name) and the name of the file. In
68 * the case of there is not file with the provided data - the IllegalArgumentException would appear.
69 *
70 * @param claz Class instance using which we would obtain the test case folder name
71 * @param testCase test case name, could be null. In that case the file is obtained from the root test data folder
72 * for this class
73 * @param jPatternsConfigFileName the name of the JPatterns config file
74 *
75 * @return the obtained JPatternsConfig castor config
76 */
77 public static JPatternsConfig getConfig(final Class claz, final String testCase, final String jPatternsConfigFileName)
78 {
79 InputArgumentUtils.checkObjects(claz, jPatternsConfigFileName);
80 String fullFilePath = ZmicerTestUtils.getFullTestDataFileName(claz, false, testCase, jPatternsConfigFileName, true);
81 File file = new File(fullFilePath);
82 if (!file.isFile())
83 {
84 throw new IllegalArgumentException("Invalid data is passed: the following path doesn't define file [" + fullFilePath + "]");
85 }
86 return CastorUtils.getJPatternsConfig(file);
87 }
88
89 /**
90 * Set the JVM parameters defining the properties files to be used (the full names to the properties files would be set -
91 * please study {@link com.zmicer.utils.ResourceUtils#getResourceBundle(java.lang.String)},
92 * {@link com.sourceforge.jpatterns.core.configuration.PropertiesManagerImpl#initConfigs())}
93 * <br/>
94 * Pleaes be noticed in the case the default property file specified is not exist the JPInitializationException would appear.
95 * It is allowable for the custom properties not to be at the pointed folder.
96 * <br/>
97 * Be noticed we do not check if the provided folder - class exists - just because user may set the params incorrectly to turn of smth
98 *
99 * @param claz the class object for which we would set the properties - it would be used for the obtaining folder with the test
100 * data for the concrete class. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
101 * @param considerMethodName if this is true then we should have the special folder which name is "method_%running method name%",
102 * and only then put there the folder with test case. It could be null
103 * @param testCase the test case - defines the folder inside the root folder with the test data for the certain class.
104 * Could be null - in this case the properties files would be found at the root test data folder for the certain
105 * class.
106 * @param defaultProps default JPatterns properties file name. Could be null - in this case we would find for the
107 * {@link com.sourceforge.jpatterns.core.JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME}
108 * @param customProps custom JPatterns properties file name. Could be null - in this case we would find for the
109 * {@link com.sourceforge.jpatterns.core.JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME}
110 * @param reloadFactories if this is <code>true</code> we reload PropertiesBasedFactory implementations and PropertiesManager too.
111 */
112 public static void setJVMPropsParams(final Class claz, boolean considerMethodName, final String testCase, final String defaultProps,
113 final String customProps, boolean reloadFactories)
114 {
115 InputArgumentUtils.checkObjects(claz);
116 final String actualDefaultBaseName =
117 ((null == defaultProps) ? JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME : defaultProps) +
118 JPConstants.PropertiesConfigFilesConstants.PROPERTIES_EXTENSION;
119 final String actualCustomBaseName =
120 ((null == customProps) ? JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME : customProps) +
121 JPConstants.PropertiesConfigFilesConstants.PROPERTIES_EXTENSION;
122 final String fullDefaultName = ZmicerTestUtils.getFullTestDataFileName(claz, considerMethodName, testCase, actualDefaultBaseName,
123 false);
124 System.setProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM, fullDefaultName);
125
126 try
127 {
128 final String fullCustomName = ZmicerTestUtils.getFullTestDataFileName(claz, considerMethodName, testCase, actualCustomBaseName,
129 false);
130 System.setProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM, fullCustomName);
131 }
132 catch (Exception ex)
133 {
134 // just catch - it is allowable for the custom properties not to be at the pointed directory.
135 }
136 if (reloadFactories)
137 {
138 reloadPropertiesManager();
139 }
140 }
141
142 /**
143 * Analog of the method below still allows to set the parameters not depending in the class - just on the name of folder in the
144 * "testdata" folder - it is our test case. Please study testdata\00_common_props_for_tests - it is an example of this approach
145 *
146 * @param testCase the test case - defines the folder inside the root folder with the test data for the certain class.
147 * Could be null - in this case the properties files would be found at the root test data folder for the certain
148 * class.
149 * @param defaultProps default JPatterns properties file name. Could be null - in this case we would find for the
150 * {@link com.sourceforge.jpatterns.core.JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME}
151 * @param customProps custom JPatterns properties file name. Could be null - in this case we would find for the
152 * {@link com.sourceforge.jpatterns.core.JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME}
153 * @param reloadFactories if this is <code>true</code> we reload PropertiesBasedFactory implementations and PropertiesManager too.
154 */
155 public static void setJVMPropsParams(final String testCase, final String defaultProps,
156 final String customProps, boolean reloadFactories)
157 {
158 final String actualDefaultBaseName =
159 ((null == defaultProps) ? JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME : defaultProps) +
160 JPConstants.PropertiesConfigFilesConstants.PROPERTIES_EXTENSION;
161 final String actualCustomBaseName =
162 ((null == customProps) ? JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME : customProps) +
163 JPConstants.PropertiesConfigFilesConstants.PROPERTIES_EXTENSION;
164 final String fullDefaultName = ZmicerTestUtils.getFullTestDataFileName(testCase, actualDefaultBaseName);
165 System.setProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM, fullDefaultName);
166
167 try
168 {
169 final String fullCustomName = ZmicerTestUtils.getFullTestDataFileName(testCase, actualCustomBaseName);
170 System.setProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM, fullCustomName);
171 }
172 catch (Exception ex)
173 {
174 // just catch - it is allowable for the custom properties not to be at the pointed directory.
175 }
176 if (reloadFactories)
177 {
178 reloadPropertiesManager();
179 }
180 }
181
182 /**
183 * See the method {@link JPatternsTestUtils#setJVMPropsParams(Class,boolean,String,String,String,boolean)}.
184 * The only difference is this method allows to set the default property separately
185 *
186 * @param claz the class object for which we would set the properties - it would be used for the obtaining folder with the test
187 * data for the concrete class. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
188 * @param testCase the test case - defines the folder inside the root folder with the test data for the certain class.
189 * Could be null - in this case the properties files would be found at the root test data folder for the certain class.
190 * @param defaultProps default JPatterns properties file name. Could be null - in this case we would find for the
191 * {@link com.sourceforge.jpatterns.core.JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME}
192 */
193 public static void setJVMDefaultProps(final Class claz, final String testCase, final String defaultProps)
194 {
195 InputArgumentUtils.checkObjects(claz);
196 final String actualDefaultBaseName =
197 ((null == defaultProps) ? JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME : defaultProps) +
198 JPConstants.PropertiesConfigFilesConstants.PROPERTIES_EXTENSION;
199 final String fullDefaultName = ZmicerTestUtils.getFullTestDataFileName(claz, false, testCase, actualDefaultBaseName, false);
200 System.setProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM, fullDefaultName);
201
202 reloadPropertiesManager();
203 }
204
205 /**
206 * See the method {@link JPatternsTestUtils#setJVMPropsParams(Class,boolean,String,String,String,boolean)}.
207 * The only difference is this method allows to set the default property separately
208 *
209 * @param claz the class object for which we would set the properties - it would be used for the obtaining folder with the test
210 * data for the concrete class. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
211 * @param testCase the test case - defines the folder inside the root folder with the test data for the certain class.
212 * Could be null - in this case the properties files would be found at the root test data folder for the certain class.
213 * @param defaultProps default JPatterns properties file name. Could be null - in this case we would find for the
214 * {@link com.sourceforge.jpatterns.core.JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME}
215 */
216 public static void setJVMCustomProps(final Class claz, final String testCase, final String defaultProps)
217 {
218 InputArgumentUtils.checkObjects(claz);
219 final String actualDefaultBaseName =
220 ((null == defaultProps) ? JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME : defaultProps) +
221 JPConstants.PropertiesConfigFilesConstants.PROPERTIES_EXTENSION;
222 final String fullDefaultName = ZmicerTestUtils.getFullTestDataFileName(claz, false, testCase, actualDefaultBaseName, false);
223 System.setProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM, fullDefaultName);
224
225 reloadPropertiesManager();
226 }
227
228 /**
229 * Reset the JVM params related to the JVM properties.
230 */
231 public static void resetJVMProps()
232 {
233 SystemPropsUtils.clearProps(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM,
234 JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM);
235 // reload the instances depends on these settings
236 reloadPropertiesManager();
237 }
238
239 /**
240 * This methods allows us to setup the configuration for the tests using the provided class (usually it is a class of the test you are
241 * working with).
242 * <br/>
243 * Be noticed we do not check if the provided file - class exists - just because user may set the params incorrectly to turn of smth.
244 * But the folder existence would be check
245 * <br/>
246 * The custom values are set to the not existed values
247 *
248 * @param claz the class having appropriate folder with test data.
249 * Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
250 * @param considerMethodName if this is true then we should have the special folder which name is "method_%running method name%",
251 * and only then put there the folder with test case. It could be null
252 * @param testCase test case defines the test case folder inside the root test data folder of the class provided.
253 * Could be null.
254 * @param frameworkConfig framework config defines the name of the xml file would be used for the framework config
255 * @param useCommonFrameworkConfig this parameter defines that the common JPatterns framework configuration file is searched at the
256 * <code>%test data folder%/JPatternsTestUtils.CommonTestCases.COMMON_PROPS</code> folder. This
257 * parameter is necessary to avoid duplicating framework configuration files for all the tests.
258 */
259 public static void setJVMXmlParams(final Class claz, boolean considerMethodName, final String testCase, final String frameworkConfig,
260 final boolean useCommonFrameworkConfig)
261 {
262 InputArgumentUtils.checkObjects(claz);
263 final String actualName = (null == frameworkConfig || frameworkConfig.trim().equals("")) ?
264 JPConstants.XMLConfigFilesConstants.DEFAULT_XML_FRAMEWORK_CONFIG_FILE_NAME : frameworkConfig;
265
266 if (useCommonFrameworkConfig)
267 {
268 System.setProperty(JPConstants.XMLConfigFilesConstants.DEFAULT_XML_FRAMEWORK_CONFIG_FILE_NAME_JVM_PARAM,
269 ZmicerTestUtils.getFullTestDataFileName(claz, false,
270 ".." + File.separator + ZmicerTestUtils.CommonTestCases.COMMON_PROPS, actualName, false));
271 }
272 else
273 {
274 System.setProperty(JPConstants.XMLConfigFilesConstants.DEFAULT_XML_FRAMEWORK_CONFIG_FILE_NAME_JVM_PARAM,
275 ZmicerTestUtils.getFullTestDataFileName(claz, considerMethodName, testCase, actualName, false));
276 }
277 System.setProperty(JPConstants.XMLConfigFilesConstants.CUSTOM_XML_FRAMEWORK_CONFIG_FILE_NAME_JVM_PARAM,
278 ZmicerTestUtils.getFullTestDataFileName(claz, considerMethodName, testCase, actualName + "not_existed", false));
279
280 System.setProperty(JPConstants.XMLConfigFilesConstants.DEFAULT_XML_CONSUMER_CONFIG_DIR_NAME_JVM_PARAM,
281 ZmicerTestUtils.getFullTestDataFolderName(claz, considerMethodName, testCase, false));
282 System.setProperty(JPConstants.XMLConfigFilesConstants.CUSTOM_XML_CONSUMER_CONFIG_DIR_NAME_JVM_PARAM,
283 ZmicerTestUtils.getFullTestDataFolderName(claz, considerMethodName, testCase + "not_existed", false));
284 }
285
286 /**
287 * Reset XML related JVM settings which were set at the method
288 * {@link JPatternsTestUtils#setJVMXmlParams(Class,boolean,String,String,boolean)}
289 */
290 public static void resetJVMXmlParams()
291 {
292 SystemPropsUtils.clearProps(JPConstants.XMLConfigFilesConstants.DEFAULT_XML_FRAMEWORK_CONFIG_FILE_NAME_JVM_PARAM,
293 JPConstants.XMLConfigFilesConstants.CUSTOM_XML_FRAMEWORK_CONFIG_FILE_NAME_JVM_PARAM,
294 JPConstants.XMLConfigFilesConstants.DEFAULT_XML_CONSUMER_CONFIG_DIR_NAME_JVM_PARAM,
295 JPConstants.XMLConfigFilesConstants.CUSTOM_XML_CONSUMER_CONFIG_DIR_NAME_JVM_PARAM);
296 }
297
298 /**
299 * Perform resetting all the init stuff - for both props and XML namings
300 */
301 public static void resetAll()
302 {
303 resetJVMProps();
304 resetJVMXmlParams();
305 }
306 }