View Javadoc
1   package org.junit.samples;
2   
3   import static org.junit.Assert.assertTrue;
4   
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import junit.framework.JUnit4TestAdapter;
9   import org.junit.Before;
10  import org.junit.BeforeClass;
11  import org.junit.Ignore;
12  import org.junit.Test;
13  
14  /**
15   * A sample test case, testing {@link java.util.ArrayList}.
16   */
17  public class ListTest {
18      protected List<Integer> fEmpty;
19      protected List<Integer> fFull;
20      protected static List<Integer> fgHeavy;
21  
22      public static void main(String... args) {
23          junit.textui.TestRunner.run(suite());
24      }
25  
26      @BeforeClass
27      public static void setUpOnce() {
28          fgHeavy = new ArrayList<Integer>();
29          for (int i = 0; i < 1000; i++) {
30              fgHeavy.add(i);
31          }
32      }
33  
34      @Before
35      public void setUp() {
36          fEmpty = new ArrayList<Integer>();
37          fFull = new ArrayList<Integer>();
38          fFull.add(1);
39          fFull.add(2);
40          fFull.add(3);
41      }
42  
43      public static junit.framework.Test suite() {
44          return new JUnit4TestAdapter(ListTest.class);
45      }
46  
47      @Ignore("not today")
48      @Test
49      public void capacity() {
50          int size = fFull.size();
51          for (int i = 0; i < 100; i++) {
52              fFull.add(i);
53          }
54          assertTrue(fFull.size() == 100 + size);
55      }
56  
57      @Test
58      public void testCopy() {
59          List<Integer> copy = new ArrayList<Integer>(fFull.size());
60          copy.addAll(fFull);
61          assertTrue(copy.size() == fFull.size());
62          assertTrue(copy.contains(1));
63      }
64  
65      @Test
66      public void contains() {
67          assertTrue(fFull.contains(1));
68          assertTrue(!fEmpty.contains(1));
69      }
70  
71      @Test(expected = IndexOutOfBoundsException.class)
72      public void elementAt() {
73          int i = fFull.get(0);
74          assertTrue(i == 1);
75          fFull.get(fFull.size()); // Should throw IndexOutOfBoundsException
76      }
77  
78      @Test
79      public void removeAll() {
80          fFull.removeAll(fFull);
81          fEmpty.removeAll(fEmpty);
82          assertTrue(fFull.isEmpty());
83          assertTrue(fEmpty.isEmpty());
84      }
85  
86      @Test
87      public void removeElement() {
88          fFull.remove(new Integer(3));
89          assertTrue(!fFull.contains(3));
90      }
91  }