* added JUnit

* added some unit tests
This commit is contained in:
Reinhard Pointner 2008-06-09 18:41:06 +00:00
parent 21b5dc3fab
commit 405f04b9dd
7 changed files with 540 additions and 41 deletions

122
build.xml
View File

@ -3,30 +3,28 @@
<project name="FileBot" basedir="." default="fatjar">
<property name="title" value="${ant.project.name}" />
<property name="version" value="2.0" />
<property name="version" value="1.9" />
<property name="source" location="${basedir}/source" />
<property name="build" location="${basedir}/build" />
<property name="lib" location="${basedir}/lib" />
<property name="executable" location="${build}/${title}.jar" />
<property name="dir.source" location="${basedir}/source" />
<property name="dir.test" location="${basedir}/test" />
<property name="dir.build" location="${basedir}/build" />
<property name="dir.lib" location="${basedir}/lib" />
<property name="lib.xerces" value="xercesImpl.jar" />
<property name="lib.nekohtml" value="nekohtml-1.9.7.jar" />
<property name="lib.simmetrics" value="simmetrics_jar_v1_6_2_d07_02_07.jar" />
<property name="lib.xmlrpc" value="xmlrpc-client-1.1.jar" />
<property name="executable" location="${dir.build}/${title}.jar" />
<patternset id="classes">
<include name="**/*.class" />
<include name="**/*.properties" />
<exclude name="**/*Test*" />
</patternset>
<property name="lib.xerces" value="${dir.lib}/xercesImpl.jar" />
<property name="lib.nekohtml" value="${dir.lib}/nekohtml-1.9.7.jar" />
<property name="lib.simmetrics" value="${dir.lib}/simmetrics_jar_v1_6_2_d07_02_07.jar" />
<property name="lib.xmlrpc" value="${dir.lib}/xmlrpc-client-1.1.jar" />
<property name="lib.glazedlists" value="${dir.lib}/glazedlists-1.7.0_java15.jar" />
<property name="lib.junit" value="${dir.lib}/junit-4.4.jar" />
<target name="fatjar" depends="build">
<target name="fatjar" depends="clean, build">
<jar destfile="${executable}" duplicate="fail">
<fileset dir="${build}" />
<fileset dir="${dir.build}" />
<manifest>
<attribute name="Built-By" value="${user.name}" />
@ -34,57 +32,99 @@
<attribute name="Version" value="${version}" />
</manifest>
<!-- include libs if fatjar is set -->
<zipfileset src="${lib}/${lib.xerces}">
<patternset refid="classes" />
<!-- include libs -->
<zipfileset src="${lib.xerces}">
<include name="org/apache/html/dom/**" />
<include name="org/apache/xerces/dom/**" />
<include name="org/apache/xerces/impl/**" />
<include name="org/apache/xerces/parsers/**" />
<include name="org/apache/xerces/util/**" />
<include name="org/apache/xerces/xni/**" />
<include name="org/apache/xerces/xs/**" />
</zipfileset>
<zipfileset src="${lib}/${lib.nekohtml}">
<patternset refid="classes" />
</zipfileset>
<zipfileset src="${lib}/${lib.simmetrics}">
<patternset refid="classes" />
<zipfileset src="${lib.nekohtml}">
<include name="**/*.class" />
<include name="**/*.properties" />
</zipfileset>
<zipfileset src="${lib}/${lib.xmlrpc}">
<patternset refid="classes" />
<zipfileset src="${lib.simmetrics}">
<include name="uk/ac/shef/wit/simmetrics/math/**" />
<include name="uk/ac/shef/wit/simmetrics/similaritymetrics/**" />
<include name="uk/ac/shef/wit/simmetrics/tokenisers/**" />
<include name="uk/ac/shef/wit/simmetrics/wordhandlers/**" />
</zipfileset>
<zipfileset src="${lib.xmlrpc}">
<include name="**/*.class" />
</zipfileset>
<!--
<zipfileset src="${lib.glazedlists}">
<include name="ca/odell/glazedlists/*.class" />
<include name="ca/odell/glazedlists/event/**" />
<include name="ca/odell/glazedlists/matchers/**" />
<include name="ca/odell/glazedlists/gui/**" />
<include name="ca/odell/glazedlists/swing/**" />
</zipfileset>
-->
</jar>
</target>
<target name="build" depends="clean">
<target name="build">
<!-- create build dir -->
<mkdir dir="${build}" />
<mkdir dir="${dir.build}" />
<!-- compile -->
<javac srcdir="${source}" destdir="${build}">
<javac srcdir="${dir.source}" destdir="${dir.build}">
<classpath>
<fileset dir="${lib}" includes="*.jar" />
<fileset dir="${dir.lib}" includes="*.jar" />
</classpath>
</javac>
<!-- copy resources -->
<copy todir="${build}">
<fileset dir="${source}">
<copy todir="${dir.build}">
<fileset dir="${dir.source}">
<include name="**/*.png" />
<include name="**/*.gif" />
<include name="**/*.xml" />
</fileset>
</copy>
</target>
<target name="build-test" depends="build">
<javac srcdir="${dir.test}" destdir="${dir.build}">
<classpath>
<fileset dir="${dir.lib}" includes="*.jar" />
</classpath>
</javac>
</target>
<target name="clean">
<delete dir="${build}" />
<delete file="${executable}" verbose="true" />
<delete file="${executable}" verbose="yes" />
<delete dir="${dir.build}" />
</target>
<target name="test" depends="clean, build-test, fatjar">
<junit printsummary="yes" fork="yes">
<classpath>
<pathelement location="${executable}"/>
<pathelement location="${lib.junit}"/>
</classpath>
<formatter type="plain" />
<test name="AllTests" outfile="test-report" />
</junit>
</target>
<target name="run" depends="fatjar">
<java jar="${executable}" fork="true" />
<java jar="${executable}" fork="yes" />
</target>
</project>

BIN
lib/junit-4.4.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,120 @@
package net.sourceforge.tuned;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import net.sourceforge.tuned.FunctionIterator.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FunctionIteratorTest {
@Parameters
public static Collection<Object[]> createParameters() {
List<String> data = new ArrayList<String>();
data.add("http://filebot.sourceforge.net");
data.add("http://javapuzzlers.com");
data.add("http://www.google.com");
data.add("Vanessa Mae - Classical Gas"); // invalid URI
return TestUtil.asParameters(TestUtil.rotations(data).toArray());
}
private final List<String> data;
public FunctionIteratorTest(List<String> data) {
this.data = data;
}
@Test
public void skipNull() {
Iterator<String> iterator = new FunctionIterator<String, String>(data, new FilterFunction("filebot"));
String result = iterator.next();
assertEquals("http://filebot.sourceforge.net", result);
}
@Test(expected = NoSuchElementException.class)
public void noMoreNext() {
Iterator<URI> iterator = new FunctionIterator<String, URI>(new ArrayList<String>(), new UriFunction());
iterator.next();
}
@Test(expected = IllegalArgumentException.class)
public void throwException() {
Iterator<URI> iterator = new FunctionIterator<String, URI>(data, new UriFunction());
while (iterator.hasNext()) {
iterator.next();
}
}
@Test
public void iterate() {
Iterator<URI> iterator = new FunctionIterator<String, URI>(data, new UriFunction());
List<URI> values = new ArrayList<URI>();
while (iterator.hasNext()) {
try {
values.add(iterator.next());
} catch (Exception e) {
}
}
assertTrue(values.size() == 3);
}
private static class UriFunction implements Function<String, URI> {
@Override
public URI evaluate(String sourceValue) {
return URI.create(sourceValue);
}
}
private static class FilterFunction implements Function<String, String> {
private final String filter;
public FilterFunction(String filter) {
this.filter = filter;
}
@Override
public String evaluate(String sourceValue) {
if (sourceValue.contains(filter))
return sourceValue;
return null;
}
}
}

View File

@ -0,0 +1,102 @@
package net.sourceforge.tuned;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.prefs.Preferences;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class PreferencesListTest {
private static Preferences root;
private static Preferences strings;
private static Preferences numbers;
private static Preferences temp;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
root = Preferences.userRoot().node("filebot-test/PreferencesList");
strings = root.node("strings");
strings.put("0", "Rei");
strings.put("1", "Firefly");
strings.put("2", "Roswell");
strings.put("3", "Angel");
strings.put("4", "Dead like me");
strings.put("5", "Babylon");
numbers = root.node("numbers");
numbers.putInt("0", 4);
numbers.putInt("1", 5);
numbers.putInt("2", 2);
temp = root.node("temp");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
root.removeNode();
}
@Test
public void get() {
List<String> list = PreferencesList.map(strings, String.class);
assertEquals("Rei", list.get(0));
assertEquals("Roswell", list.get(2));
assertEquals("Babylon", list.get(5));
}
@Test
public void testAdd() {
List<Integer> list = PreferencesList.map(numbers, Integer.class);
list.add(3);
assertEquals("3", numbers.get("3", null));
}
@Test
public void remove() {
temp.put("0", "Gladiator 1");
temp.put("1", "Gladiator 2");
temp.put("2", "Gladiator 3");
temp.put("3", "Gladiator 4");
List<String> list = PreferencesList.map(temp, String.class);
assertEquals("Gladiator 2", list.remove(1));
assertEquals("Gladiator 4", list.remove(2));
assertEquals("Gladiator 1", list.remove(0));
}
@Test
public void setEntry() {
List<String> list = PreferencesList.map(strings, String.class);
list.set(3, "Buffy");
assertEquals(strings.get("3", null), "Buffy");
}
@Test
public void toArray() throws Exception {
List<String> list = PreferencesList.map(strings, String.class);
assertArrayEquals(list.subList(0, 3).toArray(), new Object[] { "Rei", "Firefly", "Roswell" });
}
}

View File

@ -0,0 +1,181 @@
package net.sourceforge.tuned;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.prefs.Preferences;
import net.sourceforge.filebot.web.MovieDescriptor;
import net.sourceforge.tuned.PreferencesMap.SerializableAdapter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class PreferencesMapTest {
private static Preferences root;
private static Preferences strings;
private static Preferences numbers;
private static Preferences temp;
private static Preferences sequence;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
root = Preferences.userRoot().node("filebot-test/PreferencesMap");
strings = root.node("strings");
strings.put("1", "Firefly");
strings.put("2", "Roswell");
strings.put("3", "Angel");
strings.put("4", "Dead like me");
strings.put("5", "Babylon");
numbers = root.node("numbers");
numbers.putInt("M", 4);
numbers.putInt("A", 5);
numbers.putInt("X", 2);
sequence = root.node("sequence");
sequence.putInt("1", 1);
sequence.putInt("2", 2);
sequence.putInt("3", 3);
temp = root.node("temp");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
root.removeNode();
}
@Test
public void get() {
Map<String, String> stringMap = PreferencesMap.map(strings, String.class);
assertEquals("Firefly", stringMap.get("1"));
}
@Test
public void put() {
Map<String, String> stringMap = PreferencesMap.map(temp, String.class);
stringMap.put("key", "snake");
assertEquals("snake", temp.get("key", null));
}
@Test
public void remove() throws Exception {
Map<String, Integer> map = PreferencesMap.map(numbers, Integer.class);
map.remove("A");
assertFalse(Arrays.asList(numbers.keys()).contains("A"));
}
@Test
public void clear() throws Exception {
Map<String, Integer> map = PreferencesMap.map(temp, Integer.class);
map.put("X", 42);
map.clear();
assertTrue(temp.keys().length == 0);
}
@Test
public void containsKey() {
temp.put("name", "kaya");
Map<String, String> map = PreferencesMap.map(temp, String.class);
assertTrue(map.containsKey("name"));
}
@Test
public void values() {
Map<String, Integer> map = PreferencesMap.map(sequence, Integer.class);
Collection<Integer> list = map.values();
assertTrue(list.contains(1));
assertTrue(list.contains(2));
assertTrue(list.contains(3));
}
@Test
public void containsValue() {
Map<String, String> map = PreferencesMap.map(strings, String.class);
assertTrue(map.containsValue("Firefly"));
}
@Test
public void entrySet() {
Map<String, Integer> map = PreferencesMap.map(numbers, Integer.class);
for (Entry<String, Integer> entry : map.entrySet()) {
Integer v = entry.getValue();
entry.setValue(v + 1);
}
assertEquals(5, numbers.getInt("M", -1));
}
@Test(expected = NumberFormatException.class)
public void adapterException() {
PreferencesMap.map(strings, Integer.class).values();
}
@Test
public void containsKeyWithObjectKey() throws Exception {
Map<String, String> map = PreferencesMap.map(strings, String.class);
assertFalse(map.containsKey(new Object()));
}
@Test(expected = IllegalArgumentException.class)
public void getWithObjectKey() throws Exception {
Map<String, String> map = PreferencesMap.map(strings, String.class);
map.get(new Object());
}
@Test
public void serializableAdapter() {
Map<String, MovieDescriptor> map = PreferencesMap.map(temp, new SerializableAdapter<MovieDescriptor>());
MovieDescriptor movie = new MovieDescriptor("The Hitchhiker's Guide to the Galaxy", 42);
map.put("movie", movie);
MovieDescriptor retrieved = map.get("movie");
assertEquals(movie.getImdbId(), retrieved.getImdbId());
}
}

View File

@ -0,0 +1,21 @@
package net.sourceforge.tuned;
import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses( { FunctionIteratorTest.class, PreferencesMapTest.class, PreferencesListTest.class })
public class TestSuite {
public static Test suite() {
return new JUnit4TestAdapter(TestSuite.class);
}
}

View File

@ -0,0 +1,35 @@
package net.sourceforge.tuned;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class TestUtil {
public static <T> List<List<T>> rotations(Collection<T> source) {
List<List<T>> rotations = new ArrayList<List<T>>();
for (int i = 0; i < source.size(); i++) {
List<T> copy = new ArrayList<T>(source);
Collections.rotate(copy, i);
rotations.add(copy);
}
return rotations;
}
public static List<Object[]> asParameters(Object... parameterSet) {
List<Object[]> list = new ArrayList<Object[]>();
for (Object parameter : parameterSet) {
list.add(new Object[] { parameter });
}
return list;
}
}