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.assertSame;
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.LinkedList;
26  import java.util.Queue;
27  
28  import org.apache.commons.collections4.Unmodifiable;
29  import org.apache.commons.collections4.collection.AbstractCollectionTest;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link AbstractCollectionTest} for exercising the
34   * {@link UnmodifiableQueue} implementation.
35   */
36  public class UnmodifiableQueueTest<E> extends AbstractQueueTest<E> {
37  
38      @Override
39      public Queue<E> getCollection() {
40          return super.getCollection();
41      }
42  
43      @Override
44      public String getCompatibilityVersion() {
45          return "4";
46      }
47  
48      @Override
49      public boolean isAddSupported() {
50          return false;
51      }
52  
53      @Override
54      public boolean isNullSupported() {
55          return false;
56      }
57  
58      @Override
59      public boolean isRemoveSupported() {
60          return false;
61      }
62  
63      @Override
64      public Collection<E> makeConfirmedCollection() {
65          return new LinkedList<>();
66      }
67  
68      @Override
69      public Collection<E> makeConfirmedFullCollection() {
70          return new LinkedList<>(Arrays.asList(getFullElements()));
71      }
72  
73      @Override
74      public Queue<E> makeFullCollection() {
75          final Queue<E> queue = new LinkedList<>(Arrays.asList(getFullElements()));
76          return UnmodifiableQueue.unmodifiableQueue(queue);
77      }
78  
79      @Override
80      public Queue<E> makeObject() {
81          return UnmodifiableQueue.unmodifiableQueue(new LinkedList<>());
82      }
83  
84      @Test
85      public void testDecorateFactory() {
86          final Queue<E> queue = makeFullCollection();
87          assertSame(queue, UnmodifiableQueue.unmodifiableQueue(queue));
88  
89          assertThrows(NullPointerException.class, () -> UnmodifiableQueue.unmodifiableQueue(null));
90      }
91  
92      @Test
93      public void testOffer() {
94          final Queue<E> queue = makeFullCollection();
95          final E e = null;
96          assertThrows(UnsupportedOperationException.class, () -> queue.offer(e));
97      }
98  
99      @Test
100     public void testPoll() {
101         final Queue<E> queue = makeFullCollection();
102         assertThrows(UnsupportedOperationException.class, () -> queue.poll());
103     }
104 
105     @Test
106     @Override
107     public void testQueueRemove() {
108         resetEmpty();
109         assertThrows(UnsupportedOperationException.class, () -> getCollection().remove());
110     }
111 
112     @Test
113     public void testUnmodifiable() {
114         assertTrue(makeObject() instanceof Unmodifiable);
115         assertTrue(makeFullCollection() instanceof Unmodifiable);
116     }
117 
118 //    public void testCreate() throws Exception {
119 //        resetEmpty();
120 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableQueue.emptyCollection.version4.obj");
121 //        resetFull();
122 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableQueue.fullCollection.version4.obj");
123 //    }
124 
125 }