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#unescape 27 * @compile --enable-preview -source 12 -encoding utf8 Unescape.java 28 * @run main/othervm --enable-preview Unescape 29 */ 30 31 @SuppressWarnings( "deprecation" ) 32 public class Unescape { 33 public static void main(String[] args) { 34 System.err.println("HERE:" + (`a\tb\u0063`) + ":"); 35 test1(); 36 test2(); 37 } 38 39 /* 40 * Test unescaping functionality. 41 */ 42 static void test1() { 43 equal(`a\tb\u0063`, "a\\tb\\u0063"); 44 equal(`a\tb\u0063`.unescape(), "a\tbc"); 45 equal(`a\tb\u2022`, "a\\tb\\u2022"); 46 equal(`a\tb\u2022`.unescape(), "a\tb\u2022"); 47 equal(`\0\12\012`.unescape(), "\0\12\012"); 48 equal(`•\0\12\012`.unescape(), "\u2022\0\12\012"); 49 50 equal(`\b`.unescape(), "\b"); 51 equal(`\f`.unescape(), "\f"); 52 equal(`\n`.unescape(), "\n"); 53 equal(`\r`.unescape(), "\r"); 54 equal(`\t`.unescape(), "\t"); 55 equal(`\0`.unescape(), "\0"); 56 equal(`\7`.unescape(), "\7"); 57 equal(`\12`.unescape(), "\12"); 58 equal(`\012`.unescape(), "\012"); 59 equal(`\u0000`.unescape(), "\u0000"); 60 equal(`\u2022`.unescape(), "\u2022"); 61 equal(`•\b`.unescape(), "•\b"); 62 equal(`•\f`.unescape(), "•\f"); 63 equal(`•\n`.unescape(), "•\n"); 64 equal(`•\r`.unescape(), "•\r"); 65 equal(`•\t`.unescape(), "•\t"); 66 equal(`•\0`.unescape(), "•\0"); 67 equal(`•\7`.unescape(), "•\7"); 68 equal(`•\12`.unescape(), "•\12"); 69 equal(`•\177`.unescape(), "•\177"); 70 equal(`•\u0000`.unescape(), "•\u0000"); 71 equal(`•\u2022`.unescape(), "•\u2022"); 72 73 equal(`\u005c\u0072`.unescape(), "\r"); 74 } 75 76 /* 77 * Test for IllegalArgumentException. 78 */ 79 static void test2() { 80 wellFormed(`\b`); 81 wellFormed(`\f`); 82 wellFormed(`\n`); 83 wellFormed(`\r`); 84 wellFormed(`\t`); 85 wellFormed(`\0`); 86 wellFormed(`\7`); 87 wellFormed(`\12`); 88 wellFormed(`\012`); 89 wellFormed(`\u0000`); 90 wellFormed(`\u2022`); 91 wellFormed(`•\b`); 92 wellFormed(`•\f`); 93 wellFormed(`•\n`); 94 wellFormed(`•\r`); 95 wellFormed(`•\t`); 96 wellFormed(`•\0`); 97 wellFormed(`•\7`); 98 wellFormed(`•\12`); 99 wellFormed(`•\012`); 100 wellFormed(`•\u0000`); 101 wellFormed(`•\u2022`); 102 103 malformed(`\x`); 104 malformed(`\+`); 105 malformed(`\u`); 106 malformed(`\uuuuu`); 107 malformed(`\u2`); 108 malformed(`\u20`); 109 malformed(`\u202`); 110 malformed(`\u2 `); 111 malformed(`\u20 `); 112 malformed(`\u202 `); 113 malformed(`\uuuuu2`); 114 malformed(`\uuuuu20`); 115 malformed(`\uuuuu202`); 116 malformed(`\uuuuu2 `); 117 malformed(`\uuuuu20 `); 118 malformed(`\uuuuu202 `); 119 malformed(`\uG`); 120 malformed(`\u2G`); 121 malformed(`\u20G`); 122 malformed(`\uG `); 123 malformed(`\u2G `); 124 malformed(`\u20G `); 125 malformed(`\uuuuuG`); 126 malformed(`\uuuuu2G`); 127 malformed(`\uuuuu20G`); 128 malformed(`\uuuuuG `); 129 malformed(`\uuuuu2G `); 130 malformed(`\uuuuu20G `); 131 132 malformed(`•\x`); 133 malformed(`•\+`); 134 malformed(`•\u`); 135 malformed(`•\uuuuu`); 136 malformed(`•\u2`); 137 malformed(`•\u20`); 138 malformed(`•\u202`); 139 malformed(`•\u2 `); 140 malformed(`•\u20 `); 141 malformed(`•\u202 `); 142 malformed(`•\uuuuu2`); 143 malformed(`•\uuuuu20`); 144 malformed(`•\uuuuu202`); 145 malformed(`•\uuuuu2 `); 146 malformed(`•\uuuuu20 `); 147 malformed(`•\uuuuu202 `); 148 malformed(`•\uG`); 149 malformed(`•\u2G`); 150 malformed(`•\u20G`); 151 malformed(`•\uG `); 152 malformed(`•\u2G `); 153 malformed(`•\u20G `); 154 malformed(`•\uuuuuG`); 155 malformed(`•\uuuuu2G`); 156 malformed(`•\uuuuu20G`); 157 malformed(`•\uuuuuG `); 158 malformed(`•\uuuuu2G `); 159 malformed(`•\uuuuu20G `); 160 } 161 162 /* 163 * Report difference in result. 164 */ 165 static void report(String message, String inputTag, String input, 166 String outputTag, String output) { 167 System.err.println(message); 168 System.err.println(); 169 System.err.println(inputTag); 170 System.err.println(input.replaceAll(" ", ".")); 171 System.err.println(); 172 System.err.println(outputTag); 173 System.err.println(output.replaceAll(" ", ".")); 174 throw new RuntimeException(); 175 } 176 177 /* 178 * Raise an exception if the two inputs are not equivalent. 179 */ 180 static void equal(String input, String expected) { 181 if (input == null || expected == null || !expected.equals(input)) { 182 report("Failed equal", "Input:", input, "Expected:", expected); 183 } 184 } 185 186 /* 187 * Raise an exception if the string contains a malformed escape. 188 */ 189 static void wellFormed(String rawString) { 190 try { 191 rawString.unescape(); 192 } catch (IllegalArgumentException ex) { 193 System.err.println("Failed wellFormed"); 194 System.err.println(rawString); 195 throw new RuntimeException(); 196 } 197 } 198 199 /* 200 * Raise an exception if the string does not contain a malformed escape. 201 */ 202 static void malformed(String rawString) { 203 try { 204 rawString.unescape(); 205 System.err.println("Failed malformed"); 206 System.err.println(rawString); 207 throw new RuntimeException(); 208 } catch (IllegalArgumentException ex) { 209 // incorrectly formed escapes 210 } 211 } 212 }