001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.junit;
020:
021: import java.io.File;
022: import java.util.MissingResourceException;
023:
024: import junit.framework.TestCase;
025: import org.apache.jmeter.util.JMeterUtils;
026: import org.apache.jorphan.logging.LoggingManager;
027: import org.apache.log.Logger;
028:
029: /*
030: * Extend JUnit TestCase to provide common setup
031: */
032: public abstract class JMeterTestCase extends TestCase {
033: // Used by findTestFile
034: private static final String filePrefix;
035:
036: public JMeterTestCase() {
037: super ();
038: }
039:
040: public JMeterTestCase(String name) {
041: super (name);
042: }
043:
044: /*
045: * If not running under AllTests.java, make sure that the properties (and
046: * log file) are set up correctly.
047: *
048: * N.B. In order for this to work correctly, the JUnit test must be started
049: * in the bin directory, and all the JMeter jars (plus any others needed at
050: * run-time) need to be on the classpath.
051: *
052: */
053: static {
054: if (JMeterUtils.getJMeterProperties() == null) {
055: String file = "testfiles/jmetertest.properties";
056: File f = new File(file);
057: if (!f.canRead()) {
058: System.out.println("Can't find " + file
059: + " - trying bin directory");
060: file = "bin/" + file;// JMeterUtils assumes Unix-style
061: // separators
062: // Also need to set working directory so test files can be found
063: System.setProperty("user.dir", System
064: .getProperty("user.dir")
065: + File.separatorChar + "bin");
066: System.out.println("Setting user.dir="
067: + System.getProperty("user.dir"));
068: filePrefix = "bin/";
069: } else {
070: filePrefix = "";
071: }
072: // Used to be done in initializeProperties
073: String home = new File(System.getProperty("user.dir"))
074: .getParent();
075: System.out.println("Setting JMeterHome: " + home);
076: JMeterUtils.setJMeterHome(home);
077: JMeterUtils jmu = new JMeterUtils();
078: try {
079: jmu.initializeProperties(file);
080: } catch (MissingResourceException e) {
081: System.out
082: .println("** Can't find resources - continuing anyway **");
083: }
084: logprop("java.version");
085: logprop("java.vendor");
086: logprop("java.home");
087: logprop("user.home");
088: logprop("user.dir");
089: logprop("java.class.version");
090: logprop("os.name");
091: logprop("os.version");
092: logprop("os.arch");
093: logprop("java.class.path");
094: // String cp = System.getProperty("java.class.path");
095: // String cpe[]= JOrphanUtils.split(cp,File.pathSeparator);
096: // System.out.println("java.class.path=");
097: // for (int i=0;i<cpe.length;i++){
098: // System.out.println(cpe[i]);
099: // }
100: } else {
101: filePrefix = "";
102: }
103: }
104:
105: private static void logprop(String prop) {
106: System.out.println(prop + "=" + System.getProperty(prop));
107: }
108:
109: // Helper method to find a file
110: protected static File findTestFile(String file) {
111: File f = new File(file);
112: if (filePrefix.length() > 0 && !f.isAbsolute()) {
113: f = new File(filePrefix + file);// Add the offset
114: }
115: return f;
116: }
117:
118: protected static final Logger testLog = LoggingManager
119: .getLoggerForClass();
120: }
|