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.collection;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.Queue;
27  import java.util.Set;
28  
29  import org.apache.commons.collections4.Bag;
30  import org.apache.commons.collections4.Predicate;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests the PredicatedCollection.Builder class.
35   */
36  public class PredicatedCollectionBuilderTest {
37  
38      private static final class OddPredicate implements Predicate<Integer> {
39          @Override
40          public boolean evaluate(final Integer value) {
41              return value % 2 == 1;
42          }
43      }
44  
45      private void checkPredicatedCollection1(final Collection<String> collection) {
46          assertEquals(1, collection.size());
47  
48          collection.add("test2");
49          assertEquals(2, collection.size());
50  
51          assertThrows(IllegalArgumentException.class, () -> collection.add(null), "Expecting IllegalArgumentException for failing predicate!");
52  
53      }
54  
55      private void checkPredicatedCollection2(final Collection<Integer> collection) {
56          assertEquals(2, collection.size());
57          assertThrows(IllegalArgumentException.class, () -> collection.add(4), "Expecting IllegalArgumentException for failing predicate!");
58          assertEquals(2, collection.size());
59  
60          collection.add(5);
61          assertEquals(3, collection.size());
62      }
63  
64      /**
65       * Verify that only items that pass the Predicate end up in the buffer.
66       */
67      @Test
68      public void testAddAllPass() {
69          final PredicatedCollection.Builder<String> builder = PredicatedCollection.notNullBuilder();
70          builder.addAll(Arrays.asList("test1", null, "test2"));
71          assertEquals(builder.createPredicatedList().size(), 2);
72      }
73  
74      /**
75       * Verify that failing the Predicate means NOT ending up in the buffer.
76       */
77      @Test
78      public void testAddFail() {
79          final PredicatedCollection.Builder<String> builder = PredicatedCollection.notNullBuilder();
80          builder.add((String) null);
81          assertTrue(builder.createPredicatedList().isEmpty());
82  
83          assertEquals(1, builder.rejectedElements().size());
84      }
85  
86      /**
87       * Verify that passing the Predicate means ending up in the buffer.
88       */
89      @Test
90      public void testAddPass() {
91          final PredicatedCollection.Builder<String> builder = PredicatedCollection.notNullBuilder();
92          builder.add("test");
93          assertEquals(builder.createPredicatedList().size(), 1);
94      }
95  
96      @Test
97      public void testCreatePredicatedCollectionWithNotNullPredicate() {
98          final PredicatedCollection.Builder<String> builder = PredicatedCollection.notNullBuilder();
99          builder.add("test1");
100         builder.add((String) null);
101 
102         final List<String> predicatedList = builder.createPredicatedList();
103         checkPredicatedCollection1(predicatedList);
104 
105         final Set<String> predicatedSet = builder.createPredicatedSet();
106         checkPredicatedCollection1(predicatedSet);
107 
108         final Bag<String> predicatedBag = builder.createPredicatedBag();
109         checkPredicatedCollection1(predicatedBag);
110 
111         final Queue<String> predicatedQueue = builder.createPredicatedQueue();
112         checkPredicatedCollection1(predicatedQueue);
113     }
114 
115     @Test
116     public void testCreatePredicatedCollectionWithPredicate() {
117         final OddPredicate p = new OddPredicate();
118         final PredicatedCollection.Builder<Integer> builder = PredicatedCollection.builder(p);
119 
120         builder.add(1);
121         builder.add(2);
122         builder.add(3);
123 
124         final List<Integer> predicatedList = builder.createPredicatedList();
125         checkPredicatedCollection2(predicatedList);
126 
127         final Set<Integer> predicatedSet = builder.createPredicatedSet();
128         checkPredicatedCollection2(predicatedSet);
129 
130         final Bag<Integer> predicatedBag = builder.createPredicatedBag();
131         checkPredicatedCollection2(predicatedBag);
132 
133         final Queue<Integer> predicatedQueue = builder.createPredicatedQueue();
134         checkPredicatedCollection2(predicatedQueue);
135     }
136 }