1 /* 2 * Copyright (c) 2018, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8050818 27 * @run testng PredicateNotTest 28 */ 29 package test.java.util.function; 30 31 // Android-added: workaround for d8 backports. 32 import java.lang.invoke.MethodHandle; 33 import java.lang.invoke.MethodHandles; 34 import java.lang.invoke.MethodType; 35 36 import java.util.List; 37 import java.util.function.Predicate; 38 import org.testng.annotations.Test; 39 40 import static java.util.stream.Collectors.joining; 41 import static org.testng.Assert.assertEquals; 42 import static org.testng.Assert.fail; 43 44 public class PredicateNotTest { 45 // BEGIN Android-added 46 // MethodHandle for invoking Predicate.not() to prevent d8 inserting it's backported 47 // `Predicate.not()` method (b/191859202, OpenJDK 11) and masking test coverage results. 48 static final MethodHandle NOT = initializeNot(); 49 initializeNot()50 private static MethodHandle initializeNot() 51 { 52 try { 53 MethodType notType = MethodType.methodType(Predicate.class, Predicate.class); 54 return MethodHandles.lookup().findStatic(Predicate.class, "not", notType); 55 } catch (Throwable t) { 56 throw new RuntimeException(t); 57 } 58 } 59 not(Predicate<? super T> target)60 static <T> Predicate<T> not(Predicate<? super T> target) { 61 try { 62 return (Predicate<T>) NOT.invoke(target); 63 } catch (Throwable t) { 64 throw new RuntimeException(t); 65 } 66 } 67 // END Android-added 68 69 static class IsEmptyPredicate implements Predicate<String> { 70 @Override test(String s)71 public boolean test(String s) { 72 return s.isEmpty(); 73 } 74 } 75 76 @Test test()77 public void test() { 78 List<String> test = List.of( 79 "A non-empty line", 80 "", 81 "A non-empty line", 82 "", 83 "A non-empty line", 84 "", 85 "A non-empty line", 86 "" 87 ); 88 String expected = "A non-empty line\nA non-empty line\nA non-empty line\nA non-empty line"; 89 90 assertEquals(test.stream().filter(not(String::isEmpty)).collect(joining("\n")), expected); 91 assertEquals(test.stream().filter(not(s -> s.isEmpty())).collect(joining("\n")), expected); 92 assertEquals(test.stream().filter(not(new IsEmptyPredicate())).collect(joining("\n")), expected); 93 assertEquals(test.stream().filter(not(not(not(String::isEmpty)))).collect(joining("\n")), expected); 94 assertEquals(test.stream().filter(not(not(not(s -> s.isEmpty())))).collect(joining("\n")), expected); 95 assertEquals(test.stream().filter(not(not(not(new IsEmptyPredicate())))).collect(joining("\n")), expected); 96 } 97 } 98 99