package modTransf.util;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Collection;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.LinkedList;
/**
* Test case for UnmodifiableFilteredCollection.
* Tests:
* <ul>
* <li></li>
* <li></li>
* <li></li>
* <li></li>
* <li></li>
* </ul>
*/
public class UnmodifiableFilteredListTest
extends TestCase
{
protected Object endsView[] = {"local0", new Object(), "local1", new Object(), "local2", };
protected Object middleView[] = {new Object(), "local0", new Object(), "local1", "local2", new Object(), };
protected Object fullView[] = {"local0", "local1", "local2", };
protected Object emptyView[] = {new Object(), new Object(), new Object(), };
protected Object emptyListView[] = {};
protected List expectedView;
public UnmodifiableFilteredListTest(String name)
{
super(name);
}
/**
* Initialize the context for a new test.
* This method is called befor each test
* @throws IOException
*/
public void setUp() throws IOException
{
expectedView = Arrays.asList(fullView);
}
protected List createFilterColl( Object values[] )
{
List backup = Arrays.asList(values);
List coll = new UnmodifiableFilteredList( backup, new TypeFilter() );
return coll;
}
/**
* Tear down the test.
* This method is called after each test.
*/
public void tearDown()
{
}
/**
* Nested class Filter.
*/
private class TypeFilter implements Filter {
public boolean isAllowed( Object object )
{
return (object instanceof String);
}
}
/**
* Compare two collection.
* Return true if the content is the same and in the same order
* @param col1 Collection
* @param col2 Collection
* @return boolean
*/
protected boolean isSameContentOrder( Collection col1, Collection col2 )
{
Iterator i1 = col1.iterator();
Iterator i2 = col2.iterator();
while(i1.hasNext() && i2.hasNext()) {
Object o1 = i1.next();
Object o2 = i2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(i1.hasNext() || i2.hasNext());
}
/**
*/
public void testMiddleView()
{
Collection col = createFilterColl(middleView);
assertEquals( "view list size", 3, col.size() );
// Following doesn't work because two collections are not equals, even if they
// Have the same elements.
//assertEquals("comparison", expectedView, col);
assertTrue( "Content order", isSameContentOrder(col, expectedView) );
}
/**
*/
public void testEndsView()
{
Collection col = createFilterColl(endsView);
assertEquals( "view list size", 3, col.size() );
// Following doesn't work because two collections are not equals, even if they
// Have the same elements.
//assertEquals("comparison", expectedView, col);
assertTrue( "Content order", isSameContentOrder(col, expectedView) );
}
/**
*/
public void testEmptyView()
{
Collection col = createFilterColl(emptyView);
assertEquals( "view list size", 0, col.size() );
// Following doesn't work because two collections are not equals, even if they
// Have the same elements.
//assertEquals("comparison", expectedView, col);
assertFalse( "No next", col.iterator().hasNext() );
}
/**
*/
public void testEmptyListView()
{
Collection col = createFilterColl(emptyListView);
assertEquals( "view list size", 0, col.size() );
// Following doesn't work because two collections are not equals, even if they
// Have the same elements.
//assertEquals("comparison", expectedView, col);
assertFalse( "No next", col.iterator().hasNext() );
}
/**
*/
public void testFullView()
{
Collection col = createFilterColl(endsView);
assertEquals( "view list size", 3, col.size() );
// Following doesn't work because two collections are not equals, even if they
// Have the same elements.
//assertEquals("comparison", expectedView, col);
assertTrue( "Content order", isSameContentOrder(col, expectedView) );
}
/**
* Check normal behavior.
*/
public void testListIterator()
{
nextAndPreviousTest( expectedView);
}
/**
* Try to do next untill the ned of list, and then and previous untill
* the start. Check the various methods, and compare the result of next()
* or previous().
* @param list List List to compare to fullView
*/
public void nextAndPreviousTest( List list )
{
ListIterator listIter = list.listIterator();
assertTrue( "hasNext", listIter.hasNext() );
assertEquals( "next index", 0, listIter.nextIndex() );
assertEquals( "next", fullView[0], listIter.next() );
assertTrue( "hasNext", listIter.hasNext() );
assertEquals( "next index", 1, listIter.nextIndex() );
assertEquals( "next", fullView[1], listIter.next() );
assertTrue( "hasNext", listIter.hasNext() );
assertEquals( "next index", 2, listIter.nextIndex() );
assertEquals( "next", fullView[2], listIter.next() );
assertFalse( "hasNext", listIter.hasNext() );
// We are at the end of the list. Go back
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 2, listIter.previousIndex() );
assertEquals( "previous", fullView[2], listIter.previous() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 1, listIter.previousIndex() );
assertEquals( "previous", fullView[1], listIter.previous() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 0, listIter.previousIndex() );
assertEquals( "previous", fullView[0], listIter.previous() );
assertFalse( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", -1, listIter.previousIndex() );
}
/**
*/
public void testNextAndPrevious()
{
List col = createFilterColl(middleView);
assertEquals( "view list size", 3, col.size() );
ListIterator listIter = col.listIterator();
assertTrue( "hasNext", listIter.hasNext() );
assertEquals( "next index", 0, listIter.nextIndex() );
assertEquals( "next", fullView[0], listIter.next() );
assertTrue( "hasNext", listIter.hasNext() );
assertEquals( "next index", 1, listIter.nextIndex() );
assertEquals( "next", fullView[1], listIter.next() );
assertTrue( "hasNext", listIter.hasNext() );
assertEquals( "next index", 2, listIter.nextIndex() );
assertEquals( "next", fullView[2], listIter.next() );
assertFalse( "hasNext", listIter.hasNext() );
// We are at the end of the list. Go back
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 2, listIter.previousIndex() );
assertEquals( "previous", fullView[2], listIter.previous() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 1, listIter.previousIndex() );
assertEquals( "previous", fullView[1], listIter.previous() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 0, listIter.previousIndex() );
assertEquals( "previous", fullView[0], listIter.previous() );
assertFalse( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", -1, listIter.previousIndex() );
}
/**
*/
public void testIndexPositioning()
{
List col = createFilterColl(endsView);
//List col = expectedView;
assertEquals( "view list size", 3, col.size() );
ListIterator listIter = col.listIterator(1);
assertEquals( "next index ", 1, listIter.nextIndex() );
assertEquals( "previous index", 0, listIter.previousIndex() );
assertTrue( "hasNext", listIter.hasNext() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "next", fullView[1], listIter.next() );
assertEquals( "next", fullView[2], listIter.next() );
assertFalse( "hasNext", listIter.hasNext() );
assertEquals( "next index ", col.size(), listIter.nextIndex() );
// We are at the end of the list. Go back
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 2, listIter.previousIndex() );
assertEquals( "previous", fullView[2], listIter.previous() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 1, listIter.previousIndex() );
assertEquals( "previous", fullView[1], listIter.previous() );
assertTrue( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", 0, listIter.previousIndex() );
assertEquals( "previous", fullView[0], listIter.previous() );
assertFalse( "hasPrevious", listIter.hasPrevious() );
assertEquals( "previous index", -1, listIter.previousIndex() );
}
}