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.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.core.Is.is;
21  import static org.hamcrest.core.IsEqual.equalTo;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collection;
29  import java.util.List;
30  
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.api.function.Executable;
33  
34  /**
35   * Extension of {@link AbstractListTest} for exercising the {@link GrowthList}.
36   */
37  public class GrowthListTest<E> extends AbstractListTest<E> {
38  
39      public GrowthListTest() {
40          super(GrowthListTest.class.getSimpleName());
41      }
42  
43      @Override
44      public String getCompatibilityVersion() {
45          return "4";
46      }
47  
48      @Override
49      public List<E> makeFullCollection() {
50          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
51          return GrowthList.growthList(list);
52      }
53  
54      @Override
55      public List<E> makeObject() {
56          return new GrowthList<>();
57      }
58  
59      @Test
60      public void testGrowthAdd() {
61          final Integer one = Integer.valueOf(1);
62          final GrowthList<Integer> grower = new GrowthList<>();
63          assertEquals(0, grower.size());
64          grower.add(1, one);
65          assertEquals(2, grower.size());
66          assertNull(grower.get(0));
67          assertEquals(one, grower.get(1));
68      }
69  
70      @Test
71      public void testGrowthAddAll() {
72          final Integer one = Integer.valueOf(1);
73          final Integer two = Integer.valueOf(2);
74          final Collection<Integer> coll = new ArrayList<>();
75          coll.add(one);
76          coll.add(two);
77          final GrowthList<Integer> grower = new GrowthList<>();
78          assertEquals(0, grower.size());
79          grower.addAll(1, coll);
80          assertEquals(3, grower.size());
81          assertNull(grower.get(0));
82          assertEquals(one, grower.get(1));
83          assertEquals(two, grower.get(2));
84      }
85  
86      @Test
87      public void testGrowthList() {
88          final Integer zero = Integer.valueOf(0);
89          final Integer one = Integer.valueOf(1);
90          final Integer two = Integer.valueOf(2);
91          final GrowthList<Integer> grower = new GrowthList(1);
92          assertEquals(0, grower.size());
93          grower.add(0, zero);
94          assertEquals(1, grower.size());
95          grower.add(1, one);
96          assertEquals(2, grower.size());
97          grower.add(2, two);
98          assertEquals(3, grower.size());
99      }
100 
101     @Test
102     public void testGrowthSet1() {
103         final Integer one = Integer.valueOf(1);
104         final GrowthList<Integer> grower = new GrowthList<>();
105         assertEquals(0, grower.size());
106         grower.set(1, one);
107         assertEquals(2, grower.size());
108         assertNull(grower.get(0));
109         assertEquals(one, grower.get(1));
110     }
111 
112     @Test
113     public void testGrowthSet2() {
114         final Integer one = Integer.valueOf(1);
115         final GrowthList<Integer> grower = new GrowthList<>();
116         assertEquals(0, grower.size());
117         grower.set(0, one);
118         assertEquals(1, grower.size());
119         assertEquals(one, grower.get(0));
120     }
121 
122     /**
123      * Override.
124      */
125     @Test
126     @Override
127     public void testListAddByIndexBoundsChecking() {
128         final E element = getOtherElements()[0];
129         final List<E> list = makeObject();
130 
131         final Executable testMethod = () -> list.add(-1, element);
132         final IndexOutOfBoundsException thrown = assertThrows(IndexOutOfBoundsException.class, testMethod,
133                 "List.add should throw IndexOutOfBoundsException [-1]");
134         assertThat(thrown.getMessage(), is(equalTo("Index: -1, Size: 0")));
135     }
136 
137     /**
138      * Override.
139      */
140     @Test
141     @Override
142     public void testListAddByIndexBoundsChecking2() {
143         final E element = getOtherElements()[0];
144         final List<E> list = makeFullCollection();
145         assertThrows(IndexOutOfBoundsException.class, () -> list.add(-1, element),
146                 "List.add should throw IndexOutOfBoundsException [-1]");
147     }
148 
149     /**
150      * Override.
151      */
152     @Test
153     @Override
154     public void testListSetByIndexBoundsChecking() {
155         final List<E> list = makeObject();
156         final E element = getOtherElements()[0];
157         assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element),
158                 "List.set should throw IndexOutOfBoundsException [-1]");
159     }
160 
161     /**
162      * Override.
163      */
164     @Test
165     @Override
166     public void testListSetByIndexBoundsChecking2() {
167         final List<E> list = makeFullCollection();
168         final E element = getOtherElements()[0];
169         assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, element),
170                 "List.set should throw IndexOutOfBoundsException [-1]");
171     }
172 
173 //    public void testCreate() throws Exception {
174 //        resetEmpty();
175 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/GrowthList.emptyCollection.version4.obj");
176 //        resetFull();
177 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/GrowthList.fullCollection.version4.obj");
178 //    }
179 
180 }