1 /* 2 * Copyright (c) 2015, 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 package test.java.lang.String.CompactString; 25 26 import java.io.UnsupportedEncodingException; 27 import java.nio.charset.Charset; 28 import java.util.HashMap; 29 import java.util.Map; 30 31 import org.testng.annotations.BeforeClass; 32 33 /* 34 * Base class of tests for Compact String. 35 * 36 */ 37 public class CompactString { 38 39 final Map<String, Map<String, String>> map = new HashMap<>(); 40 41 enum StringSources { 42 EMPTY(STRING_EMPTY, BYTE_ARRAY_EMTPY, CHAR_ARRAY_EMPTY, 43 POINT_ARRAY_EMTPY), LDUPLICATE(STRING_LDUPLICATE, 44 BYTE_ARRAY_LDUPLICATE, CHAR_ARRAY_LDUPLICATE, 45 POINT_ARRAY_LDUPLICATE), LLONG(STRING_LLONG, BYTE_ARRAY_LLONG, 46 CHAR_ARRAY_LLONG, POINT_ARRAY_LLONG), L1(STRING_L1, 47 BYTE_ARRAY_L1, CHAR_ARRAY_L1, POINT_ARRAY_L1), L2(STRING_L2, 48 BYTE_ARRAY_L2, CHAR_ARRAY_L2, POINT_ARRAY_L2), L4(STRING_L4, 49 BYTE_ARRAY_L4, CHAR_ARRAY_L4, POINT_ARRAY_L4), UDUPLICATE( 50 STRING_UDUPLICATE, BYTE_ARRAY_UDUPLICATE, 51 CHAR_ARRAY_UDUPLICATE, POINT_ARRAY_UDUPLICATE), U1(STRING_U1, 52 BYTE_ARRAY_U1, CHAR_ARRAY_U1, POINT_ARRAY_U1), U2(STRING_U2, 53 BYTE_ARRAY_U2, CHAR_ARRAY_U2, POINT_ARRAY_U2), MDUPLICATE1( 54 STRING_MDUPLICATE1, BYTE_ARRAY_MDUPLICATE1, 55 CHAR_ARRAY_MDUPLICATE1, POINT_ARRAY_MDUPLICATE1), MDUPLICATE2( 56 STRING_MDUPLICATE2, BYTE_ARRAY_MDUPLICATE2, 57 CHAR_ARRAY_MDUPLICATE2, POINT_ARRAY_MDUPLICATE2), MLONG1( 58 STRING_MLONG1, BYTE_ARRAY_MLONG1, CHAR_ARRAY_MLONG1, 59 POINT_ARRAY_MLONG1), MLONG2(STRING_MLONG2, BYTE_ARRAY_MLONG2, 60 CHAR_ARRAY_MLONG2, POINT_ARRAY_MLONG2), M11(STRING_M11, 61 BYTE_ARRAY_M11, CHAR_ARRAY_M11, POINT_ARRAY_M11), M12( 62 STRING_M12, BYTE_ARRAY_M12, CHAR_ARRAY_M12, POINT_ARRAY_M12), SUPPLEMENTARY( 63 STRING_SUPPLEMENTARY, BYTE_ARRAY_SUPPLEMENTARY, 64 CHAR_ARRAY_SUPPLEMENTARY, POINT_ARRAY_SUPPLEMENTARY), SUPPLEMENTARY_LOWERCASE( 65 STRING_SUPPLEMENTARY_LOWERCASE, 66 BYTE_ARRAY_SUPPLEMENTARY_LOWERCASE, 67 CHAR_ARRAY_SUPPLEMENTARY_LOWERCASE, 68 POINT_ARRAY_SUPPLEMENTARY_LOWERCASE); 69 StringSources(String s, byte[] b, char[] c, int[] i)70 private StringSources(String s, byte[] b, char[] c, int[] i) { 71 str = s; 72 ba = b; 73 ca = c; 74 ia = i; 75 } 76 getString()77 String getString() { 78 return str; 79 } 80 getByteArray()81 byte[] getByteArray() { 82 return ba; 83 } 84 getCharArray()85 char[] getCharArray() { 86 return ca; 87 } 88 getIntArray()89 int[] getIntArray() { 90 return ia; 91 } 92 93 private final String str; 94 private final byte[] ba; 95 private final char[] ca; 96 private final int[] ia; 97 } 98 99 protected static final String DEFAULT_CHARSET_NAME = "UTF-8"; 100 protected static final Charset DEFAULT_CHARSET = Charset 101 .forName(DEFAULT_CHARSET_NAME); 102 103 protected static final String STRING_EMPTY = ""; 104 protected static final byte[] BYTE_ARRAY_EMTPY = new byte[0]; 105 protected static final char[] CHAR_ARRAY_EMPTY = new char[0]; 106 protected static final int[] POINT_ARRAY_EMTPY = new int[0]; 107 108 protected static final String STRING_LDUPLICATE = "ABABABABAB"; 109 protected static final byte[] BYTE_ARRAY_LDUPLICATE = new byte[] { 'A', 'B', 110 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B' }; 111 protected static final char[] CHAR_ARRAY_LDUPLICATE = new char[] { 'A', 'B', 112 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B' }; 113 protected static final int[] POINT_ARRAY_LDUPLICATE = new int[] { 'A', 'B', 114 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B' }; 115 116 protected static final String STRING_LLONG = "ABCDEFGH"; 117 protected static final byte[] BYTE_ARRAY_LLONG = new byte[] { 'A', 'B', 'C', 118 'D', 'E', 'F', 'G', 'H' }; 119 protected static final char[] CHAR_ARRAY_LLONG = new char[] { 'A', 'B', 'C', 120 'D', 'E', 'F', 'G', 'H' }; 121 protected static final int[] POINT_ARRAY_LLONG = new int[] { 'A', 'B', 'C', 122 'D', 'E', 'F', 'G', 'H' }; 123 124 protected static final String STRING_L1 = "A"; 125 protected static final byte[] BYTE_ARRAY_L1 = new byte[] { 'A' }; 126 protected static final char[] CHAR_ARRAY_L1 = new char[] { 'A' }; 127 protected static final int[] POINT_ARRAY_L1 = new int[] { 'A' }; 128 129 protected static final String STRING_L2 = "AB"; 130 protected static final byte[] BYTE_ARRAY_L2 = new byte[] { 'A', 'B' }; 131 protected static final char[] CHAR_ARRAY_L2 = new char[] { 'A', 'B' }; 132 protected static final int[] POINT_ARRAY_L2 = new int[] { 'A', 'B' }; 133 134 protected static final String STRING_L4 = "ABCD"; 135 protected static final byte[] BYTE_ARRAY_L4 = new byte[] { 'A', 'B', 'C', 'D' }; 136 protected static final char[] CHAR_ARRAY_L4 = new char[] { 'A', 'B', 'C', 'D' }; 137 protected static final int[] POINT_ARRAY_L4 = new int[] { 'A', 'B', 'C', 'D' }; 138 139 /* 140 * Because right now ASCII is the default encoding parameter for source code 141 * in JDK build environment, so we escape them. same as below. 142 */ 143 protected static final String STRING_UDUPLICATE = "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22"; 144 protected static final byte[] BYTE_ARRAY_UDUPLICATE = getBytes(STRING_UDUPLICATE); 145 protected static final char[] CHAR_ARRAY_UDUPLICATE = new char[] { '\uFF21', 146 '\uFF22', '\uFF21', '\uFF22', '\uFF21', '\uFF22', '\uFF21', 147 '\uFF22', '\uFF21', '\uFF22' }; 148 protected static final int[] POINT_ARRAY_UDUPLICATE = new int[] { '\uFF21', 149 '\uFF22', '\uFF21', '\uFF22', '\uFF21', '\uFF22', '\uFF21', 150 '\uFF22', '\uFF21', '\uFF22' }; 151 152 protected static final String STRING_U1 = "\uFF21"; 153 protected static final byte[] BYTE_ARRAY_U1 = getBytes(STRING_U1); 154 protected static final char[] CHAR_ARRAY_U1 = new char[] { '\uFF21' }; 155 protected static final int[] POINT_ARRAY_U1 = new int[] { '\uFF21' }; 156 157 protected static final String STRING_U2 = "\uFF21\uFF22"; 158 protected static final byte[] BYTE_ARRAY_U2 = getBytes(STRING_U2); 159 protected static final char[] CHAR_ARRAY_U2 = new char[] { '\uFF21', '\uFF22' }; 160 protected static final int[] POINT_ARRAY_U2 = new int[] { '\uFF21', '\uFF22' }; 161 162 protected static final String STRING_MDUPLICATE1 = "\uFF21A\uFF21A\uFF21A\uFF21A\uFF21A"; 163 protected static final byte[] BYTE_ARRAY_MDUPLICATE1 = getBytes(STRING_MDUPLICATE1); 164 protected static final char[] CHAR_ARRAY_MDUPLICATE1 = new char[] { '\uFF21', 165 'A', '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A' }; 166 protected static final int[] POINT_ARRAY_MDUPLICATE1 = new int[] { '\uFF21', 167 'A', '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A' }; 168 169 protected static final String STRING_MDUPLICATE2 = "A\uFF21A\uFF21A\uFF21A\uFF21A\uFF21"; 170 protected static final byte[] BYTE_ARRAY_MDUPLICATE2 = getBytes(STRING_MDUPLICATE2); 171 protected static final char[] CHAR_ARRAY_MDUPLICATE2 = new char[] { 'A', 172 '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A', 173 '\uFF21' }; 174 protected static final int[] POINT_ARRAY_MDUPLICATE2 = new int[] { 'A', 175 '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A', '\uFF21', 'A', 176 '\uFF21' }; 177 178 protected static final String STRING_MLONG1 = "A\uFF21B\uFF22C\uFF23D\uFF24E\uFF25F\uFF26G\uFF27H\uFF28"; 179 protected static final byte[] BYTE_ARRAY_MLONG1 = getBytes(STRING_MLONG1); 180 protected static final char[] CHAR_ARRAY_MLONG1 = new char[] { 'A', '\uFF21', 181 'B', '\uFF22', 'C', '\uFF23', 'D', '\uFF24', 'E', '\uFF25', 'F', 182 '\uFF26', 'G', '\uFF27', 'H', '\uFF28' }; 183 protected static final int[] POINT_ARRAY_MLONG1 = new int[] { 'A', '\uFF21', 184 'B', '\uFF22', 'C', '\uFF23', 'D', '\uFF24', 'E', '\uFF25', 'F', 185 '\uFF26', 'G', '\uFF27', 'H', '\uFF28' }; 186 187 protected static final String STRING_MLONG2 = "\uFF21A\uFF22B\uFF23C\uFF24D\uFF25E\uFF26F\uFF27G\uFF28H"; 188 protected static final byte[] BYTE_ARRAY_MLONG2 = getBytes(STRING_MLONG2); 189 protected static final char[] CHAR_ARRAY_MLONG2 = new char[] { '\uFF21', 'A', 190 '\uFF22', 'B', '\uFF23', 'C', '\uFF24', 'D', '\uFF25', 'E', 191 '\uFF26', 'F', '\uFF27', 'G', '\uFF28', 'H' }; 192 protected static final int[] POINT_ARRAY_MLONG2 = new int[] { '\uFF21', 'A', 193 '\uFF22', 'B', '\uFF23', 'C', '\uFF24', 'D', '\uFF25', 'E', 194 '\uFF26', 'F', '\uFF27', 'G', '\uFF28', 'H' }; 195 196 protected static final String STRING_M11 = "A\uFF21"; 197 protected static final byte[] BYTE_ARRAY_M11 = getBytes(STRING_M11); 198 protected static final char[] CHAR_ARRAY_M11 = new char[] { 'A', '\uFF21' }; 199 protected static final int[] POINT_ARRAY_M11 = new int[] { 'A', '\uFF21' }; 200 201 protected static final String STRING_M12 = "\uFF21A"; 202 protected static final byte[] BYTE_ARRAY_M12 = getBytes(STRING_M12); 203 protected static final char[] CHAR_ARRAY_M12 = new char[] { '\uFF21', 'A' }; 204 protected static final int[] POINT_ARRAY_M12 = new int[] { '\uFF21', 'A' }; 205 206 protected static final String STRING_SUPPLEMENTARY = "\uD801\uDC00\uD801\uDC01\uFF21A"; 207 protected static final byte[] BYTE_ARRAY_SUPPLEMENTARY = getBytes(STRING_SUPPLEMENTARY); 208 protected static final char[] CHAR_ARRAY_SUPPLEMENTARY = new char[] { 209 '\uD801', '\uDC00', '\uD801', '\uDC01', '\uFF21', 'A' }; 210 protected static final int[] POINT_ARRAY_SUPPLEMENTARY = new int[] { 211 '\uD801', '\uDC00', '\uD801', '\uDC01', '\uFF21', 'A' }; 212 213 protected static final String STRING_SUPPLEMENTARY_LOWERCASE = "\uD801\uDC28\uD801\uDC29\uFF41a"; 214 protected static final byte[] BYTE_ARRAY_SUPPLEMENTARY_LOWERCASE = getBytes(STRING_SUPPLEMENTARY_LOWERCASE); 215 protected static final char[] CHAR_ARRAY_SUPPLEMENTARY_LOWERCASE = new char[] { 216 '\uD801', '\uDC28', '\uD801', '\uDC29', '\uFF41', 'a' }; 217 protected static final int[] POINT_ARRAY_SUPPLEMENTARY_LOWERCASE = new int[] { 218 '\uD801', '\uDC28', '\uD801', '\uDC29', '\uFF41', 'a' }; 219 220 protected static final String SRC_BYTE_ARRAY_WITH_CHARSETNAME = "source from byte array with charset name"; 221 protected static final String SRC_BYTE_ARRAY_WITH_CHARSET = "source from byte array with charset"; 222 protected static final String SRC_CHAR_ARRAY = "source from char array"; 223 protected static final String SRC_POINT_ARRAY = "source from code point array"; 224 protected static final String SRC_STRING = "source from String"; 225 protected static final String SRC_STRINGBUFFER = "source from StringBuffer"; 226 protected static final String SRC_STRINGBUILDER = "source from StringBuilder"; 227 protected static final String SRC_COPYVALUEOF = "source from copyValueOf from char array"; 228 protected static final String SRC_VALUEOF = "source from valueOf from char array"; 229 230 static { 231 System.out println(String .format("====== The platform's default charset is \\"%s\\", we're using \\"%s\\" for testing.", Charset.defaultCharset().name(), DEFAULT_CHARSET_NAME))232 .println(String 233 .format("====== The platform's default charset is \"%s\", we're using \"%s\" for testing.", 234 Charset.defaultCharset().name(), 235 DEFAULT_CHARSET_NAME)); 236 } 237 getBytes(String str)238 private static byte[] getBytes(String str) { 239 byte[] res = null; 240 try { 241 res = str.getBytes(DEFAULT_CHARSET_NAME); 242 } catch (UnsupportedEncodingException e) { 243 e.printStackTrace(); 244 throw new RuntimeException("caught UnsupportedEncodingException!!!", e); 245 } 246 return res; 247 } 248 setUpOneString(String content, byte[] ba, char[] ca, int[] cpa)249 private void setUpOneString(String content, byte[] ba, char[] ca, int[] cpa) 250 throws UnsupportedEncodingException { 251 final Map<String, String> m = new HashMap<>(); 252 m.put(SRC_BYTE_ARRAY_WITH_CHARSETNAME, new String(ba, 253 DEFAULT_CHARSET_NAME)); 254 m.put(SRC_BYTE_ARRAY_WITH_CHARSET, new String(ba, DEFAULT_CHARSET)); 255 m.put(SRC_CHAR_ARRAY, new String(ca)); 256 m.put(SRC_POINT_ARRAY, new String(cpa, 0, cpa.length)); 257 m.put(SRC_STRING, new String(content)); 258 m.put(SRC_STRINGBUFFER, new String(new StringBuffer(content))); 259 m.put(SRC_STRINGBUILDER, new String(new StringBuilder(content))); 260 m.put(SRC_COPYVALUEOF, String.copyValueOf(ca)); 261 m.put(SRC_VALUEOF, String.valueOf(ca)); 262 map.put(content, m); 263 } 264 265 /* 266 * Set up the test data, use 9 ways to construct one String. 267 * 268 * @throws UnsupportedEncodingException 269 * If the named charset is not supported in setUpOneString(xxx). 270 */ 271 @BeforeClass setUp()272 public void setUp() throws UnsupportedEncodingException { 273 for (StringSources src : StringSources.values()) { 274 setUpOneString(src.getString(), src.getByteArray(), 275 src.getCharArray(), src.getIntArray()); 276 } 277 } 278 279 /* 280 * Escape non-ASCII characters since not all systems support them. 281 */ escapeNonASCIIs(String str)282 protected String escapeNonASCIIs(String str) { 283 StringBuilder sb = new StringBuilder(); 284 for (int i = 0; i < str.length(); i++) { 285 char c = str.charAt(i); 286 if (c > 0x7F) { 287 sb.append("\\u").append(Integer.toHexString((int) c)); 288 } else { 289 sb.append(c); 290 } 291 } 292 return sb.toString(); 293 } 294 295 /* 296 * Escape non-ASCII characters since not all systems support them. 297 */ escapeNonASCII(char c)298 protected String escapeNonASCII(char c) { 299 StringBuilder sb = new StringBuilder(); 300 if (c > 0x7F) { 301 sb.append("\\u").append(Integer.toHexString((int) c)); 302 } else { 303 sb.append(c); 304 } 305 return sb.toString(); 306 } 307 } 308