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.util.Iterator;
22  
23  import org.apache.commons.collections4.SortedBag;
24  
25  /**
26   * Abstract test class for
27   * {@link org.apache.commons.collections4.SortedBag SortedBag}
28   * methods and contracts.
29   */
30  public abstract class AbstractSortedBagTest<T> extends AbstractBagTest<T> {
31  
32      public AbstractSortedBagTest(final String testName) {
33          super(testName);
34      }
35  
36      /**
37       * Returns the {@link #collection} field cast to a {@link SortedBag}.
38       *
39       * @return the collection field as a SortedBag
40       */
41      @Override
42      public SortedBag<T> getCollection() {
43          return (SortedBag<T>) super.getCollection();
44      }
45  
46      /**
47       * Override to return comparable objects.
48       */
49      @Override
50      @SuppressWarnings("unchecked")
51      public T[] getFullNonNullElements() {
52          final Object[] elements = new Object[30];
53  
54          for (int i = 0; i < 30; i++) {
55              elements[i] = Integer.valueOf(i + i + 1);
56          }
57          return (T[]) elements;
58      }
59  
60      /**
61       * Override to return comparable objects.
62       */
63      @Override
64      @SuppressWarnings("unchecked")
65      public T[] getOtherNonNullElements() {
66          final Object[] elements = new Object[30];
67          for (int i = 0; i < 30; i++) {
68              elements[i] = Integer.valueOf(i + i + 2);
69          }
70          return (T[]) elements;
71      }
72  
73      /**
74       * Overridden because SortedBags don't allow null elements (normally).
75       * @return false
76       */
77      @Override
78      public boolean isNullSupported() {
79          return false;
80      }
81  
82      /**
83       * Returns an empty {@link TreeBag} for use in modification testing.
84       *
85       * @return a confirmed empty collection
86       */
87      @Override
88      public SortedBag<T> makeConfirmedCollection() {
89          return new TreeBag<>();
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public SortedBag<T> makeFullCollection() {
97          return (SortedBag<T>) super.makeFullCollection();
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public abstract SortedBag<T> makeObject();
105 
106     @Override
107     public void resetEmpty() {
108         this.setCollection(CollectionSortedBag.collectionSortedBag(makeObject()));
109         this.setConfirmed(makeConfirmedCollection());
110     }
111 
112     @Override
113     public void resetFull() {
114         this.setCollection(CollectionSortedBag.collectionSortedBag(makeFullCollection()));
115         this.setConfirmed(makeConfirmedFullCollection());
116     }
117 
118     /**
119      * Verification extension, will check the order of elements,
120      * the sets should already be verified equal.
121      */
122     @Override
123     public void verify() {
124         super.verify();
125 
126         // Check that iterator returns elements in order and first() and last()
127         // are consistent
128         final Iterator<T> collIter = getCollection().iterator();
129         final Iterator<T> confIter = getConfirmed().iterator();
130         T first = null;
131         T last = null;
132         while (collIter.hasNext()) {
133             if (first == null) {
134                 first = collIter.next();
135                 last = first;
136             } else {
137                 last = collIter.next();
138             }
139             assertEquals(last, confIter.next(), "Element appears to be out of order.");
140         }
141         if (!getCollection().isEmpty()) {
142             assertEquals(first, getCollection().first(),
143                 "Incorrect element returned by first().");
144             assertEquals(last, getCollection().last(),
145                 "Incorrect element returned by last().");
146         }
147     }
148 
149     // TODO: Add the SortedBag tests!
150 }