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 package test.java.lang.String; 24 25 // Android-added: support for wrapper to avoid d8 backporting of String.isBlank (b/191859202). 26 import java.lang.invoke.MethodHandle; 27 import java.lang.invoke.MethodHandles; 28 import java.lang.invoke.MethodType; 29 30 import java.util.stream.IntStream; 31 32 import org.testng.annotations.Test; 33 import static org.testng.Assert.assertEquals; 34 35 /** 36 * @test 37 * @summary Basic isBlank functionality 38 * @bug 8200436 39 * @run main/othervm IsBlank 40 */ 41 42 public class IsBlank { 43 /* 44 * Test with strings 45 */ 46 @Test testIsBlank()47 public void testIsBlank() { 48 test("", true); 49 test(" ", true); 50 test(" \t", true); 51 test(" \u1680", true); 52 test(" abc ", false); 53 test(" abc\u2022", false); 54 } 55 56 /* 57 * Test full whitespace range 58 */ 59 @Test testWhitespace()60 void testWhitespace() { 61 StringBuilder sb = new StringBuilder(64); 62 IntStream.range(1, 0xFFFF).filter(c -> Character.isWhitespace(c)) 63 .forEach(c -> sb.append((char)c)); 64 String whiteSpace = sb.toString(); 65 66 test(whiteSpace, true); 67 test(whiteSpace + "abc" + whiteSpace, false); 68 } 69 70 /* 71 * Raise an exception if the two inputs are not equivalent. 72 */ test(String input, boolean expected)73 static void test(String input, boolean expected) { 74 // Android-changed: use wrapper to avoid d8 backporting (b/191859202). 75 // assertEquals(input.isBlank(), expected, 76 assertEquals(String_isBlank(input), expected, 77 String.format("Failed test, Input: %s, Expected: %b%n", input, expected)); 78 } 79 80 // Android-added: wrapper to avoid d8 backporting of String.isBlank (b/191859202). String_isBlank(String input)81 private static boolean String_isBlank(String input) { 82 try { 83 MethodType isBlankType = MethodType.methodType(boolean.class); 84 MethodHandle isBlank = 85 MethodHandles.lookup().findVirtual(String.class, "isBlank", isBlankType); 86 return (boolean) isBlank.invokeExact(input); 87 } catch (Throwable t) { 88 throw new RuntimeException(t); 89 } 90 } 91 } 92