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.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.Comparator;
24  
25  import org.apache.commons.collections4.Predicate;
26  import org.apache.commons.collections4.SortedBag;
27  import org.apache.commons.collections4.functors.TruePredicate;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Extension of {@link AbstractSortedBagTest} for exercising the {@link PredicatedSortedBag}
32   * implementation.
33   */
34  public class PredicatedSortedBagTest<T> extends AbstractSortedBagTest<T> {
35  
36      private final SortedBag<T> nullBag = null;
37  
38      protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
39  
40      public PredicatedSortedBagTest() {
41          super(PredicatedSortedBagTest.class.getSimpleName());
42      }
43  
44      protected SortedBag<T> decorateBag(final SortedBag<T> bag, final Predicate<T> predicate) {
45          return PredicatedSortedBag.predicatedSortedBag(bag, predicate);
46      }
47  
48      @Override
49      public String getCompatibilityVersion() {
50          return "4";
51      }
52  
53      @Override
54      public SortedBag<T> makeObject() {
55          return decorateBag(new TreeBag<>(), truePredicate);
56      }
57  
58      protected SortedBag<T> makeTestBag() {
59          return decorateBag(new TreeBag<>(), stringPredicate());
60      }
61  
62      protected Predicate<T> stringPredicate() {
63          return String.class::isInstance;
64      }
65  
66      @Test
67      public void testDecorate() {
68          final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
69          ((PredicatedSortedBag<T>) bag).decorated();
70  
71          assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<>(), null));
72  
73          assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate()));
74      }
75  
76      @Test
77      @SuppressWarnings("unchecked")
78      public void testSortOrder() {
79          final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
80          final String one = "one";
81          final String two = "two";
82          final String three = "three";
83          bag.add((T) one);
84          bag.add((T) two);
85          bag.add((T) three);
86          assertEquals(bag.first(), one, "first element");
87          assertEquals(bag.last(), two, "last element");
88          final Comparator<? super T> c = bag.comparator();
89          assertNull(c, "natural order, so comparator should be null");
90      }
91  
92  //    public void testCreate() throws Exception {
93  //        Bag<T> bag = makeObject();
94  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedSortedBag.emptyCollection.version4.obj");
95  //        bag = makeFullCollection();
96  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedSortedBag.fullCollection.version4.obj");
97  //    }
98  
99  }