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      @Override
33      public String getCompatibilityVersion() {
34          return "4";
35      }
36  
37      @Override
38      public SortedBag<T> makeObject() {
39          return new TreeBag<>();
40      }
41  
42      @SuppressWarnings("unchecked")
43      public SortedBag<T> setupBag() {
44          final SortedBag<T> bag = makeObject();
45          bag.add((T) "C");
46          bag.add((T) "A");
47          bag.add((T) "B");
48          bag.add((T) "D");
49          return bag;
50      }
51  
52      @Test
53      public void testCollections265() {
54          final Bag<Object> bag = new TreeBag<>();
55  
56          assertThrows(IllegalArgumentException.class, () -> bag.add(new Object()));
57      }
58  
59      @Test
60      public void testCollections555() {
61          final Bag<Object> bag = new TreeBag<>();
62  
63          assertThrows(NullPointerException.class, () -> bag.add(null));
64  
65          final Bag<String> bag2 = new TreeBag<>(String::compareTo);
66          // jdk bug: adding null to an empty TreeMap works
67          // thus ensure that the bag is not empty before adding null
68          bag2.add("a");
69  
70          assertThrows(NullPointerException.class, () -> bag2.add(null));
71      }
72  
73      @Test
74      public void testOrdering() {
75          final Bag<T> bag = setupBag();
76          assertEquals("A", bag.toArray()[0], "Should get elements in correct order");
77          assertEquals("B", bag.toArray()[1], "Should get elements in correct order");
78          assertEquals("C", bag.toArray()[2], "Should get elements in correct order");
79          assertEquals("A", ((SortedBag<T>) bag).first(), "Should get first key");
80          assertEquals("D", ((SortedBag<T>) bag).last(), "Should get last key");
81      }
82  
83  //    public void testCreate() throws Exception {
84  //        Bag<T> bag = makeObject();
85  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.emptyCollection.version4.obj");
86  //        bag = makeFullCollection();
87  //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.fullCollection.version4.obj");
88  //    }
89  
90  }