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.collection;
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.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.commons.collections4.Transformer;
30  import org.apache.commons.collections4.TransformerUtils;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Extension of {@link AbstractCollectionTest} for exercising the {@link TransformedCollection}
35   * implementation.
36   */
37  public class TransformedCollectionTest extends AbstractCollectionTest<Object> {
38  
39      private static final class StringToInteger implements Transformer<Object, Object> {
40          @Override
41          public Object transform(final Object input) {
42              return Integer.valueOf((String) input);
43          }
44      }
45  
46      private static final class ToLowerCase implements Transformer<Object, Object> {
47          @Override
48          public Object transform(final Object input) {
49              return ((String) input).toLowerCase();
50          }
51      }
52  
53      public static final Transformer<Object, Object> NOOP_TRANSFORMER = TransformerUtils.nopTransformer();
54      public static final Transformer<Object, Object> STRING_TO_INTEGER_TRANSFORMER = new StringToInteger();
55      public static final Transformer<Object, Object> TO_LOWER_CASE_TRANSFORMER = new ToLowerCase();
56  
57      @Override
58      public String getCompatibilityVersion() {
59          return "4";
60      }
61  
62      @Override
63      public Object[] getFullElements() {
64          return new Object[] {"1", "3", "5", "7", "2", "4", "6"};
65      }
66  
67      @Override
68      public Object[] getOtherElements() {
69          return new Object[] {"9", "88", "678", "87", "98", "78", "99"};
70      }
71  
72      @Override
73      public Collection<Object> makeConfirmedCollection() {
74          return new ArrayList<>();
75      }
76  
77      @Override
78      public Collection<Object> makeConfirmedFullCollection() {
79          return new ArrayList<>(Arrays.asList(getFullElements()));
80      }
81  
82      @Override
83      public Collection<Object> makeFullCollection() {
84          final List<Object> list = new ArrayList<>(Arrays.asList(getFullElements()));
85          return TransformedCollection.transformingCollection(list, NOOP_TRANSFORMER);
86      }
87  
88      @Override
89      public Collection<Object> makeObject() {
90          return TransformedCollection.transformingCollection(new ArrayList<>(), NOOP_TRANSFORMER);
91      }
92  
93      @Test
94      public void testTransformedCollection() {
95          final Collection<Object> coll = TransformedCollection.transformingCollection(new ArrayList<>(), STRING_TO_INTEGER_TRANSFORMER);
96          assertEquals(0, coll.size());
97          final Object[] elements = getFullElements();
98          for (int i = 0; i < elements.length; i++) {
99              coll.add(elements[i]);
100             assertEquals(i + 1, coll.size());
101             assertTrue(coll.contains(Integer.valueOf((String) elements[i])));
102             assertFalse(coll.contains(elements[i]));
103         }
104 
105         assertTrue(coll.remove(Integer.valueOf((String) elements[0])));
106     }
107 
108     @Test
109     public void testTransformedCollection_decorateTransform() {
110         final Collection<Object> originalCollection = new ArrayList<>();
111         final Object[] elements = getFullElements();
112         Collections.addAll(originalCollection, elements);
113         final Collection<Object> collection = TransformedCollection.transformedCollection(originalCollection, TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
114         assertEquals(elements.length, collection.size());
115         for (final Object element : elements) {
116             assertTrue(collection.contains(Integer.valueOf((String) element)));
117             assertFalse(collection.contains(element));
118         }
119 
120         assertFalse(collection.remove(elements[0]));
121         assertTrue(collection.remove(Integer.valueOf((String) elements[0])));
122     }
123 
124 //    public void testCreate() throws Exception {
125 //        resetEmpty();
126 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedCollection.emptyCollection.version4.obj");
127 //        resetFull();
128 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedCollection.fullCollection.version4.obj");
129 //    }
130 
131 }