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.NavigableSet;
27  import java.util.Set;
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 AbstractNavigableSetTest} for exercising the
36   * {@link PredicatedNavigableSet} implementation.
37   */
38  public class PredicatedNavigableSetTest<E> extends AbstractNavigableSetTest<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      public PredicatedNavigableSetTest() {
46          super(PredicatedNavigableSetTest.class.getSimpleName());
47      }
48  
49      @Override
50      public String getCompatibilityVersion() {
51          return "4.1";
52      }
53  
54      @Override
55      public NavigableSet<E> makeFullCollection() {
56          final TreeSet<E> set = new TreeSet<>(Arrays.asList(getFullElements()));
57          return PredicatedNavigableSet.predicatedNavigableSet(set, truePredicate);
58      }
59  
60      @Override
61      public NavigableSet<E> makeObject() {
62          return PredicatedNavigableSet.predicatedNavigableSet(new TreeSet<>(), truePredicate);
63      }
64  
65      protected PredicatedNavigableSet<E> makeTestSet() {
66          return PredicatedNavigableSet.predicatedNavigableSet(new TreeSet<>(), testPredicate);
67      }
68  
69      @Test
70      public void testComparator() {
71          final NavigableSet<E> set = makeTestSet();
72          final Comparator<? super E> c = set.comparator();
73          assertNull(c, "natural order, so comparator should be null");
74      }
75  
76      @Test
77      public void testGetSet() {
78          final PredicatedNavigableSet<E> set = makeTestSet();
79          assertNotNull(set.decorated(), "returned set should not be null");
80      }
81  
82      @Test
83      @SuppressWarnings("unchecked")
84      public void testIllegalAdd() {
85          final NavigableSet<E> set = makeTestSet();
86          final String testString = "B";
87          assertThrows(IllegalArgumentException.class, () -> set.add((E) testString),
88                  "Should fail string predicate.");
89          assertFalse(set.contains(testString), "Collection shouldn't contain illegal element");
90      }
91  
92      @Test
93      @SuppressWarnings("unchecked")
94      public void testIllegalAddAll() {
95          final NavigableSet<E> set = makeTestSet();
96          final Set<E> elements = new TreeSet<>();
97          elements.add((E) "Aone");
98          elements.add((E) "Atwo");
99          elements.add((E) "Bthree");
100         elements.add((E) "Afour");
101         assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
102                 "Should fail string predicate.");
103         assertFalse(set.contains("Aone"), "Set shouldn't contain illegal element");
104         assertFalse(set.contains("Atwo"), "Set shouldn't contain illegal element");
105         assertFalse(set.contains("Bthree"), "Set shouldn't contain illegal element");
106         assertFalse(set.contains("Afour"), "Set shouldn't contain illegal element");
107     }
108 
109 //    public void testCreate() throws Exception {
110 //        resetEmpty();
111 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedNavigableSet.emptyCollection.version4.1.obj");
112 //        resetFull();
113 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedNavigableSet.fullCollection.version4.1.obj");
114 //    }
115 
116 }