/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @summary Unit tests for String#unescape * @compile --enable-preview -source 12 -encoding utf8 Unescape.java * @run main/othervm --enable-preview Unescape */ @SuppressWarnings( "deprecation" ) public class Unescape { public static void main(String[] args) { test1(); test2(); } /* * Test unescaping functionality. */ static void test1() { equal(`a\tb\u0063`, "a\\tb\\u0063"); equal(`a\tb\u0063`.unescape(), "a\tbc"); equal(`a\tb\u2022`, "a\\tb\\u2022"); equal(`a\tb\u2022`.unescape(), "a\tb\u2022"); equal(`\0\12\012`.unescape(), "\0\12\012"); equal(`•\0\12\012`.unescape(), "\u2022\0\12\012"); equal(`\b`.unescape(), "\b"); equal(`\f`.unescape(), "\f"); equal(`\n`.unescape(), "\n"); equal(`\r`.unescape(), "\r"); equal(`\t`.unescape(), "\t"); equal(`\0`.unescape(), "\0"); equal(`\7`.unescape(), "\7"); equal(`\12`.unescape(), "\12"); equal(`\012`.unescape(), "\012"); equal(`\u0000`.unescape(), "\u0000"); equal(`\u2022`.unescape(), "\u2022"); equal(`•\b`.unescape(), "•\b"); equal(`•\f`.unescape(), "•\f"); equal(`•\n`.unescape(), "•\n"); equal(`•\r`.unescape(), "•\r"); equal(`•\t`.unescape(), "•\t"); equal(`•\0`.unescape(), "•\0"); equal(`•\7`.unescape(), "•\7"); equal(`•\12`.unescape(), "•\12"); equal(`•\177`.unescape(), "•\177"); equal(`•\u0000`.unescape(), "•\u0000"); equal(`•\u2022`.unescape(), "•\u2022"); } /* * Test for IllegalArgumentException. */ static void test2() { wellFormed(`\b`); wellFormed(`\f`); wellFormed(`\n`); wellFormed(`\r`); wellFormed(`\t`); wellFormed(`\0`); wellFormed(`\7`); wellFormed(`\12`); wellFormed(`\012`); wellFormed(`\u0000`); wellFormed(`\u2022`); wellFormed(`•\b`); wellFormed(`•\f`); wellFormed(`•\n`); wellFormed(`•\r`); wellFormed(`•\t`); wellFormed(`•\0`); wellFormed(`•\7`); wellFormed(`•\12`); wellFormed(`•\012`); wellFormed(`•\u0000`); wellFormed(`•\u2022`); malformed(`\x`); malformed(`\+`); malformed(`\u`); malformed(`\uuuuu`); malformed(`\u2`); malformed(`\u20`); malformed(`\u202`); malformed(`\u2 `); malformed(`\u20 `); malformed(`\u202 `); malformed(`\uuuuu2`); malformed(`\uuuuu20`); malformed(`\uuuuu202`); malformed(`\uuuuu2 `); malformed(`\uuuuu20 `); malformed(`\uuuuu202 `); malformed(`\uG`); malformed(`\u2G`); malformed(`\u20G`); malformed(`\uG `); malformed(`\u2G `); malformed(`\u20G `); malformed(`\uuuuuG`); malformed(`\uuuuu2G`); malformed(`\uuuuu20G`); malformed(`\uuuuuG `); malformed(`\uuuuu2G `); malformed(`\uuuuu20G `); malformed(`•\x`); malformed(`•\+`); malformed(`•\u`); malformed(`•\uuuuu`); malformed(`•\u2`); malformed(`•\u20`); malformed(`•\u202`); malformed(`•\u2 `); malformed(`•\u20 `); malformed(`•\u202 `); malformed(`•\uuuuu2`); malformed(`•\uuuuu20`); malformed(`•\uuuuu202`); malformed(`•\uuuuu2 `); malformed(`•\uuuuu20 `); malformed(`•\uuuuu202 `); malformed(`•\uG`); malformed(`•\u2G`); malformed(`•\u20G`); malformed(`•\uG `); malformed(`•\u2G `); malformed(`•\u20G `); malformed(`•\uuuuuG`); malformed(`•\uuuuu2G`); malformed(`•\uuuuu20G`); malformed(`•\uuuuuG `); malformed(`•\uuuuu2G `); malformed(`•\uuuuu20G `); } /* * Report difference in result. */ static void report(String message, String inputTag, String input, String outputTag, String output) { System.err.println(message); System.err.println(); System.err.println(inputTag); System.err.println(input.replaceAll(" ", ".")); System.err.println(); System.err.println(outputTag); System.err.println(output.replaceAll(" ", ".")); throw new RuntimeException(); } /* * Raise an exception if the two inputs are not equivalent. */ static void equal(String input, String expected) { if (input == null || expected == null || !expected.equals(input)) { report("Failed equal", "Input:", input, "Expected:", expected); } } /* * Raise an exception if the string contains a malformed escape. */ static void wellFormed(String rawString) { try { rawString.unescape(); } catch (IllegalArgumentException ex) { System.err.println("Failed wellFormed"); System.err.println(rawString); throw new RuntimeException(); } } /* * Raise an exception if the string does not contain a malformed escape. */ static void malformed(String rawString) { try { rawString.unescape(); System.err.println("Failed malformed"); System.err.println(rawString); throw new RuntimeException(); } catch (IllegalArgumentException ex) { // incorrectly formed escapes } } }