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