diff --git a/build.xml b/build.xml index 3a76ec66..e93bf33f 100644 --- a/build.xml +++ b/build.xml @@ -3,30 +3,28 @@ - + - - - - + + + + - - - - - + - - - - - + + + + + + - - + + + - + @@ -34,57 +32,99 @@ - - - - + + + + + + + + + + - - - - - - + + + - - + + + + + + + + + + + - + - + - + - + - - + + - + + + + + + + + + - - + + + - + + + + + + + + + + + + + + - + diff --git a/lib/junit-4.4.jar b/lib/junit-4.4.jar new file mode 100644 index 00000000..649b0b32 Binary files /dev/null and b/lib/junit-4.4.jar differ diff --git a/test/net/sourceforge/tuned/FunctionIteratorTest.java b/test/net/sourceforge/tuned/FunctionIteratorTest.java new file mode 100644 index 00000000..45c5318a --- /dev/null +++ b/test/net/sourceforge/tuned/FunctionIteratorTest.java @@ -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 createParameters() { + List data = new ArrayList(); + + 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 data; + + + public FunctionIteratorTest(List data) { + this.data = data; + } + + + @Test + public void skipNull() { + Iterator iterator = new FunctionIterator(data, new FilterFunction("filebot")); + + String result = iterator.next(); + + assertEquals("http://filebot.sourceforge.net", result); + } + + + @Test(expected = NoSuchElementException.class) + public void noMoreNext() { + Iterator iterator = new FunctionIterator(new ArrayList(), new UriFunction()); + + iterator.next(); + } + + + @Test(expected = IllegalArgumentException.class) + public void throwException() { + Iterator iterator = new FunctionIterator(data, new UriFunction()); + + while (iterator.hasNext()) { + iterator.next(); + } + } + + + @Test + public void iterate() { + Iterator iterator = new FunctionIterator(data, new UriFunction()); + + List values = new ArrayList(); + + while (iterator.hasNext()) { + try { + values.add(iterator.next()); + } catch (Exception e) { + + } + } + + assertTrue(values.size() == 3); + } + + + private static class UriFunction implements Function { + + @Override + public URI evaluate(String sourceValue) { + return URI.create(sourceValue); + } + } + + + private static class FilterFunction implements Function { + + 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; + } + } + +} diff --git a/test/net/sourceforge/tuned/PreferencesListTest.java b/test/net/sourceforge/tuned/PreferencesListTest.java new file mode 100644 index 00000000..8661fc50 --- /dev/null +++ b/test/net/sourceforge/tuned/PreferencesListTest.java @@ -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 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 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 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 list = PreferencesList.map(strings, String.class); + + list.set(3, "Buffy"); + + assertEquals(strings.get("3", null), "Buffy"); + } + + + @Test + public void toArray() throws Exception { + List list = PreferencesList.map(strings, String.class); + + assertArrayEquals(list.subList(0, 3).toArray(), new Object[] { "Rei", "Firefly", "Roswell" }); + } +} diff --git a/test/net/sourceforge/tuned/PreferencesMapTest.java b/test/net/sourceforge/tuned/PreferencesMapTest.java new file mode 100644 index 00000000..8ce3d330 --- /dev/null +++ b/test/net/sourceforge/tuned/PreferencesMapTest.java @@ -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 stringMap = PreferencesMap.map(strings, String.class); + + assertEquals("Firefly", stringMap.get("1")); + } + + + @Test + public void put() { + Map stringMap = PreferencesMap.map(temp, String.class); + + stringMap.put("key", "snake"); + + assertEquals("snake", temp.get("key", null)); + } + + + @Test + public void remove() throws Exception { + Map map = PreferencesMap.map(numbers, Integer.class); + + map.remove("A"); + + assertFalse(Arrays.asList(numbers.keys()).contains("A")); + } + + + @Test + public void clear() throws Exception { + Map 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 map = PreferencesMap.map(temp, String.class); + + assertTrue(map.containsKey("name")); + } + + + @Test + public void values() { + + Map map = PreferencesMap.map(sequence, Integer.class); + + Collection list = map.values(); + + assertTrue(list.contains(1)); + assertTrue(list.contains(2)); + assertTrue(list.contains(3)); + } + + + @Test + public void containsValue() { + Map map = PreferencesMap.map(strings, String.class); + + assertTrue(map.containsValue("Firefly")); + } + + + @Test + public void entrySet() { + Map map = PreferencesMap.map(numbers, Integer.class); + + for (Entry 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 map = PreferencesMap.map(strings, String.class); + + assertFalse(map.containsKey(new Object())); + } + + + @Test(expected = IllegalArgumentException.class) + public void getWithObjectKey() throws Exception { + Map map = PreferencesMap.map(strings, String.class); + + map.get(new Object()); + } + + + @Test + public void serializableAdapter() { + Map map = PreferencesMap.map(temp, new SerializableAdapter()); + + 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()); + } +} diff --git a/test/net/sourceforge/tuned/TestSuite.java b/test/net/sourceforge/tuned/TestSuite.java new file mode 100644 index 00000000..eb950fe4 --- /dev/null +++ b/test/net/sourceforge/tuned/TestSuite.java @@ -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); + } + +} diff --git a/test/net/sourceforge/tuned/TestUtil.java b/test/net/sourceforge/tuned/TestUtil.java new file mode 100644 index 00000000..089faed3 --- /dev/null +++ b/test/net/sourceforge/tuned/TestUtil.java @@ -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 List> rotations(Collection source) { + List> rotations = new ArrayList>(); + + for (int i = 0; i < source.size(); i++) { + List copy = new ArrayList(source); + Collections.rotate(copy, i); + rotations.add(copy); + } + + return rotations; + } + + + public static List asParameters(Object... parameterSet) { + List list = new ArrayList(); + + for (Object parameter : parameterSet) { + list.add(new Object[] { parameter }); + } + + return list; + } +}