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
17
18
19
20
21 public class JPatternsTestUtils
22 {
23
24
25
26 final public static Logger LOG = Logger.getLogger(JPatternsTestUtils.class);
27
28
29
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
43
44
45
46
47
48
49
50
51
52
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
68
69
70
71
72
73
74
75
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
135 }
136 if (reloadFactories)
137 {
138 reloadPropertiesManager();
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
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
175 }
176 if (reloadFactories)
177 {
178 reloadPropertiesManager();
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
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
207
208
209
210
211
212
213
214
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
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
236 reloadPropertiesManager();
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
288
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
300
301 public static void resetAll()
302 {
303 resetJVMProps();
304 resetJVMXmlParams();
305 }
306 }