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