Created a testing framework for the samskivert library based on JUnit and
ant. The test drivers are located in a separate hierarchy and are built and invoked as a single ant target. git-svn-id: https://samskivert.googlecode.com/svn/trunk@419 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -0,0 +1,95 @@
|
|||||||
|
//
|
||||||
|
// $Id: TestUtil.java,v 1.1 2001/11/05 09:13:24 mdb Exp $
|
||||||
|
//
|
||||||
|
// samskivert library - useful routines for java programs
|
||||||
|
// Copyright (C) 2001 Michael Bayne
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
package com.samskivert.test;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import com.samskivert.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utilities used by unit tests for the samskivert library which are
|
||||||
|
* potentially useful for projects that wish to implement tests in the
|
||||||
|
* same manner. The samskivert unit tests are built using the
|
||||||
|
* <a href="http://junit.org">JUnit</a> testing framework.
|
||||||
|
*/
|
||||||
|
public class TestUtil
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns the path via which a test-related resource can be loaded.
|
||||||
|
* This assumes that the mechanism used to invoke the test code
|
||||||
|
* defined a system property named <code>test_dir</code> which is the
|
||||||
|
* path to the project top-level test directory and that the top-level
|
||||||
|
* test directory contains a subdirectory named <code>rsrc</code>, to
|
||||||
|
* which the supplied path will be appended to obtain the path to the
|
||||||
|
* resource.
|
||||||
|
*
|
||||||
|
* @param path the path to the resource, relative to the
|
||||||
|
* <code>rsrc</code> directory in the top-level test directory. It
|
||||||
|
* should contain a leading slash but one will be provided if
|
||||||
|
* necessary.
|
||||||
|
*/
|
||||||
|
public static String getResourcePath (String path)
|
||||||
|
{
|
||||||
|
String testdir = System.getProperty("test_dir");
|
||||||
|
if (testdir == null) {
|
||||||
|
Log.warning("The 'test_dir' system property was not set " +
|
||||||
|
"to the top-level test directory.");
|
||||||
|
// fake it and hope for the best
|
||||||
|
testdir = ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuffer rpath = new StringBuffer(testdir);
|
||||||
|
if (rpath.charAt(rpath.length()-1) != '/') {
|
||||||
|
rpath.append("/");
|
||||||
|
}
|
||||||
|
rpath.append(RESOURCE_DIR);
|
||||||
|
if (!path.startsWith("/")) {
|
||||||
|
rpath.append("/");
|
||||||
|
}
|
||||||
|
rpath.append(path);
|
||||||
|
|
||||||
|
return rpath.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an input stream via which a test-related resource can be
|
||||||
|
* loaded. The path is constructed as in {@link #getResourcePath}.
|
||||||
|
*
|
||||||
|
* @param path the path to the resource (see {@link
|
||||||
|
* #getResourcePath}).
|
||||||
|
*
|
||||||
|
* @exception FileNotFoundException thrown if the resource file with
|
||||||
|
* the specified path does not exist.
|
||||||
|
*/
|
||||||
|
public static InputStream getResourceAsStream (String path)
|
||||||
|
throws FileNotFoundException
|
||||||
|
{
|
||||||
|
// get the path to the resource and return a file input stream
|
||||||
|
// from which to load it
|
||||||
|
return new FileInputStream(getResourcePath(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The name of the directory in the top-level test directory that
|
||||||
|
* contains test-related resources. This is <code>rsrc</code>. */
|
||||||
|
protected static final String RESOURCE_DIR = "rsrc";
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
samskivert tests
|
||||||
|
----------------
|
||||||
|
|
||||||
|
This is where all test code for the samskivert library exists. Where
|
||||||
|
possible unit tests should be created for all services provided by the
|
||||||
|
library. Tests are implemented using JUnit <http://junit.org> where
|
||||||
|
applicable and automatically invoked from ant.
|
||||||
|
|
||||||
|
Running the tests
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
First build the main library (by invoking ant in the top-level project
|
||||||
|
directory) and then the tests can be built and invoked via ant:
|
||||||
|
|
||||||
|
% ant test
|
||||||
|
|
||||||
|
which will build and then invoke the test code.
|
||||||
|
|
||||||
|
Adding new tests
|
||||||
|
----------------
|
||||||
|
|
||||||
|
Unit tests are implemented using JUnit which means that they should extend
|
||||||
|
junit.framework.TestCase and follow the guidelines outlined here
|
||||||
|
<http://junit.sourceforge.net/> for creating test cases.
|
||||||
|
|
||||||
|
Test classfiles should be named <foo>Test.java where <foo> is the
|
||||||
|
associated class in the samskivert library for which a test is being
|
||||||
|
implemented. For example, a test for the
|
||||||
|
com.samskivert.servlet.SiteResourceLoader class would be named
|
||||||
|
com.samskivert.servlet.SiteResourceLoaderTest.
|
||||||
|
|
||||||
|
If multiple test drivers for a single class are desired, they can deviate
|
||||||
|
from the prescribed naming scheme but must still end with Test.java so
|
||||||
|
that ant will automatically locate and invoke them as part of the build
|
||||||
|
process.
|
||||||
|
|
||||||
|
Test implementors will want to look at existing tests as a starting point
|
||||||
|
and at the test support classes in the com.samskivert.test package (which
|
||||||
|
is part of the main samskivert library source tree, not the test source
|
||||||
|
tree).
|
||||||
|
|
||||||
|
$Id: README,v 1.1 2001/11/05 09:13:25 mdb Exp $
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<!-- build configuration -->
|
||||||
|
<project name="samskivert tests" default="test" basedir=".">
|
||||||
|
|
||||||
|
<!-- things you may want to change -->
|
||||||
|
<property name="build.compiler" value="jikes"/>
|
||||||
|
<property name="java.libraries" value="/usr/share/java"/>
|
||||||
|
<property name="junit.fork" value="false"/>
|
||||||
|
|
||||||
|
<!-- things you probably don't want to change -->
|
||||||
|
<property name="test.dir" value="."/>
|
||||||
|
<property name="src.dir" value="src/java"/>
|
||||||
|
<property name="deploy.dir" value="dist"/>
|
||||||
|
|
||||||
|
<!-- declare our classpath -->
|
||||||
|
<path id="classpath">
|
||||||
|
<pathelement location="../${deploy.dir}/classes"/>
|
||||||
|
<pathelement location="${deploy.dir}/classes"/>
|
||||||
|
<fileset dir="../lib" includes="**/*.jar"/>
|
||||||
|
<fileset dir="${java.libraries}" includes="**/*.jar"/>
|
||||||
|
</path>
|
||||||
|
|
||||||
|
<!-- prepares the application directories -->
|
||||||
|
<target name="prepare">
|
||||||
|
<mkdir dir="${deploy.dir}"/>
|
||||||
|
<mkdir dir="${deploy.dir}/classes"/>
|
||||||
|
<copy todir="${deploy.dir}/classes">
|
||||||
|
<fileset dir="${src.dir}" includes="**/*.properties"/>
|
||||||
|
</copy>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<!-- cleans out the installed application -->
|
||||||
|
<target name="clean">
|
||||||
|
<delete dir="${deploy.dir}"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<!-- build the java class files -->
|
||||||
|
<target name="compile" depends="prepare">
|
||||||
|
<javac srcdir="${src.dir}" destdir="${deploy.dir}/classes"
|
||||||
|
debug="on" optimize="off" deprecation="off">
|
||||||
|
<classpath refid="classpath"/>
|
||||||
|
</javac>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<!-- run the tests -->
|
||||||
|
<target name="test" depends="compile" description="Run tests.">
|
||||||
|
<junit printsummary="no" haltonfailure="yes" fork="${junit.fork}">
|
||||||
|
<classpath refid="classpath"/>
|
||||||
|
<sysproperty key="test_dir" value="${test.dir}"/>
|
||||||
|
<formatter type="brief" usefile="false"/>
|
||||||
|
<batchtest>
|
||||||
|
<fileset dir="${src.dir}">
|
||||||
|
<include name="**/*Test*"/>
|
||||||
|
</fileset>
|
||||||
|
</batchtest>
|
||||||
|
</junit>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Test data for com.samskivert.servlet.SiteResourceLoaderTest
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
This is the default body.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
This is the default footer.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
This is the default header.
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
This is the default header.
|
||||||
|
This is the default body.
|
||||||
|
This is the default footer.
|
||||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
|||||||
|
This is the site1 header.
|
||||||
|
This is the default body.
|
||||||
|
This is the site1 footer.
|
||||||
Binary file not shown.
@@ -0,0 +1,3 @@
|
|||||||
|
This is the site2 header.
|
||||||
|
This is the default body.
|
||||||
|
This is the site2 footer.
|
||||||
Reference in New Issue
Block a user