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.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Extension of {@link AbstractListTest} for exercising the {@link FixedSizeList}
32   * implementation.
33   */
34  public class FixedSizeListTest<E> extends AbstractListTest<E> {
35  
36      public FixedSizeListTest() {
37          super(FixedSizeListTest.class.getSimpleName());
38      }
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      private FixedSizeList<String> initFixedSizeList() {
46          final List<String> decoratedList = new ArrayList<>();
47          decoratedList.add("item 1");
48          decoratedList.add("item 2");
49          //
50          return FixedSizeList.fixedSizeList(decoratedList);
51      }
52  
53      @Override
54      public boolean isAddSupported() {
55          return false;
56      }
57  
58      @Override
59      public boolean isRemoveSupported() {
60          return false;
61      }
62  
63      @Override
64      public List<E> makeFullCollection() {
65          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
66          return FixedSizeList.fixedSizeList(list);
67      }
68  
69  //    public void testCreate() throws Exception {
70  //        resetEmpty();
71  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/FixedSizeList.emptyCollection.version4.obj");
72  //        resetFull();
73  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/FixedSizeList.fullCollection.version4.obj");
74  //    }
75  
76      @Override
77      public List<E> makeObject() {
78          return FixedSizeList.fixedSizeList(new ArrayList<>());
79      }
80  
81      @Test
82      public void testAdd() {
83          final FixedSizeList<String> fixedSizeList = initFixedSizeList();
84  
85          assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.add(2, "New Value"));
86      }
87  
88      @Test
89      public void testAddAll() {
90          final FixedSizeList<String> fixedSizeList = initFixedSizeList();
91  
92          final List<String> addList = new ArrayList<>();
93          addList.add("item 3");
94          addList.add("item 4");
95  
96          assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.addAll(2, addList));
97      }
98  
99      @Test
100     public void testIsFull() {
101         final FixedSizeList<String> fixedSizeList = initFixedSizeList();
102 
103         assertTrue(fixedSizeList.isFull());
104     }
105 
106     @Test
107     public void testListAllowsMutationOfUnderlyingCollection() {
108 
109         final List<String> decoratedList = new ArrayList<>();
110         decoratedList.add("item 1");
111         decoratedList.add("item 2");
112         //
113         final FixedSizeList<String> fixedSizeList = FixedSizeList.fixedSizeList(decoratedList);
114         final int sizeBefore = fixedSizeList.size();
115         //
116         final boolean changed = decoratedList.add("New Value");
117         assertTrue(changed);
118         //
119         assertEquals(sizeBefore + 1, fixedSizeList.size(),
120                 "Modifying an the underlying list is allowed");
121     }
122 
123     @Test
124     public void testMaxSize() {
125         final FixedSizeList<String> fixedSizeList = initFixedSizeList();
126 
127         assertEquals(2, fixedSizeList.maxSize());
128     }
129 
130     @Test
131     public void testRemove() {
132         final FixedSizeList<String> fixedSizeList = initFixedSizeList();
133 
134         assertThrows(UnsupportedOperationException.class, () -> fixedSizeList.remove(1));
135     }
136 
137     @Test
138     public void testSubList() {
139         final FixedSizeList<String> fixedSizeList = initFixedSizeList();
140 
141         final List<String> subFixedSizeList = fixedSizeList.subList(1, 1);
142         assertNotNull(subFixedSizeList);
143         assertEquals(0, subFixedSizeList.size());
144     }
145 
146 }