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