1 /*
   2  * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.util.Collections;
  27 import java.util.LinkedList;
  28 import java.util.List;
  29 import java.util.Set;
  30 
  31 import org.testng.annotations.Test;
  32 
  33 import static org.testng.Assert.assertTrue;
  34 import static org.testng.Assert.fail;
  35 
  36 import java.util.function.Predicate;
  37 
  38 /**
  39  * @test
  40  * @library testlibrary
  41  * @build CollectionAsserts CollectionSupplier
  42  * @run testng CollectionExtensionMethodsTest
  43  * @summary Unit tests for extension methods on Collection
  44  */
  45 public class CollectionExtensionMethodsTest {
  46 
  47     private static final Predicate<Integer> P_EVEN = x -> 0 == x % 2;
  48     private static final Predicate<Integer> P_ODD = x -> 0 != x % 2;
  49 
  50     private static final String[] SET_CLASSES = {
  51         "java.util.HashSet",
  52         "java.util.LinkedHashSet",
  53         "java.util.TreeSet"
  54     };
  55 
  56     @Test
  57     public void testUnmodifiable() throws Exception {
  58         final CollectionSupplier supplier = new CollectionSupplier(SET_CLASSES, 10);
  59         for (final CollectionSupplier.TestCase test : supplier.get()) {
  60             final Set<Integer> set = ((Set<Integer>) test.collection);
  61             final Set<Integer> unmodifiable = Collections.unmodifiableSet(set);
  62             // forEach does not modify, it should not throw UnsupportedOperationException
  63             unmodifiable.forEach((x) -> {});
  64             try {
  65                 unmodifiable.removeAll(P_EVEN);
  66                 fail("removeAll on unmodifiable set did not throw exception");
  67             } catch (UnsupportedOperationException ignore) {}
  68         }
  69     }
  70 
  71     @Test
  72     public void testForNullPointerException() throws Exception {
  73         final CollectionSupplier supplier = new CollectionSupplier(SET_CLASSES, 10);
  74         for (final CollectionSupplier.TestCase test : supplier.get()) {
  75             final Set<Integer> set = ((Set<Integer>) test.collection);
  76             try {
  77                 set.forEach(null);
  78                 fail("forEach with null Block did not throw NPE");
  79             } catch (NullPointerException nx) {}
  80             try {
  81                 set.removeAll((Predicate<? super Integer>) null);
  82                 fail("removeAll with null Predicate did not throw NPE");
  83             } catch (NullPointerException nx) {}
  84         }
  85     }
  86 
  87     @Test
  88     public void testForEach() throws Exception {
  89         final CollectionSupplier supplier = new CollectionSupplier(SET_CLASSES, 100);
  90         for (final CollectionSupplier.TestCase test : supplier.get()) {
  91             final Set<Integer> original = ((Set<Integer>) test.original);
  92             final Set<Integer> set = ((Set<Integer>) test.collection);
  93             final List<Integer> actual = new LinkedList<>();
  94             set.forEach(actual::add);
  95             if (test.className.equals("java.util.HashSet")) {
  96                 CollectionAsserts.assertContentsUnordered(actual, set);
  97                 CollectionAsserts.assertContentsUnordered(actual, original);
  98             } else {
  99                 CollectionAsserts.assertContents(actual, set);
 100                 CollectionAsserts.assertContents(actual, original);
 101             }
 102         }
 103     }
 104 
 105     @Test
 106     public void testRemoveAll() throws Exception {
 107         final CollectionSupplier supplier = new CollectionSupplier(SET_CLASSES, 100);
 108         for (final CollectionSupplier.TestCase test : supplier.get()) {
 109             final Set<Integer> original = ((Set<Integer>) test.original);
 110             final Set<Integer> set = ((Set<Integer>) test.collection);
 111             set.removeAll(P_EVEN);
 112             for (final int i : set) {
 113                 assertTrue((i % 2) == 1);
 114             }
 115             for (final int i : original) {
 116                 if (i % 2 != 0) {
 117                     assertTrue(set.contains(i));
 118                 }
 119             }
 120             set.removeAll(P_ODD);
 121             assertTrue(set.isEmpty());
 122         }
 123     }
 124 }