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  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  
27  import org.apache.commons.collections4.Bag;
28  import org.apache.commons.collections4.SortedBag;
29  import org.apache.commons.collections4.collection.AbstractCollectionTest;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Test class for {@link CollectionSortedBag}.
34   * <p>
35   * Note: This test is mainly for serialization support, the CollectionSortedBag decorator
36   * is extensively used and tested in AbstractSortedBagTest.
37   */
38  public class CollectionSortedBagTest<T> extends AbstractCollectionTest<T> {
39  
40      @Override
41      public String getCompatibilityVersion() {
42          return "4";
43      }
44  
45      /**
46       * Override to return comparable objects.
47       */
48      @Override
49      @SuppressWarnings("unchecked")
50      public T[] getFullNonNullElements() {
51          final Object[] elements = new Object[30];
52  
53          for (int i = 0; i < 30; i++) {
54              elements[i] = Integer.valueOf(i + i + 1);
55          }
56          return (T[]) elements;
57      }
58  
59      /**
60       * Override to return comparable objects.
61       */
62      @Override
63      @SuppressWarnings("unchecked")
64      public T[] getOtherNonNullElements() {
65          final Object[] elements = new Object[30];
66          for (int i = 0; i < 30; i++) {
67              elements[i] = Integer.valueOf(i + i + 2);
68          }
69          return (T[]) elements;
70      }
71  
72      /**
73       * Overridden because SortedBags don't allow null elements (normally).
74       * @return false
75       */
76      @Override
77      public boolean isNullSupported() {
78          return false;
79      }
80  
81      /**
82       * Returns an empty List for use in modification testing.
83       *
84       * @return a confirmed empty collection
85       */
86      @Override
87      public Collection<T> makeConfirmedCollection() {
88          return new ArrayList<>();
89      }
90  
91      /**
92       * Returns a full Set for use in modification testing.
93       *
94       * @return a confirmed full collection
95       */
96      @Override
97      public Collection<T> makeConfirmedFullCollection() {
98          final Collection<T> set = makeConfirmedCollection();
99          set.addAll(Arrays.asList(getFullElements()));
100         return set;
101     }
102 
103     @Override
104     public Bag<T> makeObject() {
105         return CollectionSortedBag.collectionSortedBag(new TreeBag<>());
106     }
107 
108 //    public void testCreate() throws Exception {
109 //        resetEmpty();
110 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionSortedBag.emptyCollection.version4.obj");
111 //        resetFull();
112 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionSortedBag.fullCollection.version4.obj");
113 //    }
114 
115     /**
116      * Compare the current serialized form of the Bag
117      * against the canonical version in SCM.
118      */
119     @Test
120     public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
121         // test to make sure the canonical form has been preserved
122         final Bag<T> bag = makeObject();
123         if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
124             final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
125             assertEquals(0, bag2.size(), "Bag is empty");
126             assertEquals(bag, bag2);
127         }
128     }
129 
130     /**
131      * Compare the current serialized form of the Bag
132      * against the canonical version in SCM.
133      */
134     @Test
135     public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
136         // test to make sure the canonical form has been preserved
137         final SortedBag<T> bag = (SortedBag<T>) makeFullCollection();
138         if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
139             final SortedBag<?> bag2 = (SortedBag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
140             assertEquals(bag.size(), bag2.size(), "Bag is the right size");
141             assertEquals(bag, bag2);
142         }
143     }
144 
145 }