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.assertTrue;
21  
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.Set;
25  import java.util.SortedSet;
26  import java.util.TreeSet;
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 AbstractSortedSetTest} for exercising the {@link TransformedSortedSet}
34   * implementation.
35   */
36  public class TransformedSortedSetTest<E> extends AbstractSortedSetTest<E> {
37  
38      @Override
39      public String getCompatibilityVersion() {
40          return "4";
41      }
42  
43      @Override
44      @SuppressWarnings("unchecked")
45      public SortedSet<E> makeFullCollection() {
46          final SortedSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
47          return TransformedSortedSet.transformingSortedSet(set, (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
48      }
49  
50      @Override
51      @SuppressWarnings("unchecked")
52      public SortedSet<E> makeObject() {
53          return TransformedSortedSet.transformingSortedSet(new TreeSet<>(), (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
54      }
55  
56      @Test
57      @SuppressWarnings("unchecked")
58      public void testTransformedSet() {
59          final SortedSet<E> set = TransformedSortedSet.transformingSortedSet(new TreeSet<>(),
60                  (Transformer<E, E>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
61          assertEquals(0, set.size());
62          final E[] els = (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
63          for (int i = 0; i < els.length; i++) {
64              set.add(els[i]);
65              assertEquals(i + 1, set.size());
66              assertTrue(set.contains(Integer.valueOf((String) els[i])));
67          }
68  
69          assertTrue(set.remove(Integer.valueOf((String) els[0])));
70      }
71  
72      @Test
73      public void testTransformedSet_decorateTransform() {
74          final Set<Object> originalSet = new TreeSet<>();
75          final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
76          Collections.addAll(originalSet, els);
77          final Set<?> set = TransformedSet.transformedSet(originalSet, TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
78          assertEquals(els.length, set.size());
79          for (final Object el : els) {
80              assertTrue(set.contains(Integer.valueOf((String) el)));
81          }
82  
83          assertTrue(set.remove(Integer.valueOf((String) els[0])));
84      }
85  
86  //    public void testCreate() throws Exception {
87  //        resetEmpty();
88  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedSortedSet.emptyCollection.version4.obj");
89  //        resetFull();
90  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedSortedSet.fullCollection.version4.obj");
91  //    }
92  
93  }