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