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; } } }