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