View Javadoc
1   package junit.samples;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import junit.framework.Test;
7   import junit.framework.TestCase;
8   import junit.framework.TestSuite;
9   
10  /**
11   * A sample test case, testing {@link java.util.ArrayList}.
12   */
13  public class ListTest extends TestCase {
14      protected List<Integer> fEmpty;
15      protected List<Integer> fFull;
16  
17      public static void main(String[] args) {
18          junit.textui.TestRunner.run(suite());
19      }
20  
21      @Override
22      protected void setUp() {
23          fEmpty = new ArrayList<Integer>();
24          fFull = new ArrayList<Integer>();
25          fFull.add(1);
26          fFull.add(2);
27          fFull.add(3);
28      }
29  
30      public static Test suite() {
31          return new TestSuite(ListTest.class);
32      }
33  
34      public void testCapacity() {
35          int size = fFull.size();
36          for (int i = 0; i < 100; i++) {
37              fFull.add(new Integer(i));
38          }
39          assertTrue(fFull.size() == 100 + size);
40      }
41  
42      public void testContains() {
43          assertTrue(fFull.contains(1));
44          assertTrue(!fEmpty.contains(1));
45      }
46  
47      public void testElementAt() {
48          int i = fFull.get(0);
49          assertTrue(i == 1);
50  
51          try {
52              fFull.get(fFull.size());
53          } catch (IndexOutOfBoundsException e) {
54              return;
55          }
56          fail("Should raise an ArrayIndexOutOfBoundsException");
57      }
58  
59      public void testRemoveAll() {
60          fFull.removeAll(fFull);
61          fEmpty.removeAll(fEmpty);
62          assertTrue(fFull.isEmpty());
63          assertTrue(fEmpty.isEmpty());
64      }
65  
66      public void testRemoveElement() {
67          fFull.remove(new Integer(3));
68          assertTrue(!fFull.contains(3));
69      }
70  }