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.Collectors; 31 import java.util.stream.IntStream; 32 33 import org.testng.annotations.Test; 34 35 /** 36 * @test 37 * @summary Basic strip, stripLeading, stripTrailing functionality 38 * @bug 8200377 39 * @run main/othervm Strip 40 */ 41 42 public class Strip { 43 44 /* 45 * Test basic stripping routines 46 */ 47 @Test testStrip()48 public void testStrip() { 49 // Android-changed: call wrappered string methods to avoid d8 backport (b/191859202). 50 // equal(" abc ".strip(), "abc"); 51 // equal(" abc ".stripLeading(), "abc "); 52 // equal(" abc ".stripTrailing(), " abc"); 53 // equal(" abc\u2022 ".strip(), "abc\u2022"); 54 // equal(" abc\u2022 ".stripLeading(), "abc\u2022 "); 55 // equal(" abc\u2022 ".stripTrailing(), " abc\u2022"); 56 // equal("".strip(), ""); 57 // equal("".stripLeading(), ""); 58 // equal("".stripTrailing(), ""); 59 // equal("\b".strip(), "\b"); 60 // equal("\b".stripLeading(), "\b"); 61 // equal("\b".stripTrailing(), "\b"); 62 equal(String_strip(" abc "), "abc"); 63 equal(String_stripLeading(" abc "), "abc "); 64 equal(String_stripTrailing(" abc "), " abc"); 65 equal(String_strip(" abc\u2022 "), "abc\u2022"); 66 equal(String_stripLeading(" abc\u2022 "), "abc\u2022 "); 67 equal(String_stripTrailing(" abc\u2022 "), " abc\u2022"); 68 equal(String_strip(""), ""); 69 equal(String_stripLeading(""), ""); 70 equal(String_stripTrailing(""), ""); 71 equal(String_strip("\b"), "\b"); 72 equal(String_stripLeading("\b"), "\b"); 73 equal(String_stripTrailing("\b"), "\b"); 74 } 75 76 /* 77 * Test full whitespace range 78 */ 79 @Test testWhitespace()80 public void testWhitespace() { 81 StringBuilder sb = new StringBuilder(64); 82 IntStream.range(1, 0xFFFF).filter(c -> Character.isWhitespace(c)) 83 .forEach(c -> sb.append((char)c)); 84 String whiteSpace = sb.toString(); 85 86 String testString = whiteSpace + "abc" + whiteSpace; 87 // Android-changed: call wrappered string methods to avoid d8 backport (b/191859202). 88 // equal(testString.strip(), "abc"); 89 // equal(testString.stripLeading(), "abc" + whiteSpace); 90 // equal(testString.stripTrailing(), whiteSpace + "abc"); 91 equal(String_strip(testString), "abc"); 92 equal(String_stripLeading(testString), "abc" + whiteSpace); 93 equal(String_stripTrailing(testString), whiteSpace + "abc"); 94 } 95 96 /* 97 * Report difference in result. 98 */ report(String message, String inputTag, String input, String outputTag, String output)99 static void report(String message, String inputTag, String input, 100 String outputTag, String output) { 101 System.err.println(message); 102 System.err.println(); 103 System.err.println(inputTag); 104 System.err.println(input.codePoints() 105 .mapToObj(c -> (Integer)c) 106 .collect(Collectors.toList())); 107 System.err.println(); 108 System.err.println(outputTag); 109 System.err.println(output.codePoints() 110 .mapToObj(c -> (Integer)c) 111 .collect(Collectors.toList())); 112 throw new RuntimeException(); 113 } 114 115 /* 116 * Raise an exception if the two inputs are not equivalent. 117 */ equal(String input, String expected)118 static void equal(String input, String expected) { 119 if (input == null || expected == null || !expected.equals(input)) { 120 report("Failed equal", "Input:", input, "Expected:", expected); 121 } 122 } 123 124 // Android-added: wrapper to avoid d8 backporting of String strip methods (b/191859202). String_stripCommon(String method, String input)125 private static String String_stripCommon(String method, String input) { 126 try { 127 MethodType type = MethodType.methodType(String.class); 128 MethodHandle strip = MethodHandles.lookup().findVirtual(String.class, method, type); 129 return (String) strip.invokeExact(input); 130 } catch (Throwable t) { 131 throw new RuntimeException(t); 132 } 133 } 134 135 // Android-added: wrapper to avoid d8 backporting of String.strip()(b/191859202). String_strip(String input)136 private static String String_strip(String input) { 137 return String_stripCommon("strip", input); 138 } 139 140 // Android-added: wrapper to avoid d8 backporting of String.stripLeading() (b/191859202). String_stripLeading(String input)141 private static String String_stripLeading(String input) { 142 return String_stripCommon("stripLeading", input); 143 } 144 145 // Android-added: wrapper to avoid d8 backporting of String.stripTrailing() (b/191859202). String_stripTrailing(String input)146 private static String String_stripTrailing(String input) { 147 return String_stripCommon("stripTrailing", input); 148 } 149 } 150