View Javadoc

    1   package net.sourceforge.pmd.util;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertFalse;
    5   import static org.junit.Assert.assertTrue;
    6
    7   import java.util.ArrayList;
    8   import java.util.Iterator;
    9   import java.util.List;
    10  import java.util.NoSuchElementException;
    11
    12  import net.sourceforge.pmd.util.CompoundIterator;
    13
    14  import org.junit.Before;
    15  import org.junit.Test;
    16
    17  public class CompoundListTest {
    18
    19      private List<String> l1;
    20      private List<String> l2;
    21      private Iterator<String> iterator;
    22
    23      @Before
    24      public void setUp() {
    25  	l1 = new ArrayList<String>();
    26  	l1.add("1");
    27  	l1.add("2");
    28  	l2 = new ArrayList<String>();
    29  	l2.add("3");
    30  	l2.add("4");
    31
    32  	iterator = new CompoundIterator<String>(l1.iterator(), l2.iterator());
    33      }
    34
    35      @Test
    36      public void testHappyPath() {
    37  	assertTrue(iterator.hasNext());
    38  	assertEquals("1", iterator.next());
    39  	assertTrue(iterator.hasNext());
    40  	assertEquals("2", iterator.next());
    41  	assertTrue(iterator.hasNext());
    42  	assertEquals("3", iterator.next());
    43  	assertTrue(iterator.hasNext());
    44  	assertEquals("4", iterator.next());
    45  	assertFalse(iterator.hasNext());
    46  	assertEquals(2, l1.size());
    47  	assertEquals(2, l2.size());
    48      }
    49
    50      @Test
    51      public void testHappyPathRemove() {
    52  	assertTrue(iterator.hasNext());
    53  	assertEquals("1", iterator.next());
    54  	iterator.remove();
    55  	assertTrue(iterator.hasNext());
    56  	assertEquals("2", iterator.next());
    57  	assertTrue(iterator.hasNext());
    58  	assertEquals("3", iterator.next());
    59  	iterator.remove();
    60  	assertTrue(iterator.hasNext());
    61  	assertEquals("4", iterator.next());
    62  	assertFalse(iterator.hasNext());
    63  	assertEquals(1, l1.size());
    64  	assertEquals("2", l1.get(0));
    65  	assertEquals(1, l2.size());
    66  	assertEquals("4", l2.get(0));
    67      }
    68
    69      @Test
    70      public void testEmpty() {
    71  	Iterator<?> iterator = new CompoundIterator();
    72  	assertFalse(iterator.hasNext());
    73      }
    74
    75      @Test(expected = NoSuchElementException.class)
    76      public void testEmptyBadNext() {
    77  	Iterator<?> iterator = new CompoundIterator();
    78  	iterator.next();
    79      }
    80
    81      @Test(expected = IllegalStateException.class)
    82      public void testEmptyBadRemove() {
    83  	Iterator<?> iterator = new CompoundIterator();
    84  	iterator.remove();
    85      }
    86
    87      public static junit.framework.Test suite() {
    88  	return new junit.framework.JUnit4TestAdapter(CompoundListTest.class);
    89      }
    90  }