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.set;
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.Arrays;
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.apache.commons.collections4.Transformer;
29  import org.apache.commons.collections4.collection.TransformedCollectionTest;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link AbstractSetTest} for exercising the {@link TransformedSet}
34   * implementation.
35   */
36  public class TransformedSetTest<E> extends AbstractSetTest<E> {
37  
38      @Override
39      public String getCompatibilityVersion() {
40          return "4";
41      }
42  
43      @Override
44      protected int getIterationBehaviour() {
45          return UNORDERED;
46      }
47  
48      @Override
49      public Set<E> makeConfirmedCollection() {
50          return new HashSet<>();
51      }
52  
53      @Override
54      public Set<E> makeConfirmedFullCollection() {
55          return new HashSet<>(Arrays.asList(getFullElements()));
56      }
57  
58      @Override
59      @SuppressWarnings("unchecked")
60      public Set<E> makeFullCollection() {
61          final Set<E> list = new HashSet<>(Arrays.asList(getFullElements()));
62          return TransformedSet.transformingSet(list,
63                  (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
64      }
65  
66      @Override
67      @SuppressWarnings("unchecked")
68      public Set<E> makeObject() {
69          return TransformedSet.transformingSet(new HashSet<>(),
70                  (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
71      }
72  
73      @Test
74      @SuppressWarnings("unchecked")
75      public void testTransformedSet() {
76          final Set<E> set = TransformedSet.transformingSet(new HashSet<>(),
77                  (Transformer<E, E>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
78          assertEquals(0, set.size());
79          final E[] els = (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
80          for (int i = 0; i < els.length; i++) {
81              set.add(els[i]);
82              assertEquals(i + 1, set.size());
83              assertTrue(set.contains(Integer.valueOf((String) els[i])));
84              assertFalse(set.contains(els[i]));
85          }
86  
87          assertFalse(set.remove(els[0]));
88          assertTrue(set.remove(Integer.valueOf((String) els[0])));
89  
90      }
91  
92      @Test
93      public void testTransformedSet_decorateTransform() {
94          final Set<Object> originalSet = new HashSet<>();
95          final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
96          Collections.addAll(originalSet, els);
97          final Set<?> set = TransformedSet.transformedSet(originalSet, TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
98          assertEquals(els.length, set.size());
99          for (final Object el : els) {
100             assertTrue(set.contains(Integer.valueOf((String) el)));
101             assertFalse(set.contains(el));
102         }
103 
104         assertFalse(set.remove(els[0]));
105         assertTrue(set.remove(Integer.valueOf((String) els[0])));
106     }
107 
108 //    public void testCreate() throws Exception {
109 //        resetEmpty();
110 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedSet.emptyCollection.version4.obj");
111 //        resetFull();
112 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedSet.fullCollection.version4.obj");
113 //    }
114 
115 }