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