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.bag;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import org.apache.commons.collections4.SortedBag;
23  import org.apache.commons.collections4.Transformer;
24  import org.apache.commons.collections4.collection.TransformedCollectionTest;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Extension of {@link AbstractSortedBagTest} for exercising the {@link TransformedSortedBag}
29   * implementation.
30   */
31  public class TransformedSortedBagTest<T> extends AbstractSortedBagTest<T> {
32  
33      @Override
34      public String getCompatibilityVersion() {
35          return "4";
36      }
37  
38      @Override
39      @SuppressWarnings("unchecked")
40      public SortedBag<T> makeObject() {
41          return TransformedSortedBag.transformingSortedBag(new TreeBag<>(), (Transformer<T, T>) TransformedCollectionTest.NOOP_TRANSFORMER);
42      }
43  
44      @Test
45      @SuppressWarnings("unchecked")
46      public void testTransformedBag() {
47          final SortedBag<T> bag = TransformedSortedBag.transformingSortedBag(new TreeBag<>(), (Transformer<T, T>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
48          assertEquals(0, bag.size());
49          final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
50          for (int i = 0; i < els.length; i++) {
51              bag.add((T) els[i]);
52              assertEquals(i + 1, bag.size());
53              assertTrue(bag.contains(Integer.valueOf((String) els[i])));
54          }
55  
56          assertTrue(bag.remove(Integer.valueOf((String) els[0])));
57  
58      }
59  
60      @Test
61      public void testTransformedBag_decorateTransform() {
62          final TreeBag<T> originalBag = new TreeBag<>();
63          final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
64          for (final Object el : els) {
65              originalBag.add((T) el);
66          }
67          final SortedBag<T> bag = TransformedSortedBag.transformedSortedBag(originalBag, (Transformer<T, T>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
68          assertEquals(els.length, bag.size());
69          for (final Object el : els) {
70              assertTrue(bag.contains(Integer.valueOf((String) el)));
71          }
72  
73          assertTrue(bag.remove(Integer.valueOf((String) els[0])));
74      }
75  
76  //    public void testCreate() throws Exception {
77  //        Bag<T> bag = makeObject();
78  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedSortedBag.emptyCollection.version4.obj");
79  //        bag = makeFullCollection();
80  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedSortedBag.fullCollection.version4.obj");
81  //    }
82  
83  }