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