diff --git a/projects/samskivert/src/java/com/samskivert/test/TestUtil.java b/projects/samskivert/src/java/com/samskivert/test/TestUtil.java new file mode 100644 index 00000000..87422227 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/test/TestUtil.java @@ -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 + * JUnit 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 test_dir which is the + * path to the project top-level test directory and that the top-level + * test directory contains a subdirectory named rsrc, 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 + * rsrc 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 rsrc. */ + protected static final String RESOURCE_DIR = "rsrc"; +} diff --git a/projects/samskivert/tests/README b/projects/samskivert/tests/README new file mode 100644 index 00000000..0085edf1 --- /dev/null +++ b/projects/samskivert/tests/README @@ -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 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 + for creating test cases. + +Test classfiles should be named Test.java where 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 $ diff --git a/projects/samskivert/tests/build.xml b/projects/samskivert/tests/build.xml new file mode 100644 index 00000000..9e1f20c1 --- /dev/null +++ b/projects/samskivert/tests/build.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/samskivert/tests/rsrc/servlet/srl/README b/projects/samskivert/tests/rsrc/servlet/srl/README new file mode 100644 index 00000000..0fefd852 --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/README @@ -0,0 +1 @@ +Test data for com.samskivert.servlet.SiteResourceLoaderTest diff --git a/projects/samskivert/tests/rsrc/servlet/srl/default/body.txt b/projects/samskivert/tests/rsrc/servlet/srl/default/body.txt new file mode 100644 index 00000000..b0bb2e9c --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/default/body.txt @@ -0,0 +1 @@ +This is the default body. diff --git a/projects/samskivert/tests/rsrc/servlet/srl/default/footer.txt b/projects/samskivert/tests/rsrc/servlet/srl/default/footer.txt new file mode 100644 index 00000000..1e29a7fe --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/default/footer.txt @@ -0,0 +1 @@ +This is the default footer. diff --git a/projects/samskivert/tests/rsrc/servlet/srl/default/header.txt b/projects/samskivert/tests/rsrc/servlet/srl/default/header.txt new file mode 100644 index 00000000..8d0d22d5 --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/default/header.txt @@ -0,0 +1 @@ +This is the default header. diff --git a/projects/samskivert/tests/rsrc/servlet/srl/defaultout.txt b/projects/samskivert/tests/rsrc/servlet/srl/defaultout.txt new file mode 100644 index 00000000..9baee214 --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/defaultout.txt @@ -0,0 +1,3 @@ +This is the default header. +This is the default body. +This is the default footer. diff --git a/projects/samskivert/tests/rsrc/servlet/srl/site1.jar b/projects/samskivert/tests/rsrc/servlet/srl/site1.jar new file mode 100644 index 00000000..f793c5c5 Binary files /dev/null and b/projects/samskivert/tests/rsrc/servlet/srl/site1.jar differ diff --git a/projects/samskivert/tests/rsrc/servlet/srl/site1out.txt b/projects/samskivert/tests/rsrc/servlet/srl/site1out.txt new file mode 100644 index 00000000..58ff7946 --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/site1out.txt @@ -0,0 +1,3 @@ +This is the site1 header. +This is the default body. +This is the site1 footer. diff --git a/projects/samskivert/tests/rsrc/servlet/srl/site2.jar b/projects/samskivert/tests/rsrc/servlet/srl/site2.jar new file mode 100644 index 00000000..4298b320 Binary files /dev/null and b/projects/samskivert/tests/rsrc/servlet/srl/site2.jar differ diff --git a/projects/samskivert/tests/rsrc/servlet/srl/site2out.txt b/projects/samskivert/tests/rsrc/servlet/srl/site2out.txt new file mode 100644 index 00000000..f3abede8 --- /dev/null +++ b/projects/samskivert/tests/rsrc/servlet/srl/site2out.txt @@ -0,0 +1,3 @@ +This is the site2 header. +This is the default body. +This is the site2 footer.