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.bag;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.apache.commons.collections4.Bag;
23  import org.apache.commons.collections4.SortedBag;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Extension of {@link AbstractBagTest} for exercising the {@link TreeBag}
28   * implementation.
29   */
30  public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
31  
32      public TreeBagTest() {
33          super(TreeBagTest.class.getSimpleName());
34      }
35  
36      @Override
37      public String getCompatibilityVersion() {
38          return "4";
39      }
40  
41      @Override
42      public SortedBag<T> makeObject() {
43          return new TreeBag<>();
44      }
45  
46      @SuppressWarnings("unchecked")
47      public SortedBag<T> setupBag() {
48          final SortedBag<T> bag = makeObject();
49          bag.add((T) "C");
50          bag.add((T) "A");
51          bag.add((T) "B");
52          bag.add((T) "D");
53          return bag;
54      }
55  
56      @Test
57      public void testCollections265() {
58          final Bag<Object> bag = new TreeBag<>();
59  
60          assertThrows(IllegalArgumentException.class, () -> bag.add(new Object()));
61      }
62  
63      @Test
64      public void testCollections555() {
65          final Bag<Object> bag = new TreeBag<>();
66  
67          assertThrows(NullPointerException.class, () -> bag.add(null));
68  
69          final Bag<String> bag2 = new TreeBag<>(String::compareTo);
70          // jdk bug: adding null to an empty TreeMap works
71          // thus ensure that the bag is not empty before adding null
72          bag2.add("a");
73  
74          assertThrows(NullPointerException.class, () -> bag2.add(null));
75      }
76  
77      @Test
78      public void testOrdering() {
79          final Bag<T> bag = setupBag();
80          assertEquals("A", bag.toArray()[0], "Should get elements in correct order");
81          assertEquals("B", bag.toArray()[1], "Should get elements in correct order");
82          assertEquals("C", bag.toArray()[2], "Should get elements in correct order");
83          assertEquals("A", ((SortedBag<T>) bag).first(), "Should get first key");
84          assertEquals("D", ((SortedBag<T>) bag).last(), "Should get last key");
85      }
86  
87  //    public void testCreate() throws Exception {
88  //        Bag<T> bag = makeObject();
89  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.emptyCollection.version4.obj");
90  //        bag = makeFullCollection();
91  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.fullCollection.version4.obj");
92  //    }
93  
94  }