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.set;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.Arrays;
25  import java.util.Comparator;
26  import java.util.Set;
27  import java.util.SortedSet;
28  import java.util.TreeSet;
29  
30  import org.apache.commons.collections4.Predicate;
31  import org.apache.commons.collections4.functors.TruePredicate;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Extension of {@link AbstractSortedSetTest} for exercising the
36   * {@link PredicatedSortedSet} implementation.
37   */
38  public class PredicatedSortedSetTest<E> extends AbstractSortedSetTest<E> {
39  
40      protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
41  
42      protected Predicate<E> testPredicate =
43          o -> o instanceof String && ((String) o).startsWith("A");
44  
45      @Override
46      public String getCompatibilityVersion() {
47          return "4";
48      }
49  
50      @Override
51      public SortedSet<E> makeFullCollection() {
52          final TreeSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
53          return PredicatedSortedSet.predicatedSortedSet(set, truePredicate);
54      }
55  
56      @Override
57      public SortedSet<E> makeObject() {
58          return PredicatedSortedSet.predicatedSortedSet(new TreeSet<>(), truePredicate);
59      }
60  
61      protected PredicatedSortedSet<E> makeTestSet() {
62          return PredicatedSortedSet.predicatedSortedSet(new TreeSet<>(), testPredicate);
63      }
64  
65      @Test
66      public void testComparator() {
67          final SortedSet<E> set = makeTestSet();
68          final Comparator<? super E> c = set.comparator();
69          assertNull(c, "natural order, so comparator should be null");
70      }
71  
72      @Test
73      public void testGetSet() {
74          final PredicatedSortedSet<E> set = makeTestSet();
75          assertNotNull(set.decorated(), "returned set should not be null");
76      }
77  
78      @Test
79      @SuppressWarnings("unchecked")
80      public void testIllegalAdd() {
81          final SortedSet<E> set = makeTestSet();
82          final String testString = "B";
83          assertThrows(IllegalArgumentException.class, () -> set.add((E) testString),
84                  "Should fail string predicate.");
85          assertFalse(set.contains(testString), "Collection shouldn't contain illegal element");
86      }
87  
88      @Test
89      @SuppressWarnings("unchecked")
90      public void testIllegalAddAll() {
91          final SortedSet<E> set = makeTestSet();
92          final Set<E> elements = new TreeSet<>();
93          elements.add((E) "Aone");
94          elements.add((E) "Atwo");
95          elements.add((E) "Bthree");
96          elements.add((E) "Afour");
97          assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
98                  "Should fail string predicate.");
99          assertFalse(set.contains("Aone"), "Set shouldn't contain illegal element");
100         assertFalse(set.contains("Atwo"), "Set shouldn't contain illegal element");
101         assertFalse(set.contains("Bthree"), "Set shouldn't contain illegal element");
102         assertFalse(set.contains("Afour"), "Set shouldn't contain illegal element");
103     }
104 
105 //    public void testCreate() throws Exception {
106 //        resetEmpty();
107 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedSortedSet.emptyCollection.version4.obj");
108 //        resetFull();
109 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedSortedSet.fullCollection.version4.obj");
110 //    }
111 
112 }