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