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      public TransformedSortedBagTest() {
34          super(TransformedSortedBagTest.class.getSimpleName());
35      }
36  
37      @Override
38      public String getCompatibilityVersion() {
39          return "4";
40      }
41  
42      @Override
43      @SuppressWarnings("unchecked")
44      public SortedBag<T> makeObject() {
45          return TransformedSortedBag.transformingSortedBag(new TreeBag<>(), (Transformer<T, T>) TransformedCollectionTest.NOOP_TRANSFORMER);
46      }
47  
48      @Test
49      @SuppressWarnings("unchecked")
50      public void testTransformedBag() {
51          final SortedBag<T> bag = TransformedSortedBag.transformingSortedBag(new TreeBag<>(), (Transformer<T, T>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
52          assertEquals(0, bag.size());
53          final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
54          for (int i = 0; i < els.length; i++) {
55              bag.add((T) els[i]);
56              assertEquals(i + 1, bag.size());
57              assertTrue(bag.contains(Integer.valueOf((String) els[i])));
58          }
59  
60          assertTrue(bag.remove(Integer.valueOf((String) els[0])));
61  
62      }
63  
64      @Test
65      public void testTransformedBag_decorateTransform() {
66          final TreeBag<T> originalBag = new TreeBag<>();
67          final Object[] els = {"1", "3", "5", "7", "2", "4", "6"};
68          for (final Object el : els) {
69              originalBag.add((T) el);
70          }
71          final SortedBag<T> bag = TransformedSortedBag.transformedSortedBag(originalBag, (Transformer<T, T>) TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
72          assertEquals(els.length, bag.size());
73          for (final Object el : els) {
74              assertTrue(bag.contains(Integer.valueOf((String) el)));
75          }
76  
77          assertTrue(bag.remove(Integer.valueOf((String) els[0])));
78      }
79  
80  //    public void testCreate() throws Exception {
81  //        Bag<T> bag = makeObject();
82  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedSortedBag.emptyCollection.version4.obj");
83  //        bag = makeFullCollection();
84  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedSortedBag.fullCollection.version4.obj");
85  //    }
86  
87  }