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