Path: blob/master/test/jdk/java/lang/String/Transform.java
41149 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*/2223/*24* @test25* @summary Unit tests for String#transform(Function<String, R> f)26* @run main Transform27*/2829import java.util.function.Function;30import java.util.stream.Collectors;3132public class Transform {33public static void main(String[] args) {34test1();35}3637/*38* Test String#transform(Function<? super String, ? extends R> f) functionality.39*/40static void test1() {41simpleTransform("toUpperCase", "abc", s -> s.toUpperCase());42simpleTransform("toLowerCase", "ABC", s -> s.toLowerCase());43simpleTransform("substring", "John Smith", s -> s.substring(0, 4));4445String multiline = " This is line one\n" +46" This is line two\n" +47" This is line three\n";48String expected = "This is line one!\n" +49" This is line two!\n" +50" This is line three!\n";51check("multiline", multiline.transform(string -> {52return string.lines()53.map(s -> s.transform(t -> t.substring(4) + "!"))54.collect(Collectors.joining("\n", "", "\n"));55}), expected);56}5758static void simpleTransform(String test, String s, Function<String, String> f) {59check(test, s.transform(f), f.apply(s));60}6162static void check(String test, Object output, Object expected) {63if (output != expected && (output == null || !output.equals(expected))) {64System.err.println("Testing " + test + ": unexpected result");65System.err.println("Output:");66System.err.println(output);67System.err.println("Expected:");68System.err.println(expected);69throw new RuntimeException();70}71}72}737475