Path: blob/master/test/jdk/java/lang/String/TranslateEscapes.java
41149 views
/*1* Copyright (c) 2019, 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* @bug 822378026* @summary This exercises String#translateEscapes patterns and limits.27* @compile TranslateEscapes.java28* @run main TranslateEscapes29*/3031public class TranslateEscapes {32public static void main(String... arg) {33test1();34test2();35test3();36test4();37}3839/*40* Standard escapes.41*/42static void test1() {43verifyEscape("b", '\b');44verifyEscape("f", '\f');45verifyEscape("n", '\n');46verifyEscape("r", '\r');47verifyEscape("s", '\s');48verifyEscape("t", '\t');49verifyEscape("'", '\'');50verifyEscape("\"", '\"');51verifyEscape("\\", '\\');52}5354/*55* Octal escapes.56*/57static void test2() {58verifyOctalEscape("0", 0);59verifyOctalEscape("3", 03);60verifyOctalEscape("7", 07);61verifyOctalEscape("07", 07);62verifyOctalEscape("17", 017);63verifyOctalEscape("27", 027);64verifyOctalEscape("37", 037);65verifyOctalEscape("377", 0377);6667verifyOctalEscape("777", 077);68verifyOctalEscape("78", 07);69}7071/*72* Exceptions.73*/74static void test3() {75exceptionThrown("+");76exceptionThrown("q");77}7879/*80* Escape line terminator.81*/82static void test4() {83verifyLineTerminator("\n");84verifyLineTerminator("\r\n");85verifyLineTerminator("\r");86}8788static void verifyEscape(String string, char ch) {89String escapes = "\\" + string;90if (escapes.translateEscapes().charAt(0) != ch) {91System.err.format("\"%s\" not escape \"%s\"'%n", string, escapes);92throw new RuntimeException();93}94}9596static void verifyOctalEscape(String string, int octal) {97String escapes = "\\" + string;98if (escapes.translateEscapes().charAt(0) != octal) {99System.err.format("\"%s\" not octal %o%n", string, octal);100throw new RuntimeException();101}102}103104static void exceptionThrown(String string) {105String escapes = "\\" + string;106try {107escapes.translateEscapes();108System.err.format("escape not thrown for %s%n", string);109throw new RuntimeException();110111} catch (IllegalArgumentException ex) {112// okay113}114}115116static void verifyLineTerminator(String string) {117String escapes = "\\" + string;118if (!escapes.translateEscapes().isEmpty()) {119System.err.format("escape for line terminator not handled %s%n",120string.replace("\n", "\\n").replace("\r", "\\r"));121throw new RuntimeException();122}123}124}125126127