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.queue;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.LinkedList;
25  import java.util.Queue;
26  
27  import org.apache.commons.collections4.Predicate;
28  import org.apache.commons.collections4.collection.PredicatedCollectionTest;
29  import org.apache.commons.collections4.functors.TruePredicate;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link PredicatedCollectionTest} for exercising the
34   * {@link PredicatedQueue} implementation.
35   */
36  public class PredicatedQueueTest<E> extends AbstractQueueTest<E> {
37  
38      protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
39  
40      protected Predicate<E> testPredicate = String.class::isInstance;
41  
42      protected Queue<E> decorateCollection(final Queue<E> queue, final Predicate<E> predicate) {
43          return PredicatedQueue.predicatedQueue(queue, predicate);
44      }
45  
46      @Override
47      public String getCompatibilityVersion() {
48          return "4";
49      }
50  
51      @Override
52      public Collection<E> makeConfirmedCollection() {
53          return new LinkedList<>();
54      }
55  
56      @Override
57      public Collection<E> makeConfirmedFullCollection() {
58          return new LinkedList<>(Arrays.asList(getFullElements()));
59      }
60  
61      @Override
62      public Queue<E> makeFullCollection() {
63          final Queue<E> queue = new LinkedList<>(Arrays.asList(getFullElements()));
64          return decorateCollection(queue, truePredicate);
65      }
66  
67      @Override
68      public Queue<E> makeObject() {
69          return decorateCollection(new LinkedList<>(), truePredicate);
70      }
71  
72      public Queue<E> makeTestQueue() {
73          return decorateCollection(new LinkedList<>(), testPredicate);
74      }
75  
76      @Test
77      @SuppressWarnings("unchecked")
78      public void testGet() {
79          final Queue<E> queue = makeTestQueue();
80  
81          assertNull(queue.peek());
82  
83          queue.add((E) "one");
84          queue.add((E) "two");
85          queue.add((E) "three");
86          assertEquals("one", queue.peek(), "Queue get");
87      }
88  
89      @Test
90      @SuppressWarnings("unchecked")
91      public void testRemove() {
92          final Queue<E> queue = makeTestQueue();
93          queue.add((E) "one");
94          assertEquals("one", queue.poll(), "Queue get");
95          assertNull(queue.peek());
96      }
97  
98  //    public void testCreate() throws Exception {
99  //        resetEmpty();
100 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedQueue.emptyCollection.version4.obj");
101 //        resetFull();
102 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedQueue.fullCollection.version4.obj");
103 //    }
104 
105 }