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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.LinkedList;
26  import java.util.Queue;
27  
28  import org.apache.commons.collections4.Transformer;
29  import org.apache.commons.collections4.collection.AbstractCollectionTest;
30  import org.apache.commons.collections4.collection.TransformedCollectionTest;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Extension of {@link AbstractCollectionTest} for exercising the
35   * {@link TransformedQueue} implementation.
36   */
37  public class TransformedQueueTest<E> extends AbstractQueueTest<E> {
38  
39      public TransformedQueueTest() {
40          super(TransformedQueueTest.class.getSimpleName());
41      }
42  
43      @Override
44      public String getCompatibilityVersion() {
45          return "4";
46      }
47  
48      @Override
49      public Queue<E> makeConfirmedCollection() {
50          return new LinkedList<>();
51      }
52  
53      @Override
54      public Queue<E> makeConfirmedFullCollection() {
55          return new LinkedList<>(Arrays.asList(getFullElements()));
56      }
57  
58      @Override
59      @SuppressWarnings("unchecked")
60      public Queue<E> makeFullCollection() {
61          final Queue<E> list = new LinkedList<>(Arrays.asList(getFullElements()));
62          return TransformedQueue.transformingQueue(list, (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
63      }
64  
65      @Override
66      @SuppressWarnings("unchecked")
67      public Queue<E> makeObject() {
68          return TransformedQueue.transformingQueue(new LinkedList<>(),
69                  (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
70      }
71  
72      @Test
73      public void testTransformedQueue() {
74          final Queue<Object> queue = TransformedQueue.transformingQueue(new LinkedList<>(),
75                  TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
76          assertEquals(0, queue.size());
77          final Object[] elements = { "1", "3", "5", "7", "2", "4", "6" };
78          for (int i = 0; i < elements.length; i++) {
79              queue.add(elements[i]);
80              assertEquals(i + 1, queue.size());
81              assertTrue(queue.contains(Integer.valueOf((String) elements[i])));
82              assertFalse(queue.contains(elements[i]));
83          }
84  
85          assertFalse(queue.remove(elements[0]));
86          assertTrue(queue.remove(Integer.valueOf((String) elements[0])));
87  
88      }
89  
90      @SuppressWarnings({ "rawtypes", "unchecked" })
91      @Test
92      public void testTransformedQueue_decorateTransform() {
93          final Queue originalQueue = new LinkedList();
94          final Object[] elements = {"1", "3", "5", "7", "2", "4", "6"};
95          Collections.addAll(originalQueue, elements);
96          final Queue<?> queue = TransformedQueue.transformedQueue(originalQueue,
97                  TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
98          assertEquals(elements.length, queue.size());
99          for (final Object el : elements) {
100             assertTrue(queue.contains(Integer.valueOf((String) el)));
101             assertFalse(queue.contains(el));
102         }
103 
104         assertFalse(queue.remove(elements[0]));
105         assertTrue(queue.remove(Integer.valueOf((String) elements[0])));
106     }
107 
108 //  public void testCreate() throws Exception {
109 //      resetEmpty();
110 //      writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedQueue.emptyCollection.version4.obj");
111 //      resetFull();
112 //      writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedQueue.fullCollection.version4.obj");
113 //  }
114 
115 }