1 /* 2 * Copyright (c) 2019, 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 /* 26 * @test 27 * @bug 8223780 28 * @summary This exercises String#translateEscapes patterns and limits. 29 * @compile TranslateEscapes.java 30 * @run main TranslateEscapes 31 */ 32 33 import org.testng.annotations.Test; 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertTrue; 36 import static org.testng.Assert.fail; 37 38 public class TranslateEscapes { 39 40 @Test testStandardEscapes()41 public void testStandardEscapes() { 42 verifyEscape("b", '\b'); 43 verifyEscape("f", '\f'); 44 verifyEscape("n", '\n'); 45 verifyEscape("r", '\r'); 46 verifyEscape("t", '\t'); 47 verifyEscape("'", '\''); 48 verifyEscape("\"", '\"'); 49 verifyEscape("\\", '\\'); 50 } 51 52 @Test testOctalEscapes()53 public void testOctalEscapes() { 54 verifyOctalEscape("0", 0); 55 verifyOctalEscape("3", 03); 56 verifyOctalEscape("7", 07); 57 verifyOctalEscape("07", 07); 58 verifyOctalEscape("17", 017); 59 verifyOctalEscape("27", 027); 60 verifyOctalEscape("37", 037); 61 verifyOctalEscape("377", 0377); 62 63 verifyOctalEscape("777", 077); 64 verifyOctalEscape("78", 07); 65 } 66 67 @Test testExceptions()68 static void testExceptions() { 69 exceptionThrown("+"); 70 exceptionThrown("q"); 71 } 72 73 @Test testEscapeLineTerminator()74 public void testEscapeLineTerminator() { 75 verifyLineTerminator("\n"); 76 verifyLineTerminator("\r\n"); 77 verifyLineTerminator("\r"); 78 } 79 verifyEscape(String string, char ch)80 static void verifyEscape(String string, char ch) { 81 String escapes = "\\" + string; 82 assertEquals(escapes.translateEscapes().charAt(0), ch, 83 String.format("\"%s\" not escape \"%s\"'%n", string, escapes)); 84 } 85 verifyOctalEscape(String string, int octal)86 static void verifyOctalEscape(String string, int octal) { 87 String escapes = "\\" + string; 88 assertEquals(escapes.translateEscapes().charAt(0), octal, 89 String.format("\"%s\" not octal %o%n", string, octal)); 90 } 91 exceptionThrown(String string)92 static void exceptionThrown(String string) { 93 String escapes = "\\" + string; 94 try { 95 escapes.translateEscapes(); 96 fail(String.format("escape not thrown for %s%n", string)); 97 } catch (IllegalArgumentException ex) { 98 // okay 99 } 100 } 101 verifyLineTerminator(String string)102 static void verifyLineTerminator(String string) { 103 String escapes = "\\" + string; 104 assertTrue(escapes.translateEscapes().isEmpty(), 105 String.format("escape for line terminator not handled %s%n", 106 string.replace("\n", "\\n").replace("\r", "\\r"))); 107 } 108 } 109