View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.list;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.function.Executable;
30  
31  /**
32   * Extension of {@link AbstractListTest} for exercising the {@link GrowthList}.
33   */
34  public class GrowthListTest<E> extends AbstractListTest<E> {
35  
36      @Override
37      public String getCompatibilityVersion() {
38          return "4";
39      }
40  
41      @Override
42      public List<E> makeFullCollection() {
43          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
44          return GrowthList.growthList(list);
45      }
46  
47      @Override
48      public List<E> makeObject() {
49          return new GrowthList<>();
50      }
51  
52      @Test
53      public void testGrowthAdd() {
54          final Integer one = Integer.valueOf(1);
55          final GrowthList<Integer> grower = new GrowthList<>();
56          assertEquals(0, grower.size());
57          grower.add(1, one);
58          assertEquals(2, grower.size());
59          assertNull(grower.get(0));
60          assertEquals(one, grower.get(1));
61      }
62  
63      @Test
64      public void testGrowthAddAll() {
65          final Integer one = Integer.valueOf(1);
66          final Integer two = Integer.valueOf(2);
67          final Collection<Integer> coll = new ArrayList<>();
68          coll.add(one);
69          coll.add(two);
70          final GrowthList<Integer> grower = new GrowthList<>();
71          assertEquals(0, grower.size());
72          grower.addAll(1, coll);
73          assertEquals(3, grower.size());
74          assertNull(grower.get(0));
75          assertEquals(one, grower.get(1));
76          assertEquals(two, grower.get(2));
77      }
78  
79      @Test
80      public void testGrowthList() {
81          final Integer zero = Integer.valueOf(0);
82          final Integer one = Integer.valueOf(1);
83          final Integer two = Integer.valueOf(2);
84          final GrowthList<Integer> grower = new GrowthList(1);
85          assertEquals(0, grower.size());
86          grower.add(0, zero);
87          assertEquals(1, grower.size());
88          grower.add(1, one);
89          assertEquals(2, grower.size());
90          grower.add(2, two);
91          assertEquals(3, grower.size());
92      }
93  
94      @Test
95      public void testGrowthSet1() {
96          final Integer one = Integer.valueOf(1);
97          final GrowthList<Integer> grower = new GrowthList<>();
98          assertEquals(0, grower.size());
99          grower.set(1, one);
100         assertEquals(2, grower.size());
101         assertNull(grower.get(0));
102         assertEquals(one, grower.get(1));
103     }
104 
105     @Test
106     public void testGrowthSet2() {
107         final Integer one = Integer.valueOf(1);
108         final GrowthList<Integer> grower = new GrowthList<>();
109         assertEquals(0, grower.size());
110         grower.set(0, one);
111         assertEquals(1, grower.size());
112         assertEquals(one, grower.get(0));
113     }
114 
115     /**
116      * Override.
117      */
118     @Test
119     @Override
120     public void testListAddByIndexBoundsChecking() {
121         final E element = getOtherElements()[0];
122         final List<E> list = makeObject();
123 
124         final Executable testMethod = () -> list.add(-1, element);
125         final IndexOutOfBoundsException thrown = assertThrows(IndexOutOfBoundsException.class, testMethod,
126                 "List.add should throw IndexOutOfBoundsException [-1]");
127         assertEquals("Index: -1, Size: 0", thrown.getMessage());
128     }
129 
130     /**
131      * Override.
132      */
133     @Test
134     @Override
135     public void testListAddByIndexBoundsChecking2() {
136         final E element = getOtherElements()[0];
137         final List<E> list = makeFullCollection();
138         assertThrows(IndexOutOfBoundsException.class, () -> list.add(-1, element),
139                 "List.add should throw IndexOutOfBoundsException [-1]");
140     }
141 
142     /**
143      * Override.
144      */
145     @Test
146     @Override
147     public void testListSetByIndexBoundsChecking() {
148         final List<E> list = makeObject();
149         final E element = getOtherElements()[0];
150         assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element),
151                 "List.set should throw IndexOutOfBoundsException [-1]");
152     }
153 
154     /**
155      * Override.
156      */
157     @Test
158     @Override
159     public void testListSetByIndexBoundsChecking2() {
160         final List<E> list = makeFullCollection();
161         final E element = getOtherElements()[0];
162         assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element),
163                 "List.set should throw IndexOutOfBoundsException [-1]");
164     }
165 
166 //    public void testCreate() throws Exception {
167 //        resetEmpty();
168 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/GrowthList.emptyCollection.version4.obj");
169 //        resetFull();
170 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/GrowthList.fullCollection.version4.obj");
171 //    }
172 
173 }