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      protected SortedBag<T> decorateBag(final SortedBag<T> bag, final Predicate<T> predicate) {
41          return PredicatedSortedBag.predicatedSortedBag(bag, predicate);
42      }
43  
44      @Override
45      public String getCompatibilityVersion() {
46          return "4";
47      }
48  
49      @Override
50      public SortedBag<T> makeObject() {
51          return decorateBag(new TreeBag<>(), truePredicate);
52      }
53  
54      protected SortedBag<T> makeTestBag() {
55          return decorateBag(new TreeBag<>(), stringPredicate());
56      }
57  
58      protected Predicate<T> stringPredicate() {
59          return String.class::isInstance;
60      }
61  
62      @Test
63      public void testDecorate() {
64          final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
65          ((PredicatedSortedBag<T>) bag).decorated();
66  
67          assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<>(), null));
68  
69          assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate()));
70      }
71  
72      @Test
73      @SuppressWarnings("unchecked")
74      public void testSortOrder() {
75          final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
76          final String one = "one";
77          final String two = "two";
78          final String three = "three";
79          bag.add((T) one);
80          bag.add((T) two);
81          bag.add((T) three);
82          assertEquals(bag.first(), one, "first element");
83          assertEquals(bag.last(), two, "last element");
84          final Comparator<? super T> c = bag.comparator();
85          assertNull(c, "natural order, so comparator should be null");
86      }
87  
88  //    public void testCreate() throws Exception {
89  //        Bag<T> bag = makeObject();
90  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedSortedBag.emptyCollection.version4.obj");
91  //        bag = makeFullCollection();
92  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedSortedBag.fullCollection.version4.obj");
93  //    }
94  
95  }