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