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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.ListIterator;
29  
30  import org.apache.commons.collections4.Transformer;
31  import org.apache.commons.collections4.collection.TransformedCollectionTest;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Extension of {@link AbstractListTest} for exercising the {@link TransformedList}
36   * implementation.
37   */
38  public class TransformedListTest<E> extends AbstractListTest<E> {
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      @Override
46      public List<E> makeConfirmedCollection() {
47          return new ArrayList<>();
48      }
49  
50      @Override
51      public List<E> makeConfirmedFullCollection() {
52          return new ArrayList<>(Arrays.asList(getFullElements()));
53      }
54  
55      @Override
56      @SuppressWarnings("unchecked")
57      public List<E> makeFullCollection() {
58          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
59          return TransformedList.transformingList(list, (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
60      }
61  
62      @Override
63      @SuppressWarnings("unchecked")
64      public List<E> makeObject() {
65          return TransformedList.transformingList(new ArrayList<>(), (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
66      }
67  
68      @Test
69      public void testSubList() {
70          final List<E> list = makeObject();
71          List<E> subList = list.subList(0, 0);
72          assertNotNull(subList);
73          list.add((E) "zero");
74          //subList without any element of list
75          subList = list.subList(0, 0);
76          assertNotNull(subList);
77          assertEquals(0, subList.size());
78  
79          //subList with one element oif list
80          subList = list.subList(0, 1);
81          assertEquals(1, subList.size());
82  
83          final List<E> elements = new ArrayList<>();
84          elements.add((E) "one");
85          elements.add((E) "two");
86          elements.add((E) "three");
87          list.addAll(1, elements);
88          //subList with all elements of list
89          subList = list.subList(0, list.size());
90          assertEquals(list.size(), subList.size());
91      }
92  
93      @Test
94      @SuppressWarnings("unchecked")
95      public void testTransformedList() {
96          final List<E> list = TransformedList.transformingList(new ArrayList<>(), (Transformer<E, E>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
97          assertEquals(0, list.size());
98          final E[] els = (E[]) new Object[] {"1", "3", "5", "7", "2", "4", "6"};
99          for (int i = 0; i < els.length; i++) {
100             list.add(els[i]);
101             assertEquals(i + 1, list.size());
102             assertTrue(list.contains(Integer.valueOf((String) els[i])));
103             assertFalse(list.contains(els[i]));
104         }
105 
106         assertFalse(list.remove(els[0]));
107         assertTrue(list.remove(Integer.valueOf((String) els[0])));
108 
109         list.clear();
110         for (int i = 0; i < els.length; i++) {
111             list.add(0, els[i]);
112             assertEquals(i + 1, list.size());
113             assertEquals(Integer.valueOf((String) els[i]), list.get(0));
114         }
115 
116         list.set(0, (E) "22");
117         assertEquals(Integer.valueOf(22), list.get(0));
118 
119         final ListIterator<E> it = list.listIterator();
120         it.next();
121         it.set((E) "33");
122         assertEquals(Integer.valueOf(33), list.get(0));
123         it.add((E) "44");
124         assertEquals(Integer.valueOf(44), list.get(1));
125 
126         final List<E> adds = new ArrayList<>();
127         adds.add((E) "1");
128         adds.add((E) "2");
129         list.clear();
130         list.addAll(adds);
131         assertEquals(Integer.valueOf(1), list.get(0));
132         assertEquals(Integer.valueOf(2), list.get(1));
133 
134         adds.clear();
135         adds.add((E) "3");
136         list.addAll(1, adds);
137         assertEquals(Integer.valueOf(1), list.get(0));
138         assertEquals(Integer.valueOf(3), list.get(1));
139         assertEquals(Integer.valueOf(2), list.get(2));
140     }
141 
142     @Test
143     public void testTransformedList_decorateTransform() {
144         final List<Object> originalList = new ArrayList<>();
145         final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
146         Collections.addAll(originalList, els);
147         final List<?> list = TransformedList.transformedList(originalList, TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
148         assertEquals(els.length, list.size());
149         for (final Object el : els) {
150             assertTrue(list.contains(Integer.valueOf((String) el)));
151             assertFalse(list.contains(el));
152         }
153 
154         assertFalse(list.remove(els[0]));
155         assertTrue(list.remove(Integer.valueOf((String) els[0])));
156     }
157 
158 //    public void testCreate() throws Exception {
159 //        resetEmpty();
160 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedList.emptyCollection.version4.obj");
161 //        resetFull();
162 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedList.fullCollection.version4.obj");
163 //    }
164 
165 }