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.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.IOException;
24  import java.io.Serializable;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.Comparator;
29  
30  import org.apache.commons.collections4.Bag;
31  import org.apache.commons.collections4.Predicate;
32  import org.apache.commons.collections4.collection.AbstractCollectionTest;
33  import org.apache.commons.collections4.functors.NonePredicate;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test class for {@link CollectionBag}.
38   * <p>
39   * Note: This test is mainly for serialization support, the CollectionBag decorator
40   * is extensively used and tested in AbstractBagTest.
41   */
42  public class CollectionBagTest<T> extends AbstractCollectionTest<T> {
43  
44      @Override
45      public String getCompatibilityVersion() {
46          return "4";
47      }
48  
49      @Override
50      protected int getIterationBehaviour() {
51          return UNORDERED;
52      }
53  
54      /**
55       * Returns an empty List for use in modification testing.
56       *
57       * @return a confirmed empty collection
58       */
59      @Override
60      public Collection<T> makeConfirmedCollection() {
61          return new ArrayList<>();
62      }
63  
64      /**
65       * Returns a full Set for use in modification testing.
66       *
67       * @return a confirmed full collection
68       */
69      @Override
70      public Collection<T> makeConfirmedFullCollection() {
71          final Collection<T> set = makeConfirmedCollection();
72          set.addAll(Arrays.asList(getFullElements()));
73          return set;
74      }
75  
76      @Override
77      public Bag<T> makeObject() {
78          return CollectionBag.collectionBag(new HashBag<>());
79      }
80  
81      @Test
82      public void testAdd_Predicate_ComparatorCustom() throws Throwable {
83          final TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>(Comparator.comparing(Predicate::toString));
84          final CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject);
85          collectionBagOfPredicateOfObject.add(NonePredicate.nonePredicate(collectionBagOfPredicateOfObject), 24);
86      }
87  
88      @Test
89      public void testAdd_Predicate_ComparatorDefault() throws Throwable {
90          final TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>();
91          final CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject);
92          assertThrows(ClassCastException.class, () -> collectionBagOfPredicateOfObject.add(NonePredicate.nonePredicate(collectionBagOfPredicateOfObject), 24));
93      }
94  
95  //    public void testCreate() throws Exception {
96  //        resetEmpty();
97  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionBag.emptyCollection.version4.obj");
98  //        resetFull();
99  //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionBag.fullCollection.version4.obj");
100 //    }
101 
102     /**
103      * Compares the current serialized form of the Bag
104      * against the canonical version in SCM.
105      */
106     @Test
107     public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
108         // test to make sure the canonical form has been preserved
109         final Bag<T> bag = makeObject();
110         if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
111             final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
112             assertTrue(bag2.isEmpty(), "Bag is empty");
113             assertEquals(bag, bag2);
114         }
115     }
116 
117     /**
118      * Compares the current serialized form of the Bag
119      * against the canonical version in SCM.
120      */
121     @Test
122     public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
123         // test to make sure the canonical form has been preserved
124         final Bag<T> bag = (Bag<T>) makeFullCollection();
125         if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
126             final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
127             assertEquals(bag.size(), bag2.size(), "Bag is the right size");
128             assertEquals(bag, bag2);
129         }
130     }
131 }