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  
18  package org.apache.commons.collections4;
19  
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.function.Supplier;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  import org.apache.commons.collections4.list.TreeList;
30  import org.apache.commons.collections4.map.HashedMap;
31  import org.apache.commons.collections4.map.LRUMap;
32  import org.apache.commons.collections4.map.LinkedMap;
33  import org.apache.commons.collections4.map.ReferenceMap;
34  
35  import com.google.common.collect.testing.ListTestSuiteBuilder;
36  import com.google.common.collect.testing.MapTestSuiteBuilder;
37  import com.google.common.collect.testing.TestStringListGenerator;
38  import com.google.common.collect.testing.TestStringMapGenerator;
39  import com.google.common.collect.testing.features.CollectionFeature;
40  import com.google.common.collect.testing.features.CollectionSize;
41  import com.google.common.collect.testing.features.Feature;
42  import com.google.common.collect.testing.features.ListFeature;
43  import com.google.common.collect.testing.features.MapFeature;
44  
45  /**
46   * This test uses Google's Guava Testlib testing libraries to validate the
47   * contract of collection classes in Commons Collections. This was introduced
48   * after COLLECTIONS-802, where the issue reported was found with Testlib.
49   *
50   * @see <a href="https://github.com/google/guava/tree/master/guava-testlib">https://github.com/google/guava/tree/master/guava-testlib</a>
51   * @see <a href="https://issues.apache.org/jira/browse/COLLECTIONS-802">https://issues.apache.org/jira/browse/COLLECTIONS-802</a>
52   */
53  public final class GuavaTestlibTest extends TestCase {
54  
55      public static Test suite() {
56          final TestSuite test = new TestSuite();
57          // Map
58          test.addTest(suiteMap("HashedMap", HashedMap::new));
59          test.addTest(suiteMap("LinkedMap", LinkedMap::new));
60          test.addTest(suiteMap("LRUMap", LRUMap::new));
61          test.addTest(suiteMap("ReferenceMap", ReferenceMap::new));
62          // List
63          test.addTest(suiteList("TreeList", TreeList::new));
64          // TODO: In COLLECTIONS-811 we enabled the list tests for TreeList, but these other two types did not
65          //       pass the tests. Someone needs to confirm if it is a bug in the code, or we need to change the
66          //       test features.
67          // test.addTest(suiteList("GrowthList", GrowthList::new, CollectionFeature.SERIALIZABLE));
68          // test.addTest(suiteList("CursorableLinkedList", CursorableLinkedList::new, CollectionFeature.SERIALIZABLE));
69          return test;
70      }
71  
72      /**
73       * Programmatically create a JUnit (3, 4) Test Suite for Guava testlib tests with Lists.
74       * @param name name of the test
75       * @param factory factory to create new Lists
76       * @param features test features used in the tests
77       * @return a JUnit 3, 4 Test Suite
78       */
79      private static Test suiteList(final String name, final Supplier<List<String>> factory, final Feature<?>... features) {
80          final ListTestSuiteBuilder<String> suite = ListTestSuiteBuilder.using(new TestStringListGenerator() {
81              @Override
82              protected List<String> create(final String[] elements) {
83                  final List<String> list = factory.get();
84                  Collections.addAll(list, elements);
85                  return list;
86              }
87          })
88                  .named(name)
89                  .withFeatures(
90                          CollectionSize.ANY,
91                          ListFeature.GENERAL_PURPOSE,
92                          ListFeature.REMOVE_OPERATIONS,
93                          CollectionFeature.ALLOWS_NULL_VALUES,
94                          CollectionFeature.DESCENDING_VIEW,
95                          CollectionFeature.SUBSET_VIEW);
96          suite.withFeatures(features);
97          return suite.createTestSuite();
98      }
99  
100     /**
101      * Programmatically create a JUnit (3, 4) Test Suite for Guava testlib tests with Maps.
102      * @param name name of the test
103      * @param factory factory to create new Maps
104      * @return a JUnit 3, 4 Test Suite
105      */
106     private static Test suiteMap(final String name, final Supplier<Map<String, String>> factory) {
107         return MapTestSuiteBuilder.using(new TestStringMapGenerator() {
108             @Override
109             protected Map<String, String> create(final Map.Entry<String, String>[] entries) {
110                 final Map<String, String> map = factory.get();
111                 for (final Map.Entry<String, String> entry : entries) {
112                     map.put(entry.getKey(), entry.getValue());
113                 }
114                 return map;
115             }
116         })
117                 .named(name)
118                 .withFeatures(
119                         CollectionSize.ANY, MapFeature.GENERAL_PURPOSE,
120                         MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE)
121                 .createTestSuite();
122     }
123 }