KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > testsupport > TestSupport


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */

19
20 package org.apache.geronimo.testsupport;
21
22 import java.io.File JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Provides support for tests.
31  *
32  * @version $Rev: 476321 $ $Date: 2006-11-17 16:18:49 -0500 (Fri, 17 Nov 2006) $
33  */

34 public abstract class TestSupport
35     extends TestCase
36 {
37     /**
38      * The base-directory which tests should be run from.
39      *
40      * @see #getBaseDir() This field is initialized from the return of this method on instance construction.
41      */

42     protected final File JavaDoc BASEDIR = getBaseDir();
43     
44     //
45
// NOTE: Logging must be initialized after BASEDIR has been discovered, as it is used
46
// by the log4j logging-config properties to set the target/test.log file.
47
//
48

49     /**
50      * Instance logger which tests should use to produce tracing information.
51      *
52      * <p>
53      * Unless you have a really good reason to, do not change this field from your sub-class.
54      * And if you do, please document why you have done so.
55      */

56     protected Log log = LogFactory.getLog(getClass());
57     
58     /**
59      * Constructor for tests that specify a specific test name.
60      *
61      * @see #TestSupport() This is the prefered constructor for sub-classes to use.
62      */

63     protected TestSupport(final String JavaDoc name) {
64         super(name);
65         
66         log.info("Initialized");
67     }
68     
69     /**
70      * Default constructor.
71      */

72     protected TestSupport() {
73         super();
74         
75         log.info("Initialized");
76     }
77     
78     /**
79      * Determine the value of <tt>${basedir}</tt>, which should be the base directory of
80      * the module which the concreate test class is defined in.
81      *
82      * <p>
83      * If The system property <tt>basedir</tt> is already set, then that value is used,
84      * otherwise we determine the value from the codesource of the containing concrete class
85      * and set the <tt>basedir</tt> system property to that value.
86      *
87      * @see #BASEDIR This field is always initialized to the value which this method returns.
88      *
89      * @return The base directory of the module which contains the concreate test class.
90      */

91     protected final File JavaDoc getBaseDir() {
92         File JavaDoc dir;
93
94         // If ${basedir} is set, then honor it
95
String JavaDoc tmp = System.getProperty("basedir");
96         if (tmp != null) {
97             dir = new File JavaDoc(tmp);
98         }
99         else {
100             // Find the directory which this class (or really the sub-class of TestSupport) is defined in.
101
String JavaDoc 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 JavaDoc(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 JavaDoc resolveFile(final String JavaDoc path) {
122         assert path != null;
123         
124         File JavaDoc file = new File JavaDoc(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 JavaDoc(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 JavaDoc resolvePath(final String JavaDoc path) {
146         assert path != null;
147         
148         return resolveFile(path).getPath();
149     }
150 }
151
Popular Tags