View Javadoc

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