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.collection;
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.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collection;
26  
27  import org.apache.commons.collections4.BoundedCollection;
28  import org.apache.commons.collections4.Unmodifiable;
29  import org.apache.commons.collections4.list.FixedSizeList;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link AbstractCollectionTest} for exercising the
34   * {@link UnmodifiableBoundedCollection} implementation.
35   */
36  public class UnmodifiableBoundedCollectionTest<E> extends AbstractCollectionTest<E> {
37  
38      public UnmodifiableBoundedCollectionTest() {
39          super(UnmodifiableBoundedCollectionTest.class.getSimpleName());
40      }
41  
42      @Override
43      public String getCompatibilityVersion() {
44          return "4";
45      }
46  
47      @Override
48      public boolean isAddSupported() {
49          return false;
50      }
51  
52      @Override
53      public boolean isRemoveSupported() {
54          return false;
55      }
56  
57      @Override
58      public Collection<E> makeConfirmedCollection() {
59          return new ArrayList<>();
60      }
61  
62      @Override
63      public Collection<E> makeConfirmedFullCollection() {
64          return new ArrayList<>(Arrays.asList(getFullElements()));
65      }
66  
67      @Override
68      public BoundedCollection<E> makeFullCollection() {
69          final E[] allElements = getFullElements();
70          final BoundedCollection<E> coll = FixedSizeList.<E>fixedSizeList(new ArrayList<>(Arrays.asList(allElements)));
71          return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll);
72      }
73  
74      @Override
75      public Collection<E> makeObject() {
76          final BoundedCollection<E> coll = FixedSizeList.<E>fixedSizeList(new ArrayList<>());
77          return UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll);
78      }
79  
80      @Override
81      protected boolean skipSerializedCanonicalTests() {
82          return true;
83      }
84  
85      @Test
86      public void testDecorateFactory() {
87          final BoundedCollection<E> coll = makeFullCollection();
88          assertSame(coll, UnmodifiableBoundedCollection.unmodifiableBoundedCollection(coll));
89  
90          assertThrows(NullPointerException.class, () -> UnmodifiableBoundedCollection.unmodifiableBoundedCollection(null));
91      }
92  
93      @Test
94      public void testUnmodifiable() {
95          assertTrue(makeObject() instanceof Unmodifiable);
96          assertTrue(makeFullCollection() instanceof Unmodifiable);
97      }
98  
99  }