Path: blob/master/test/jdk/java/lang/String/Strip.java
41152 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.List;24import java.util.stream.Collectors;25import java.util.stream.IntStream;2627/**28* @test29* @summary Basic strip, stripLeading, stripTrailing functionality30* @bug 820037731* @run main/othervm Strip32*/3334public class Strip {35public static void main(String... arg) {36testStrip();37testWhitespace();38}3940/*41* Test basic stripping routines42*/43static void testStrip() {44equal(" abc ".strip(), "abc");45equal(" abc ".stripLeading(), "abc ");46equal(" abc ".stripTrailing(), " abc");47equal(" abc\u2022 ".strip(), "abc\u2022");48equal(" abc\u2022 ".stripLeading(), "abc\u2022 ");49equal(" abc\u2022 ".stripTrailing(), " abc\u2022");50equal("".strip(), "");51equal("".stripLeading(), "");52equal("".stripTrailing(), "");53equal("\b".strip(), "\b");54equal("\b".stripLeading(), "\b");55equal("\b".stripTrailing(), "\b");56}5758/*59* Test full whitespace range60*/61static void testWhitespace() {62StringBuilder sb = new StringBuilder(64);63IntStream.range(1, 0xFFFF).filter(c -> Character.isWhitespace(c))64.forEach(c -> sb.append((char)c));65String whiteSpace = sb.toString();6667String testString = whiteSpace + "abc" + whiteSpace;68equal(testString.strip(), "abc");69equal(testString.stripLeading(), "abc" + whiteSpace);70equal(testString.stripTrailing(), whiteSpace + "abc");71}7273/*74* Report difference in result.75*/76static void report(String message, String inputTag, String input,77String outputTag, String output) {78System.err.println(message);79System.err.println();80System.err.println(inputTag);81System.err.println(input.codePoints()82.mapToObj(c -> (Integer)c)83.collect(Collectors.toList()));84System.err.println();85System.err.println(outputTag);86System.err.println(output.codePoints()87.mapToObj(c -> (Integer)c)88.collect(Collectors.toList()));89throw new RuntimeException();90}9192/*93* Raise an exception if the two inputs are not equivalent.94*/95static void equal(String input, String expected) {96if (input == null || expected == null || !expected.equals(input)) {97report("Failed equal", "Input:", input, "Expected:", expected);98}99}100}101102103