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