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