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