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 import java.util.List; 25 import java.util.stream.Collectors; 26 import java.util.stream.IntStream; 27 28 /** 29 * @test 30 * @summary Basic strip, stripLeading, stripTrailing functionality 31 * @bug 8200377 32 * @run main/othervm Strip 33 */ 34 35 public class Strip { main(String... arg)36 public static void main(String... arg) { 37 testStrip(); 38 testWhitespace(); 39 } 40 41 /* 42 * Test basic stripping routines 43 */ testStrip()44 static void testStrip() { 45 equal(" abc ".strip(), "abc"); 46 equal(" abc ".stripLeading(), "abc "); 47 equal(" abc ".stripTrailing(), " abc"); 48 equal(" abc\u2022 ".strip(), "abc\u2022"); 49 equal(" abc\u2022 ".stripLeading(), "abc\u2022 "); 50 equal(" abc\u2022 ".stripTrailing(), " abc\u2022"); 51 equal("".strip(), ""); 52 equal("".stripLeading(), ""); 53 equal("".stripTrailing(), ""); 54 equal("\b".strip(), "\b"); 55 equal("\b".stripLeading(), "\b"); 56 equal("\b".stripTrailing(), "\b"); 57 } 58 59 /* 60 * Test full whitespace range 61 */ testWhitespace()62 static void testWhitespace() { 63 StringBuilder sb = new StringBuilder(64); 64 IntStream.range(1, 0xFFFF).filter(c -> Character.isWhitespace(c)) 65 .forEach(c -> sb.append((char)c)); 66 String whiteSpace = sb.toString(); 67 68 String testString = whiteSpace + "abc" + whiteSpace; 69 equal(testString.strip(), "abc"); 70 equal(testString.stripLeading(), "abc" + whiteSpace); 71 equal(testString.stripTrailing(), whiteSpace + "abc"); 72 } 73 74 /* 75 * Report difference in result. 76 */ report(String message, String inputTag, String input, String outputTag, String output)77 static void report(String message, String inputTag, String input, 78 String outputTag, String output) { 79 System.err.println(message); 80 System.err.println(); 81 System.err.println(inputTag); 82 System.err.println(input.codePoints() 83 .mapToObj(c -> (Integer)c) 84 .collect(Collectors.toList())); 85 System.err.println(); 86 System.err.println(outputTag); 87 System.err.println(output.codePoints() 88 .mapToObj(c -> (Integer)c) 89 .collect(Collectors.toList())); 90 throw new RuntimeException(); 91 } 92 93 /* 94 * Raise an exception if the two inputs are not equivalent. 95 */ equal(String input, String expected)96 static void equal(String input, String expected) { 97 if (input == null || expected == null || !expected.equals(input)) { 98 report("Failed equal", "Input:", input, "Expected:", expected); 99 } 100 } 101 } 102