Get rid of TestUtil and related hackery.
The SiteResourceLoader is some ancient stuff that's not used anywhere any more, and there's no use in keeping this crufty crap around to test it. I'd nix it too but I don't want to cause yet more trouble. TestUtil was weird and a bad idea and good riddance to it as well.
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
//
|
|
||||||
// samskivert library - useful routines for java programs
|
|
||||||
// Copyright (C) 2001-2012 Michael Bayne, et al.
|
|
||||||
// http://github.com/samskivert/samskivert/blob/master/COPYING
|
|
||||||
|
|
||||||
package com.samskivert.test;
|
|
||||||
|
|
||||||
import com.samskivert.util.Logger;
|
|
||||||
|
|
||||||
/** Provides a log instance for this package. */
|
|
||||||
class Log {
|
|
||||||
|
|
||||||
static final Logger log = Logger.getLogger("com.samskivert.test");
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
//
|
|
||||||
// samskivert library - useful routines for java programs
|
|
||||||
// Copyright (C) 2001-2012 Michael Bayne, et al.
|
|
||||||
// http://github.com/samskivert/samskivert/blob/master/COPYING
|
|
||||||
|
|
||||||
package com.samskivert.test;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import static com.samskivert.test.Log.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, 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 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 = ".";
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder rpath = new StringBuilder(testdir);
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<!--
|
|
||||||
|
|
||||||
samskivert library - useful routines for java programs
|
|
||||||
Copyright (C) 2001-2012 Michael Bayne, et al.
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
-->
|
|
||||||
</head>
|
|
||||||
<body bgcolor="white">
|
|
||||||
|
|
||||||
Utilities useful when writing unit tests.
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
//
|
|
||||||
// samskivert library - useful routines for java programs
|
|
||||||
// Copyright (C) 2001-2012 Michael Bayne, et al.
|
|
||||||
// http://github.com/samskivert/samskivert/blob/master/COPYING
|
|
||||||
|
|
||||||
package com.samskivert.servlet;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
import org.junit.*;
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import com.samskivert.io.StreamUtil;
|
|
||||||
import com.samskivert.test.TestUtil;
|
|
||||||
|
|
||||||
public class SiteResourceLoaderTest
|
|
||||||
{
|
|
||||||
@BeforeClass
|
|
||||||
public static void setTestDir () {
|
|
||||||
System.setProperty("test_dir", "target/test-classes");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void runTest ()
|
|
||||||
{
|
|
||||||
// we need to fake a couple of things to get the test to work
|
|
||||||
TestSiteIdentifier ident = new TestSiteIdentifier();
|
|
||||||
|
|
||||||
// now create a resource loader and load up some resources
|
|
||||||
SiteResourceLoader loader = new SiteResourceLoader(
|
|
||||||
ident, TestUtil.getResourcePath("servlet/srl"));
|
|
||||||
|
|
||||||
try {
|
|
||||||
testResourceLoader(SiteIdentifier.DEFAULT_SITE_ID,
|
|
||||||
loader, "defaultout.txt");
|
|
||||||
testResourceLoader(SITE1_ID, loader, "site1out.txt");
|
|
||||||
testResourceLoader(SITE2_ID, loader, "site2out.txt");
|
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
ioe.printStackTrace();
|
|
||||||
fail("Caught exception while testing resource loader: " + ioe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void testResourceLoader (int siteId, SiteResourceLoader loader, String compareFile)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
StringBuffer gen = new StringBuffer();
|
|
||||||
appendResource(siteId, gen, loader, "/header.txt");
|
|
||||||
appendResource(siteId, gen, loader, "/body.txt");
|
|
||||||
appendResource(siteId, gen, loader, "/footer.txt");
|
|
||||||
|
|
||||||
StringBuffer cmp = new StringBuffer();
|
|
||||||
compareFile = "servlet/srl/" + compareFile;
|
|
||||||
InputStream cin = TestUtil.getResourceAsStream(compareFile);
|
|
||||||
if (cin == null) {
|
|
||||||
throw new IOException("Unable to load " + compareFile);
|
|
||||||
}
|
|
||||||
cmp.append(StreamUtil.toString(cin, "UTF-8"));
|
|
||||||
|
|
||||||
// Log.info("Loaded resources [cmp=" + compareFile + "]: " + gen);
|
|
||||||
|
|
||||||
// now make sure the strings match
|
|
||||||
assertEquals("Testing " + compareFile, cmp.toString(), gen.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void appendResource (
|
|
||||||
int siteId, StringBuffer buffer, SiteResourceLoader loader, String path)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
InputStream rin = null;
|
|
||||||
|
|
||||||
// load up the site-specific resource
|
|
||||||
try {
|
|
||||||
rin = loader.getResourceAsStream(siteId, path);
|
|
||||||
} catch (FileNotFoundException fnfe) {
|
|
||||||
// fall through
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rin == null) {
|
|
||||||
// fall back to the "default" resource if we couldn't load a
|
|
||||||
// site-specific version
|
|
||||||
String rpath = "servlet/srl/default/" + path;
|
|
||||||
rpath = TestUtil.getResourcePath(rpath);
|
|
||||||
rin = new FileInputStream(rpath);
|
|
||||||
}
|
|
||||||
buffer.append(StreamUtil.toString(rin, "UTF-8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class TestSiteIdentifier implements SiteIdentifier
|
|
||||||
{
|
|
||||||
public int identifySite (HttpServletRequest req)
|
|
||||||
{
|
|
||||||
return DEFAULT_SITE_ID;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSiteString (int siteId)
|
|
||||||
{
|
|
||||||
switch (siteId) {
|
|
||||||
case SITE1_ID: return "site1";
|
|
||||||
case SITE2_ID: return "site2";
|
|
||||||
default: return DEFAULT_SITE_STRING;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSiteId (String siteString)
|
|
||||||
{
|
|
||||||
if ("site1".equals(siteString)) {
|
|
||||||
return SITE1_ID;
|
|
||||||
} else if ("site2".equals(siteString)) {
|
|
||||||
return SITE2_ID;
|
|
||||||
} else {
|
|
||||||
return DEFAULT_SITE_ID;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterator<Site> enumerateSites ()
|
|
||||||
{
|
|
||||||
return null; // not used
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static final int SITE1_ID = 1;
|
|
||||||
protected static final int SITE2_ID = 2;
|
|
||||||
}
|
|
||||||
@@ -13,7 +13,6 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
import org.apache.commons.digester.Digester;
|
import org.apache.commons.digester.Digester;
|
||||||
|
|
||||||
import com.samskivert.test.TestUtil;
|
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
|
|
||||||
public class SetFieldRuleTest
|
public class SetFieldRuleTest
|
||||||
@@ -52,8 +51,8 @@ public class SetFieldRuleTest
|
|||||||
digester.addRule("object/stringArrayField", new SetFieldRule("stringArrayField"));
|
digester.addRule("object/stringArrayField", new SetFieldRule("stringArrayField"));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String xmlpath = TestUtil.getResourcePath("xml/setfieldtest.xml");
|
InputStream input = getClass().getClassLoader().getResourceAsStream(
|
||||||
InputStream input = new FileInputStream(xmlpath);
|
"xml/setfieldtest.xml");
|
||||||
digester.parse(input);
|
digester.parse(input);
|
||||||
input.close();
|
input.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user