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