From 258e27dac4529f44e57d27234656052d8876c318 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 17 Nov 2014 14:23:41 -0800 Subject: [PATCH] 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. --- src/main/java/com/samskivert/test/Log.java | 14 -- .../java/com/samskivert/test/TestUtil.java | 69 --------- .../java/com/samskivert/test/package.html | 30 ---- .../servlet/SiteResourceLoaderTest.java | 131 ------------------ .../com/samskivert/xml/SetFieldRuleTest.java | 5 +- 5 files changed, 2 insertions(+), 247 deletions(-) delete mode 100644 src/main/java/com/samskivert/test/Log.java delete mode 100644 src/main/java/com/samskivert/test/TestUtil.java delete mode 100644 src/main/java/com/samskivert/test/package.html delete mode 100644 src/test/java/com/samskivert/servlet/SiteResourceLoaderTest.java diff --git a/src/main/java/com/samskivert/test/Log.java b/src/main/java/com/samskivert/test/Log.java deleted file mode 100644 index 29afc0ad..00000000 --- a/src/main/java/com/samskivert/test/Log.java +++ /dev/null @@ -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"); -} diff --git a/src/main/java/com/samskivert/test/TestUtil.java b/src/main/java/com/samskivert/test/TestUtil.java deleted file mode 100644 index 58b35092..00000000 --- a/src/main/java/com/samskivert/test/TestUtil.java +++ /dev/null @@ -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 - * 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, 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)); - } -} diff --git a/src/main/java/com/samskivert/test/package.html b/src/main/java/com/samskivert/test/package.html deleted file mode 100644 index aecce25b..00000000 --- a/src/main/java/com/samskivert/test/package.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - Utilities useful when writing unit tests. - - - diff --git a/src/test/java/com/samskivert/servlet/SiteResourceLoaderTest.java b/src/test/java/com/samskivert/servlet/SiteResourceLoaderTest.java deleted file mode 100644 index bbfd6fe9..00000000 --- a/src/test/java/com/samskivert/servlet/SiteResourceLoaderTest.java +++ /dev/null @@ -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 enumerateSites () - { - return null; // not used - } - } - - protected static final int SITE1_ID = 1; - protected static final int SITE2_ID = 2; -} diff --git a/src/test/java/com/samskivert/xml/SetFieldRuleTest.java b/src/test/java/com/samskivert/xml/SetFieldRuleTest.java index ccd3e859..68c9eaf9 100644 --- a/src/test/java/com/samskivert/xml/SetFieldRuleTest.java +++ b/src/test/java/com/samskivert/xml/SetFieldRuleTest.java @@ -13,7 +13,6 @@ import static org.junit.Assert.*; import org.apache.commons.digester.Digester; -import com.samskivert.test.TestUtil; import com.samskivert.util.StringUtil; public class SetFieldRuleTest @@ -52,8 +51,8 @@ public class SetFieldRuleTest digester.addRule("object/stringArrayField", new SetFieldRule("stringArrayField")); try { - String xmlpath = TestUtil.getResourcePath("xml/setfieldtest.xml"); - InputStream input = new FileInputStream(xmlpath); + InputStream input = getClass().getClassLoader().getResourceAsStream( + "xml/setfieldtest.xml"); digester.parse(input); input.close(); } catch (Exception e) {