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: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.junit;
043:
044: import java.lang.reflect.*;
045: import java.io.*;
046: import java.util.*;
047: import org.netbeans.junit.diff.*;
048:
049: /**
050: *
051: * @author vstejskal
052: */
053: public class Manager extends Object {
054:
055: /** Creates new Manager */
056: private Manager() {
057: }
058:
059: public static final String JUNIT_PROPERTIES_FILENAME = "junit.properties";
060: public static final String JUNIT_PROPERTIES_LOCATION_PROPERTY = "junit.properties.file";
061:
062: protected static final String PROP_DIFF_IMPL = "nbjunit.diff.impl";
063: protected static final String DEFAULT_DIFF_IMPL = "org.netbeans.junit.diff.SimpleDiff";
064: protected static Diff systemDiff = null;
065: protected static Properties fPreferences = null;
066:
067: // workdir stuff
068: /**
069: * name of the system property defining root workdir direcory
070: * - it must be set before running tests using workdir
071: * - in the case of running tests from XTest framework, this
072: * property is set by the framework itself
073: * - otherwise the default is ${java.io.tmpdir}/tests
074: */
075: public static final String NBJUNIT_WORKDIR = "nbjunit.workdir";
076:
077: // nbjunit home dir - directory where nbjunit.jar and other files are stored
078: public static final String NBJUNIT_HOME = "nbjunit.home";
079:
080: static {
081: fPreferences = new Properties();
082: fPreferences.put(PROP_DIFF_IMPL, DEFAULT_DIFF_IMPL);
083: }
084:
085: public static Diff getSystemDiff() {
086: String diffImplName;
087:
088: if (null == systemDiff) {
089: readProperties();
090: diffImplName = fPreferences.getProperty(PROP_DIFF_IMPL);
091: systemDiff = instantiateDiffImpl(diffImplName);
092: if (null == systemDiff
093: && !diffImplName.equals(DEFAULT_DIFF_IMPL)) {
094: systemDiff = instantiateDiffImpl(DEFAULT_DIFF_IMPL);
095: }
096: }
097: return systemDiff;
098: }
099:
100: public static String getWorkDirPath() {
101: String path = System.getProperty(NBJUNIT_WORKDIR);
102:
103: if (path == null) {
104: // try to get property from user's settings
105: readProperties();
106: path = fPreferences.getProperty(NBJUNIT_WORKDIR);
107: }
108: if (path != null) {
109: path = path.replace('/', File.separatorChar);
110: } else {
111: // Fallback value, guaranteed to be defined.
112: path = System.getProperty("java.io.tmpdir")
113: + File.separatorChar + "tests-"
114: + System.getProperty("user.name");
115: }
116: return path;
117: }
118:
119: public static String getNbJUnitHomePath() throws IOException {
120: String path = System.getProperty(NBJUNIT_HOME);
121:
122: if (path == null) {
123: // try to get property from user's settings
124: readProperties();
125: path = fPreferences.getProperty(NBJUNIT_HOME);
126: }
127: if (path != null) {
128: path = path.replace('/', File.separatorChar);
129: return path;
130: } else {
131: throw new IOException(
132: "Cannot determine NbJUnit home. Please make sure you have "
133: + NBJUNIT_HOME + " propery set in your "
134: + JUNIT_PROPERTIES_FILENAME + " file.");
135: }
136:
137: }
138:
139: public static File getNbJUnitHome() throws IOException {
140: File nbJUnitHome = normalizeFile(new File(getNbJUnitHomePath()));
141: if (nbJUnitHome.isDirectory()) {
142: return nbJUnitHome;
143: } else {
144: throw new IOException("Property " + NBJUNIT_HOME
145: + " does not point to nbjunit home.");
146: }
147: }
148:
149: protected static Diff instantiateDiffImpl(String diffImplName) {
150: Diff impl = null;
151: Class clazz;
152: Object diffImpl = null;
153: Class[] prmString = null;
154: Method method;
155: Enumeration propNames;
156:
157: try {
158: prmString = new Class[] { Class.forName("java.lang.String") };
159:
160: // instantiate the diff class
161: clazz = Class.forName(diffImplName);
162: diffImpl = clazz.newInstance();
163:
164: if (diffImpl instanceof Diff) {
165: impl = (Diff) diffImpl;
166:
167: propNames = fPreferences.propertyNames();
168: while (propNames.hasMoreElements()) {
169: String propName = (String) propNames.nextElement();
170:
171: if (propName.equals(PROP_DIFF_IMPL)
172: || !propName.startsWith(PROP_DIFF_IMPL))
173: continue;
174:
175: String setter = "set"
176: + propName.substring(PROP_DIFF_IMPL
177: .length() + 1);
178: try {
179: method = clazz.getMethod(setter, prmString);
180: } catch (NoSuchMethodException e) {
181: System.out.println("The method " + setter
182: + " not fond in class " + diffImplName
183: + ".");
184: method = null;
185: }
186: if (null != method)
187: method.invoke(impl, new Object[] { fPreferences
188: .getProperty(propName, "") });
189: }
190: }
191: } catch (Exception e) {
192: // ignore exception
193: }
194: return impl;
195: }
196:
197: private static File getPreferencesFile() {
198: String junitPropertiesLocation = System
199: .getProperty(Manager.JUNIT_PROPERTIES_LOCATION_PROPERTY);
200: if (junitPropertiesLocation != null) {
201: File propertyFile = new File(junitPropertiesLocation);
202: if (propertyFile.exists()) {
203: return propertyFile;
204: }
205: }
206: // property file was not found - lets fall back to defaults
207: String home = System.getProperty("user.home");
208: return new File(home, Manager.JUNIT_PROPERTIES_FILENAME);
209: }
210:
211: protected static void readProperties() {
212: InputStream is = null;
213: try {
214: File propFile = getPreferencesFile();
215: is = new FileInputStream(propFile);
216: fPreferences = new Properties(fPreferences);
217: fPreferences.load(is);
218: } catch (IOException e) {
219: try {
220: if (is != null)
221: is.close();
222: } catch (IOException e1) {
223: }
224: }
225: }
226:
227: /**
228: * Normalize java.io.File, that is make sure that returned File has
229: * normalized case on Windows; that old Windows 8.3 filename is normalized;
230: * that Unix symlinks are not followed; that relative path is changed to
231: * absolute; etc.
232: * @param file file to normalize
233: * @return normalized file
234: */
235: public static File normalizeFile(File file) {
236: // taken from org.openide.util.FileUtil
237: if (System.getProperty("os.name").startsWith("Windows")) { // NOI18N
238: // On Windows, best to canonicalize.
239: try {
240: file = file.getCanonicalFile();
241: } catch (IOException e) {
242: System.out.println("getCanonicalFile() on file " + file
243: + " failed. " + e.toString()); // NOI18N
244: // OK, so at least try to absolutize the path
245: file = file.getAbsoluteFile();
246: }
247: } else {
248: // On Unix, do not want to traverse symlinks.
249: file = new File(file.toURI().normalize()).getAbsoluteFile();
250: }
251: return file;
252: }
253: }
|