/**
 *  Copyright 2006 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.geronimo.testsupport;

import java.io.File;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Provides support for tests.
 *
 * @version $Rev$ $Date$
 */
public abstract class TestSupport
    extends TestCase
{
    /**
     * The base-directory which tests should be run from.
     */
    private static final File BASEDIR;
    
    static {
        //
        // TODO: Add some special magic here to figure this out when we are running from
        //       and IDE, like IDEA or Eclipse.  user.dir/target might work... but need
        //       to validate what env each IDE has this set to.
        //
        
        String tmp = System.getProperty("basedir");
        if (tmp == null) {
            throw new Error("Missing 'basedir' property; tests need this property set to run correctly");
        }
        
        BASEDIR = new File(tmp);
    }
    
    /**
     * Instance logger which tests should use to produce tracing information.
     */
    protected Log log = LogFactory.getLog(getClass());
}

/**
 *  Copyright 2006 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.geronimo.testsupport;

import java.io.File;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Provides support for tests.
 *
 * @version $Rev$ $Date$
 */
public abstract class TestSupport
    extends TestCase
{
    /**
     * The base-directory which tests should be run from.
     *
     * @see #getBaseDir()   This field is initialized from the return of this method on instance construction.
     */
    protected final File BASEDIR = getBaseDir();

    /**
     * Instance logger which tests should use to produce tracing information.
     *
     * <p>
     * Unless you have a really good reason to, do not change this field from your sub-class.
     * And if you do, please document why you have done so.
     */
    protected Log log = LogFactory.getLog(getClass());

    /**
     * Determine the value of <tt>${basedir}</tt>, which should be the base directory of
     * the module which the concreate test class is defined in.
     *
     * <p>
     * If The system property <tt>basedir</tt> is already set, then that value is used,
     * otherwise we determine the value from the codesource of the containing concrete class
     * and set the <tt>basedir</tt> system property to that value.
     *
     * @see #BASEDIR    This field is always initialized to the value which this method returns.
     *
     * @return  The base directory of the module which contains the concreate test class.
     */
    protected final File getBaseDir() {
        File dir;

        // If ${basedir} is set, then honor it
        String tmp = System.getProperty("basedir");
        if (tmp != null) {
            dir = new File(tmp);
        }
        else {
            // Find the directory which this class (or really the sub-class of TestSupport) is defined in.
            String path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();

            // We expect the file to be in target/test-classes, so go up 2 dirs
            dir = new File(path).getParentFile().getParentFile();

            // Set ${basedir} which is needed by logging to initialize
            System.setProperty("basedir", dir.getPath());
        }

        // System.err.println("Base Directory: " + dir);

        return dir;
    }
}

Determine ${basedir} (if not already set) by rooting to the codebase of the containing class