Converted all tests to JUnit 4. Nixed vestigial Velocity-related test class.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2809 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-08-27 00:34:13 +00:00
parent 91639537f0
commit 83e1c95c34
17 changed files with 60 additions and 476 deletions
-1
View File
@@ -7,7 +7,6 @@
<include name="commons-digester.jar"/> <include name="commons-digester.jar"/>
<include name="commons-logging.jar"/> <include name="commons-logging.jar"/>
<include name="ehcache.jar"/> <include name="ehcache.jar"/>
<include name="junit-3.7.jar"/>
<include name="junit4.jar"/> <include name="junit4.jar"/>
<include name="log4j.jar"/> <include name="log4j.jar"/>
<include name="mail.jar"/> <include name="mail.jar"/>
@@ -1,202 +0,0 @@
//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2010 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
package com.samskivert.velocity;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Locale;
import junit.framework.TestCase;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.log.LogChute;
import com.samskivert.servlet.MessageManager;
/**
* An abstract base class for easily testing Velocity templates.
*/
public abstract class VelocityTestCase extends TestCase
{
protected VelocityTestCase (String name)
{
super(name);
}
@Override // from TestCase
protected void setUp ()
{
try {
_engine = new VelocityEngine();
_engine.setProperty(VelocityEngine.VM_LIBRARY, "");
_engine.setProperty(VelocityEngine.RESOURCE_LOADER, "classpath");
_engine.setProperty(
"classpath." + VelocityEngine.RESOURCE_LOADER + ".class",
ClasspathResourceLoader.class.getName());
_engine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, _logger);
_engine.init();
} catch (Exception e) {
fail("Velocity initialization failed " + e);
}
}
@Override // from TestCase
protected void runTest ()
{
// first simply evaluate all of the templates
for (String template : getTemplates()) {
try {
testParse(template);
} catch (Exception e) {
fail("Failed to process " + template + ": " + e);
}
}
// then try to actually merge them with a context
for (String template : getTemplates()) {
try {
testMerge(template);
} catch (Exception e) {
fail("Failed to process " + template + ": " + e);
}
}
}
protected void testParse (String template)
throws Exception
{
InputStream tempin =
getClass().getClassLoader().getResourceAsStream(template);
if (tempin == null) {
fail("Missing template '" + template + "'.");
}
// parse the template
if (!_engine.evaluate(new VelocityContext(), new StringWriter(),
template, new InputStreamReader(tempin))) {
fail("Template parsing failed '" + template + "'.");
}
}
protected void testMerge (String template)
throws Exception
{
// clear out any previous logging output
_logbuf.getBuffer().setLength(0);
// populate the context with useful bits
VelocityContext ctx = new VelocityContext();
populateContext(template, ctx);
// now merge the template
StringWriter writer = new StringWriter();
_engine.mergeTemplate(template, ctx, writer);
// if there was any logging output, the test failed
String logout = _logbuf.toString();
if (logout.length() > 0) {
fail("Template merge failed '" + template + "'.\n" + logout);
}
}
/**
* Called before parsing each template to populate the context as needed
* for the template in question. The default implementation adds some
* standard bits provided by {@link DispatcherServlet}.
*/
protected void populateContext (String template, VelocityContext ctx)
{
ctx.put("context_path", "/test");
// populate the context with various standard tools
MessageManager msgmgr = getMessageManager();
if (msgmgr != null) {
ctx.put(DispatcherServlet.I18NTOOL_KEY, new I18nTool(null, msgmgr) {
@Override protected Locale getLocale () {
return Locale.getDefault();
}
});
}
ctx.put(DispatcherServlet.FORMTOOL_KEY, new FormTool(null) {
@Override protected String getParameter (String name) {
return null;
}
});
ctx.put(DispatcherServlet.STRINGTOOL_KEY, new StringTool());
ctx.put(DispatcherServlet.DATATOOL_KEY, new DataTool());
ctx.put(DispatcherServlet.CURRENCYTOOL_KEY,
new CurrencyTool(Locale.getDefault()));
}
/**
* If the tests require translation, this method must return a message
* manager configured appropriately.
*/
protected MessageManager getMessageManager ()
{
return null;
}
/**
* Returns an array of template paths which will be resolved on the
* classpath.
*/
protected abstract Collection<String> getTemplates ();
/** Accumulates logging to a buffer for later reporting. */
protected LogChute _logger = new LogChute() {
public void init (RuntimeServices rs) throws Exception {
// nothing doing
}
public void log (int level, String message) {
switch (level) {
case ERROR_ID:
case WARN_ID:
_logout.println(message);
break;
default:
case INFO_ID:
case DEBUG_ID:
// velocity insists on sending info and debug messages even
// though isLevelEnabled() returns false
break;
}
}
public void log (int level, String message, Throwable t) {
log(level, message);
t.printStackTrace(_logout);
}
public boolean isLevelEnabled (int level) {
return (level == WARN_ID || level == ERROR_ID);
}
};
protected VelocityEngine _engine;
protected StringWriter _logbuf = new StringWriter();
protected PrintWriter _logout = new PrintWriter(_logbuf);
}
@@ -28,20 +28,15 @@ import java.util.Iterator;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
import com.samskivert.io.StreamUtil; import com.samskivert.io.StreamUtil;
import com.samskivert.test.TestUtil; import com.samskivert.test.TestUtil;
public class SiteResourceLoaderTest extends TestCase public class SiteResourceLoaderTest
{ {
public SiteResourceLoaderTest () @Test
{
super(SiteResourceLoaderTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
// we need to fake a couple of things to get the test to work // we need to fake a couple of things to get the test to work
@@ -108,11 +103,6 @@ public class SiteResourceLoaderTest extends TestCase
buffer.append(StreamUtil.toString(rin, "UTF-8")); buffer.append(StreamUtil.toString(rin, "UTF-8"));
} }
public static Test suite ()
{
return new SiteResourceLoaderTest();
}
public static class TestSiteIdentifier implements SiteIdentifier public static class TestSiteIdentifier implements SiteIdentifier
{ {
public int identifySite (HttpServletRequest req) public int identifySite (HttpServletRequest req)
@@ -24,19 +24,14 @@ import java.awt.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
import com.samskivert.swing.util.ProximityTracker; import com.samskivert.swing.util.ProximityTracker;
public class ProximityTrackerTest extends TestCase public class ProximityTrackerTest
{ {
public ProximityTrackerTest () @Test
{
super(ProximityTrackerTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
Random rand = new Random(); Random rand = new Random();
@@ -89,17 +84,6 @@ public class ProximityTrackerTest extends TestCase
} }
} }
public static Test suite ()
{
return new ProximityTrackerTest();
}
public static void main (String[] args)
{
ProximityTrackerTest test = new ProximityTrackerTest();
test.runTest();
}
protected static final int MAX_X = 1000; protected static final int MAX_X = 1000;
protected static final int MAX_Y = 1000; protected static final int MAX_Y = 1000;
} }
@@ -20,22 +20,17 @@
package com.samskivert.util; package com.samskivert.util;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
import static com.samskivert.Log.log; import static com.samskivert.Log.log;
/** /**
* Tests the {@link ArrayUtil} class. * Tests the {@link ArrayUtil} class.
*/ */
public class ArrayUtilTest extends TestCase public class ArrayUtilTest
{ {
public ArrayUtilTest () @Test
{
super(ArrayUtilTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
// test reversing an array // test reversing an array
@@ -157,15 +152,4 @@ public class ArrayUtilTest extends TestCase
work = ArrayUtil.splice(work, 2, 2); work = ArrayUtil.splice(work, 2, 2);
log.info("splice concat 2, 2: " + StringUtil.toString(work)); log.info("splice concat 2, 2: " + StringUtil.toString(work));
} }
public static Test suite ()
{
return new ArrayUtilTest();
}
public static void main (String[] args)
{
ArrayUtilTest test = new ArrayUtilTest();
test.runTest();
}
} }
@@ -20,20 +20,15 @@
package com.samskivert.util; package com.samskivert.util;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Tests the {@link CheapIntMap} class. * Tests the {@link CheapIntMap} class.
*/ */
public class CheapIntMapTest extends TestCase public class CheapIntMapTest
{ {
public CheapIntMapTest () @Test
{
super(CheapIntMapTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
CheapIntMap map = new CheapIntMap(10); CheapIntMap map = new CheapIntMap(10);
@@ -68,15 +63,4 @@ public class CheapIntMapTest extends TestCase
} }
} }
} }
public static Test suite ()
{
return new CheapIntMapTest();
}
public static void main (String[] args)
{
CheapIntMapTest test = new CheapIntMapTest();
test.runTest();
}
} }
@@ -23,20 +23,15 @@ package com.samskivert.util;
import java.util.Iterator; import java.util.Iterator;
import java.util.Properties; import java.util.Properties;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Tests the {@link Config} class. * Tests the {@link Config} class.
*/ */
public class ConfigTest extends TestCase public class ConfigTest
{ {
public ConfigTest () @Test
{
super(ConfigTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
PrefsConfig config = new PrefsConfig("rsrc/util/test"); PrefsConfig config = new PrefsConfig("rsrc/util/test");
@@ -68,15 +63,4 @@ public class ConfigTest extends TestCase
Properties subprops = config.getSubProperties("sub"); Properties subprops = config.getSubProperties("sub");
System.out.println("Sub: " + StringUtil.toString(subprops.propertyNames())); System.out.println("Sub: " + StringUtil.toString(subprops.propertyNames()));
} }
public static Test suite ()
{
return new ConfigTest();
}
public static void main (String[] args)
{
ConfigTest test = new ConfigTest();
test.runTest();
}
} }
@@ -22,8 +22,8 @@ package com.samskivert.util;
import java.util.Properties; import java.util.Properties;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Our test properties files: * Our test properties files:
@@ -64,14 +64,9 @@ import junit.framework.TestCase;
* three = test - three * three = test - three
* </pre> * </pre>
*/ */
public class ConfigUtilTest extends TestCase public class ConfigUtilTest
{ {
public ConfigUtilTest () @Test
{
super(ConfigUtilTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
try { try {
@@ -88,17 +83,6 @@ public class ConfigUtilTest extends TestCase
} }
} }
public static Test suite ()
{
return new ConfigUtilTest();
}
public static void main (String[] args)
{
ConfigUtilTest test = new ConfigUtilTest();
test.runTest();
}
protected static final String DUMP = protected static final String DUMP =
"{prop4=one, two, three,, and a half, four, " + "{prop4=one, two, three,, and a half, four, " +
"prop3=9, 8, 7, 6, prop2=twenty five, prop1=25, " + "prop3=9, 8, 7, 6, prop2=twenty five, prop1=25, " +
@@ -24,17 +24,12 @@ import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
public class HashIntMapTest extends TestCase public class HashIntMapTest
{ {
public HashIntMapTest () @Test
{
super(HashIntMapTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
HashIntMap<Integer> table = new HashIntMap<Integer>(); HashIntMap<Integer> table = new HashIntMap<Integer>();
@@ -134,11 +129,6 @@ public class HashIntMapTest extends TestCase
assertTrue(valuestr + ".equals(" + exvals + ")", valuestr.equals(exvals)); assertTrue(valuestr + ".equals(" + exvals + ")", valuestr.equals(exvals));
} }
public static Test suite ()
{
return new HashIntMapTest();
}
protected static final String TEST1 = "(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)"; protected static final String TEST1 = "(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)";
protected static final String TEST2 = "(10, 11)"; protected static final String TEST2 = "(10, 11)";
} }
@@ -22,17 +22,12 @@ package com.samskivert.util;
import java.util.Arrays; import java.util.Arrays;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
public class IntListUtilTest extends TestCase public class IntListUtilTest
{ {
public IntListUtilTest () @Test
{
super(IntListUtilTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
int[] list = null; int[] list = null;
@@ -110,9 +105,4 @@ public class IntListUtilTest extends TestCase
assertTrue("removeAt(0)", assertTrue("removeAt(0)",
Arrays.equals(list, new int[] { 5, 6, 7, 0 })); Arrays.equals(list, new int[] { 5, 6, 7, 0 }));
} }
public static Test suite ()
{
return new IntListUtilTest();
}
} }
@@ -20,20 +20,15 @@
package com.samskivert.util; package com.samskivert.util;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Tests the {@link LRUHashMap} class. * Tests the {@link LRUHashMap} class.
*/ */
public class LRUHashMapTest extends TestCase public class LRUHashMapTest
{ {
public LRUHashMapTest () @Test
{
super(LRUHashMapTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
LRUHashMap<String,Integer> map = LRUHashMap<String,Integer> map =
@@ -62,15 +57,4 @@ public class LRUHashMapTest extends TestCase
map.put("three.3", 3); map.put("three.3", 3);
assertTrue("size == 2", map.size() == 2); assertTrue("size == 2", map.size() == 2);
} }
public static Test suite ()
{
return new LRUHashMapTest();
}
public static void main (String[] args)
{
LRUHashMapTest test = new LRUHashMapTest();
test.runTest();
}
} }
@@ -20,20 +20,15 @@
package com.samskivert.util; package com.samskivert.util;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Tests the {@link ObserverList} class. * Tests the {@link ObserverList} class.
*/ */
public class ObserverListTest extends TestCase public class ObserverListTest
{ {
public ObserverListTest () @Test
{
super(ObserverListTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
// Log.info("Testing safe list."); // Log.info("Testing safe list.");
@@ -86,17 +81,6 @@ public class ObserverListTest extends TestCase
} }
} }
public static Test suite ()
{
return new ObserverListTest();
}
public static void main (String[] args)
{
ObserverListTest test = new ObserverListTest();
test.runTest();
}
protected static class TestObserver protected static class TestObserver
{ {
public TestObserver (int index) public TestObserver (int index)
@@ -22,20 +22,15 @@ package com.samskivert.util;
import java.util.Comparator; import java.util.Comparator;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Tests the {@link QuickSort} class. * Tests the {@link QuickSort} class.
*/ */
public class QuickSortTest extends TestCase public class QuickSortTest
{ {
public QuickSortTest () @Test
{
super(QuickSortTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
Integer[] a = new Integer[100]; Integer[] a = new Integer[100];
@@ -102,17 +97,6 @@ public class QuickSortTest extends TestCase
// " random arrays"); // " random arrays");
} }
public static Test suite ()
{
return new QuickSortTest();
}
public static void main (String[] args)
{
QuickSortTest test = new QuickSortTest();
test.runTest();
}
private static int rand (int n) private static int rand (int n)
{ {
return (int)(Math.random() * n); return (int)(Math.random() * n);
@@ -22,22 +22,21 @@ package com.samskivert.util;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* Tests the {@link SerialExecutor} class. * Tests the {@link SerialExecutor} class.
*/ */
public class SerialExecutorTest extends TestCase public class SerialExecutorTest
implements Executor, RunQueue implements Executor, RunQueue
{ {
public SerialExecutorTest () public SerialExecutorTest ()
{ {
super(SerialExecutorTest.class.getName());
_main = Thread.currentThread(); _main = Thread.currentThread();
} }
@Override @Test
public void runTest () public void runTest ()
{ {
SerialExecutor executor = new SerialExecutor(this); SerialExecutor executor = new SerialExecutor(this);
@@ -110,17 +109,6 @@ public class SerialExecutorTest extends TestCase
return true; return true;
} }
public static Test suite ()
{
return new SerialExecutorTest();
}
public static void main (String[] args)
{
SerialExecutorTest test = new SerialExecutorTest();
test.runTest();
}
protected class Sleeper implements SerialExecutor.ExecutorTask protected class Sleeper implements SerialExecutor.ExecutorTask
{ {
public Sleeper (long sleepFor, boolean hang) { public Sleeper (long sleepFor, boolean hang) {
@@ -20,17 +20,12 @@
package com.samskivert.util; package com.samskivert.util;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
public class StringUtilTest extends TestCase public class StringUtilTest
{ {
public StringUtilTest () @Test
{
super(StringUtilTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
String source = "mary, had, a,, little, lamb, and, a, comma,,"; String source = "mary, had, a,, little, lamb, and, a, comma,,";
@@ -48,9 +43,4 @@ public class StringUtilTest extends TestCase
joined = StringUtil.joinEscaped(tokens); joined = StringUtil.joinEscaped(tokens);
assertTrue("null elements work", joined.equals("this, , is, , a, , test")); assertTrue("null elements work", joined.equals("this, , is, , a, , test"));
} }
public static Test suite ()
{
return new StringUtilTest();
}
} }
@@ -20,13 +20,13 @@
package com.samskivert.util; package com.samskivert.util;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; import static org.junit.Assert.*;
/** /**
* A test case for {@link Throttle}. * A test case for {@link Throttle}.
*/ */
public class ThrottleTest extends TestCase public class ThrottleTest
{ {
public static class TestThrottle extends Throttle public static class TestThrottle extends Throttle
{ {
@@ -46,23 +46,7 @@ public class ThrottleTest extends TestCase
} }
} }
public static Test suite () @Test
{
return new ThrottleTest();
}
public static void main (String[] args)
{
ThrottleTest test = new ThrottleTest();
test.runTest();
}
public ThrottleTest ()
{
super(ThrottleTest.class.getName());
}
@Override
public void runTest () public void runTest ()
{ {
testUpdate(4); testUpdate(4);
@@ -23,21 +23,16 @@ package com.samskivert.xml;
import java.io.InputStream; import java.io.InputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import junit.framework.Test; import org.junit.*;
import junit.framework.TestCase; 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.test.TestUtil;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
public class SetFieldRuleTest extends TestCase public class SetFieldRuleTest
{ {
public SetFieldRuleTest ()
{
super(SetFieldRuleTest.class.getName());
}
public static class TestObject public static class TestObject
{ {
public int intField; public int intField;
@@ -56,8 +51,7 @@ public class SetFieldRuleTest extends TestCase
} }
} }
@Override @Test public void runTest ()
public void runTest ()
{ {
Digester digester = new Digester(); Digester digester = new Digester();
@@ -85,17 +79,6 @@ public class SetFieldRuleTest extends TestCase
assertTrue(EXPECTED.equals(object.toString())); assertTrue(EXPECTED.equals(object.toString()));
} }
public static Test suite ()
{
return new SetFieldRuleTest();
}
public static void main (String[] args)
{
SetFieldRuleTest test = new SetFieldRuleTest();
test.runTest();
}
protected static final String EXPECTED = protected static final String EXPECTED =
"[intField=5, stringField=howdy partner!, integerField=15, " + "[intField=5, stringField=howdy partner!, integerField=15, " +
"intArrayField=(1, 2, 3, 4, 5), " + "intArrayField=(1, 2, 3, 4, 5), " +