001: /*
002: DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:
004: Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:
006:
007: The contents of this file are subject to the terms of either the GNU
008: General Public License Version 2 only ("GPL") or the Common
009: Development and Distribution License("CDDL") (collectively, the
010: "License"). You may not use this file except in compliance with the
011: License. You can obtain a copy of the License at
012: http://www.netbeans.org/cddl-gplv2.html
013: or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: specific language governing permissions and limitations under the
015: License. When distributing the software, include this License Header
016: Notice in each file and include the License file at
017: nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: particular file as subject to the "Classpath" exception as provided
019: by Sun in the GPL Version 2 section of the License file that
020: accompanied this code. If applicable, add the following below the
021: License Header, with the fields enclosed by brackets [] replaced by
022: your own identifying information:
023: "Portions Copyrighted [year] [name of copyright owner]"
024:
025: Contributor(s):
026:
027: The Original Software is NetBeans. The Initial Developer of the Original
028: Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
029: Microsystems, Inc. All Rights Reserved.
030:
031: If you wish your version of this file to be governed by only the CDDL
032: or only the GPL Version 2, indicate your decision by adding
033: "[Contributor] elects to include this software in this distribution
034: under the [CDDL or GPL Version 2] license." If you do not indicate a
035: single choice of license, a recipient has the option to distribute
036: your version of this file under either the CDDL, the GPL Version 2 or
037: to extend the choice of license to its licensees as provided above.
038: However, if you add GPL Version 2 code and therefore, elected the GPL
039: Version 2 license, then the option applies only if the new code is
040: made subject to such option by the copyright holder.
041: */
042: package org.netbeans.test.dataprovider.common;
043:
044: import java.util.regex.*;
045: import java.util.*;
046: import java.io.*;
047:
048: public class TestPropertiesHandler implements Constants {
049: private static Properties testProperties;
050: private static List<Properties> listServerProperties,
051: listDatabaseProperties;
052: private static int serverPropertiesIndex = 0,
053: databasePropertiesIndex = 0;
054:
055: public static String getTestProperty(String propertyKey) {
056: return testProperties.getProperty(propertyKey);
057: }
058:
059: public static String getServerProperty(String propertyKey) {
060: //Utils.logMsg("serverPropertiesIndex = " + serverPropertiesIndex);
061: return listServerProperties.get(serverPropertiesIndex)
062: .getProperty(propertyKey);
063: }
064:
065: public static String getDatabaseProperty(String propertyKey) {
066: //Utils.logMsg("databasePropertiesIndex = " + databasePropertiesIndex);
067: return listDatabaseProperties.get(databasePropertiesIndex)
068: .getProperty(propertyKey);
069: }
070:
071: public static void readTestProperties() {
072: testProperties = getTestWorkdirProperties();
073: printProperties(testProperties);
074: printProperties(System.getProperties());
075: listServerProperties = handleServerProperties(testProperties
076: .getProperty(PROPERTY_NAME_SERVER_SETTINGS),
077: testProperties);
078: listDatabaseProperties = handleDatabaseProperties(
079: testProperties
080: .getProperty(PROPERTY_NAME_DATABASE_SETTINGS),
081: listServerProperties, testProperties);
082: resetPropertiesIndexes();
083: }
084:
085: public static List<String> getTestMethodNames(
086: String[] baseTestMethodNames) {
087: if (listServerProperties.isEmpty()) {
088: throw new RuntimeException(
089: "Server properties aren't defined");
090: }
091: if (listDatabaseProperties.isEmpty()) {
092: throw new RuntimeException(
093: "Database properties aren't defined");
094: }
095: List<String> methodNames = new ArrayList<String>();
096: do {
097: for (String methodName : baseTestMethodNames) {
098: methodNames.add(methodName);
099: }
100: } while (nextTestProperties());
101: resetPropertiesIndexes();
102: return methodNames;
103: }
104:
105: public static void resetPropertiesIndexes() {
106: databasePropertiesIndex = 0;
107: serverPropertiesIndex = 0;
108: }
109:
110: public static boolean nextTestProperties() {
111: boolean isAllPropertiesHandled = false;
112: ++databasePropertiesIndex;
113: if (databasePropertiesIndex == listDatabaseProperties.size()) {
114: databasePropertiesIndex = 0;
115: ++serverPropertiesIndex;
116: }
117: if (serverPropertiesIndex == listServerProperties.size()) {
118: resetPropertiesIndexes();
119: isAllPropertiesHandled = true;
120: }
121: return (!isAllPropertiesHandled);
122: }
123:
124: public static List<Properties> handleServerProperties(
125: String serverSettings, Properties commonProperties) {
126: List<String> listSeparatedProperties = separateServerProperties(serverSettings);
127: List<Properties> listSettingsProperties = parseServerProperties(listSeparatedProperties);
128: List<Properties> listResolvedProperties = resolveServerProperties(
129: listSettingsProperties, commonProperties);
130:
131: Utils.logMsg("=== Server Settings: List of Properties ===");
132: for (Properties resolvedProperties : listResolvedProperties) {
133: printProperties(resolvedProperties);
134: Utils.logMsg("-------------------------------------------");
135: }
136: return listResolvedProperties;
137: }
138:
139: private static List<String> separateServerProperties(
140: String serverSettings) {
141: return separateSettingsProperties(serverSettings);
142: }
143:
144: private static List<Properties> parseServerProperties(
145: List<String> listSettingsProperties) {
146: return parseItemsProperties(listSettingsProperties);
147: }
148:
149: private static List<Properties> resolveServerProperties(
150: List<Properties> listUnresolvedProperties,
151: Properties commonTestProperties) {
152: List<Properties> listRefProperties = new ArrayList<Properties>();
153: listRefProperties.add(System.getProperties());
154: listRefProperties.add(commonTestProperties);
155: return resolveProperies(listUnresolvedProperties,
156: listRefProperties);
157: }
158:
159: public static List<Properties> handleDatabaseProperties(
160: String databaseSettings,
161: List<Properties> listServerProperties,
162: Properties commonTestProperties) {
163: List<String> listSeparatedProperties = separateDatabaseProperties(databaseSettings);
164: List<Properties> listSettingsProperties = parseDatabaseProperties(listSeparatedProperties);
165: List<Properties> listResolvedProperties = resolveDatabaseProperties(
166: listSettingsProperties, listServerProperties,
167: commonTestProperties);
168:
169: Utils.logMsg("=== Database Settings: List of Properties ===");
170: for (Properties resolvedProperties : listResolvedProperties) {
171: printProperties(resolvedProperties);
172: Utils
173: .logMsg("---------------------------------------------");
174: }
175: return listResolvedProperties;
176: }
177:
178: private static List<String> separateDatabaseProperties(
179: String databaseSettings) {
180: return separateSettingsProperties(databaseSettings);
181: }
182:
183: private static List<Properties> parseDatabaseProperties(
184: List<String> listSettingsProperties) {
185: return parseItemsProperties(listSettingsProperties);
186: }
187:
188: private static List<Properties> resolveDatabaseProperties(
189: List<Properties> listUnresolvedProperties,
190: List<Properties> listServerProperties,
191: Properties commonTestProperties) {
192: List<Properties> listRefProperties = new ArrayList<Properties>();
193: listRefProperties.add(System.getProperties());
194: listRefProperties.add(commonTestProperties);
195: listRefProperties.addAll(listServerProperties);
196: return resolveProperies(listUnresolvedProperties,
197: listRefProperties);
198: }
199:
200: private static List<String> separateSettingsProperties(
201: String dataSettings) {
202: dataSettings = dataSettings.replace(PROP_SPEC_CHAR_SLASH,
203: PROP_SPEC_CHAR_DOUBLE_PERCENT);
204: //Utils.debugOutput("+++ modified dataSettings = [" + dataSettings + "]");
205:
206: String propertyPattern = PROP_SETTINGS_PATTERN;
207: //Utils.debugOutput("+++ propertyPattern = [" + propertyPattern + "]");
208:
209: List<String> propDataList = new ArrayList<String>();
210: try {
211: Matcher matcher = (Pattern.compile(propertyPattern))
212: .matcher(dataSettings);
213: while (matcher.find()) {
214: String data = matcher.group();
215: propDataList.add(data);
216: //Utils.debugOutput("\t+++ data = " + data);
217: }
218: } catch (Exception e) {
219: e.printStackTrace();
220: throw new RuntimeException(e);
221: }
222: //Utils.debugOutput("\t+++ propDataList = " + propDataList);
223: return propDataList;
224: }
225:
226: private static List<Properties> parseItemsProperties(
227: List<String> listItemData) {
228: String propertyPattern = PROP_ITEM_LIST_PATTERN;
229: //Utils.debugOutput("+++ propertyPattern = " + propertyPattern);
230:
231: List<Properties> propDataList = new ArrayList<Properties>();
232: try {
233: for (String itemData : listItemData) {
234: Properties itemsProperties = new Properties();
235:
236: //Utils.debugOutput("+++ itemData = " + itemData);
237: Matcher matcher = (Pattern.compile(propertyPattern))
238: .matcher(itemData);
239: while (matcher.find()) {
240: String data = matcher.group();
241: //Utils.debugOutput("\t+++ data = " + data);
242: addItemProperties(data, itemsProperties);
243: }
244: propDataList.add(itemsProperties);
245: }
246: } catch (Exception e) {
247: e.printStackTrace();
248: throw new RuntimeException(e);
249: }
250: //Utils.debugOutput("\t+++ propDataList = " + propDataList);
251:
252: for (Properties propData : propDataList) {
253: Enumeration enumPropNames = propData.propertyNames();
254: while (enumPropNames.hasMoreElements()) {
255: String propName = (String) enumPropNames.nextElement(), propValue = propData
256: .getProperty(propName);
257: propData.setProperty(propName, propValue.replace(
258: PROP_SPEC_CHAR_DOUBLE_PERCENT,
259: PROP_SPEC_CHAR_SLASH));
260: }
261: }
262: //Utils.debugOutput("\t+++ modified propDataList = " + propDataList);
263: return propDataList;
264: }
265:
266: private static void addItemProperties(String itemData,
267: Properties itemsProperties) {
268: itemData = itemData.replace(LEFT_CURLY_BRACKET, "").replace(
269: RIGHT_CURLY_BRACKET, "").trim();
270: int equalPos = itemData.indexOf(EQUAL_SIGN);
271: if (equalPos == 0) {
272: throw new RuntimeException(
273: "Property data ["
274: + itemData
275: + "] is incorrect: "
276: + "it doesn't contain the property name before the character ["
277: + EQUAL_SIGN + "]");
278: }
279: if (equalPos > -1) {
280: String name = itemData.substring(0, equalPos).trim();
281: String value = (equalPos == itemData.length() - 1 ? ""
282: : itemData.substring(equalPos + 1).trim());
283: //Utils.debugOutput("equalPos = " + equalPos, "name = [" + name + "]", "value = [" + value + "]");
284: itemsProperties.setProperty(name, value);
285: } else {
286: throw new RuntimeException("Property data [" + itemData
287: + "] is incorrect: "
288: + "it must contain the character [" + EQUAL_SIGN
289: + "]");
290: }
291: }
292:
293: private static List<Properties> resolveProperies(
294: List<Properties> listHandledProperties,
295: List<Properties> listRefProperies) {
296: for (Properties modifiedProperties : listHandledProperties) {
297: Set<Map.Entry<Object, Object>> entrySet = modifiedProperties
298: .entrySet();
299: for (Map.Entry<Object, Object> mapEntry : entrySet) {
300: String currentPpropertyValue = null, refKey = null;
301: while ((refKey = getReferenceKey(currentPpropertyValue = ((String) mapEntry
302: .getValue()))) != null) {
303: String refValue = modifiedProperties
304: .getProperty(refKey);
305: if (Utils.isStringEmpty(refValue)) {
306: for (Properties refProperties : listRefProperies) {
307: refValue = refProperties
308: .getProperty(refKey);
309: if (!Utils.isStringEmpty(refValue))
310: break;
311: }
312: }
313: if (!Utils.isStringEmpty(refValue)) {
314: mapEntry.setValue(replaceReferenceValue(
315: currentPpropertyValue, refValue));
316: } else {
317: throw new RuntimeException(
318: mapEntry.getKey()
319: + " = "
320: + currentPpropertyValue
321: + ": reference value for reference key ["
322: + refKey + "] isn't found");
323: }
324: }
325: }
326: }
327: return listHandledProperties;
328: }
329:
330: private static String getReferenceKey(String value) {
331: int leftBracketPos = value.indexOf(LEFT_ANGLE_BRACKET), rightBracketPos = value
332: .indexOf(RIGHT_ANGLE_BRACKET);
333: if ((leftBracketPos < 0) && (rightBracketPos < 0))
334: return null;
335:
336: String refKey = value.substring(leftBracketPos + 1,
337: rightBracketPos).trim();
338: return refKey;
339: }
340:
341: private static String replaceReferenceValue(String value,
342: String refValue) {
343: int leftBracketPos = value.indexOf(LEFT_ANGLE_BRACKET), rightBracketPos = value
344: .indexOf(RIGHT_ANGLE_BRACKET);
345: String newValue = value.substring(0, leftBracketPos) + refValue
346: + value.substring(rightBracketPos + 1);
347: //Utils.debugOutput("value = " + value, "refValue = " + refValue, "newValue = " + newValue);
348: return newValue;
349: }
350:
351: public static Properties getTestWorkdirProperties() {
352: Properties properties = null;
353: String xtestWorkDir = System.getProperty("xtest.workdir");
354: String[] propertyFileNames = new File(xtestWorkDir).list();
355: for (String propertyFileName : propertyFileNames) {
356: if (propertyFileName.toLowerCase().endsWith("properties")) {
357: properties = addPropertiesFromFile(xtestWorkDir + "/"
358: + propertyFileName, properties);
359: }
360: }
361: return properties;
362: }
363:
364: public static Properties addPropertiesFromFile(
365: String propertiesFilePath, Properties baseProperties) {
366: if (baseProperties == null) {
367: baseProperties = new Properties();
368: }
369: Properties properties = new Properties();
370: try {
371: properties.load(new FileInputStream(propertiesFilePath));
372: } catch (Exception e) {
373: e.printStackTrace(Utils.logStream);
374: return null;
375: }
376: if (!properties.isEmpty()) {
377: for (Object key : properties.keySet()) {
378: baseProperties.put(key, properties
379: .getProperty((String) key));
380: }
381: }
382: return baseProperties;
383: }
384:
385: public static void printProperties(Properties properties) {
386: printProperties(Utils.logStream, properties);
387: }
388:
389: public static void printProperties(PrintStream out,
390: Properties properties) {
391: Enumeration propNames = properties.propertyNames();
392: java.util.List<String> buf = new ArrayList<String>();
393: while (propNames.hasMoreElements()) {
394: String propName = (String) propNames.nextElement();
395: buf.add(propName + " = ["
396: + properties.getProperty(propName) + "]");
397: }
398: Utils.debugOutput(out, buf.toArray());
399: }
400: }
|