001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.testsupport; 021 022 import java.io.File; 023 024 import junit.framework.TestCase; 025 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * Provides support for tests. 031 * 032 * @version $Rev: 476321 $ $Date: 2006-11-17 16:18:49 -0500 (Fri, 17 Nov 2006) $ 033 */ 034 public abstract class TestSupport 035 extends TestCase 036 { 037 /** 038 * The base-directory which tests should be run from. 039 * 040 * @see #getBaseDir() This field is initialized from the return of this method on instance construction. 041 */ 042 protected final File BASEDIR = getBaseDir(); 043 044 // 045 // NOTE: Logging must be initialized after BASEDIR has been discovered, as it is used 046 // by the log4j logging-config properties to set the target/test.log file. 047 // 048 049 /** 050 * Instance logger which tests should use to produce tracing information. 051 * 052 * <p> 053 * Unless you have a really good reason to, do not change this field from your sub-class. 054 * And if you do, please document why you have done so. 055 */ 056 protected Log log = LogFactory.getLog(getClass()); 057 058 /** 059 * Constructor for tests that specify a specific test name. 060 * 061 * @see #TestSupport() This is the prefered constructor for sub-classes to use. 062 */ 063 protected TestSupport(final String name) { 064 super(name); 065 066 log.info("Initialized"); 067 } 068 069 /** 070 * Default constructor. 071 */ 072 protected TestSupport() { 073 super(); 074 075 log.info("Initialized"); 076 } 077 078 /** 079 * Determine the value of <tt>${basedir}</tt>, which should be the base directory of 080 * the module which the concreate test class is defined in. 081 * 082 * <p> 083 * If The system property <tt>basedir</tt> is already set, then that value is used, 084 * otherwise we determine the value from the codesource of the containing concrete class 085 * and set the <tt>basedir</tt> system property to that value. 086 * 087 * @see #BASEDIR This field is always initialized to the value which this method returns. 088 * 089 * @return The base directory of the module which contains the concreate test class. 090 */ 091 protected final File getBaseDir() { 092 File dir; 093 094 // If ${basedir} is set, then honor it 095 String tmp = System.getProperty("basedir"); 096 if (tmp != null) { 097 dir = new File(tmp); 098 } 099 else { 100 // Find the directory which this class (or really the sub-class of TestSupport) is defined in. 101 String path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); 102 103 // We expect the file to be in target/test-classes, so go up 2 dirs 104 dir = new File(path).getParentFile().getParentFile(); 105 106 // Set ${basedir} which is needed by logging to initialize 107 System.setProperty("basedir", dir.getPath()); 108 } 109 110 // System.err.println("Base Directory: " + dir); 111 112 return dir; 113 } 114 115 /** 116 * Resolve the given path to a file rooted to {@link #BASEDIR}. 117 * 118 * @param path The path to resolve. 119 * @return The resolved file for the given path. 120 */ 121 protected final File resolveFile(final String path) { 122 assert path != null; 123 124 File file = new File(path); 125 126 // Complain if the file is already absolute... probably an error 127 if (file.isAbsolute()) { 128 log.warn("Given path is already absolute; nothing to resolve: " + file); 129 } 130 else { 131 file = new File(BASEDIR, path); 132 } 133 134 return file; 135 } 136 137 /** 138 * Resolve the given path to a path rooted to {@link #BASEDIR}. 139 * 140 * @param path The path to resolve. 141 * @return The resolved path for the given path. 142 * 143 * @see #resolveFile(String) 144 */ 145 protected final String resolvePath(final String path) { 146 assert path != null; 147 148 return resolveFile(path).getPath(); 149 } 150 }