/* * Copyright (c) 2012, 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. */ /** * * @summary utilities class supporting common operation for tests. * @(#)StringUtilities.java * @author Tristan Yan * @version 1.0 */ import java.util.Random; public class StringUtilities { private final static Random RANDOM = new Random(System.currentTimeMillis()); public static String randomString(int max_length, int min_length){ return randomAscii(min_length + RANDOM.nextInt(max_length - min_length)); } public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars, Random rnd) { if (count == 0) { return ""; } else if (count < 0) { throw new IllegalArgumentException("Requested random string length " + count + " is less than 0."); } if ((start == 0) && (end == 0)) { end = 'z' + 1; start = ' '; if (!letters && !numbers) { start = 0; end = Integer.MAX_VALUE; } } char[] buffer = new char[count]; int gap = end - start; while (count-- != 0) { char ch; if (chars == null) { ch = (char) (rnd.nextInt(gap) + start); } else { ch = chars[rnd.nextInt(gap) + start]; } if ((letters && Character.isLetter(ch)) || (numbers && Character.isDigit(ch)) || (!letters && !numbers)) { if(ch >= 56320 && ch <= 57343) { if(count == 0) { count++; } else { // low surrogate, insert high surrogate after putting it in buffer[count] = ch; count--; buffer[count] = (char) (55296 + rnd.nextInt(128)); } } else if(ch >= 55296 && ch <= 56191) { if(count == 0) { count++; } else { // high surrogate, insert low surrogate before putting it in buffer[count] = (char) (56320 + rnd.nextInt(128)); count--; buffer[count] = ch; } } else if(ch >= 56192 && ch <= 56319) { // private high surrogate, no effing clue, so skip it count++; } else { buffer[count] = ch; } } else { count++; } } return new String(buffer); } public static String random(int count) { return random(count, false, false); } public static String randomAscii(int count) { return random(count, 32, 127, false, false); } public static String randomAlphabetic(int count) { return random(count, true, false); } public static String randomAlphanumeric(int count) { return random(count, true, true); } public static String randomNumeric(int count) { return random(count, false, true); } public static String random(int count, boolean letters, boolean numbers) { return random(count, 0, 0, letters, numbers); } public static String random(int count, int start, int end, boolean letters, boolean numbers) { return random(count, start, end, letters, numbers, null, RANDOM); } public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) { return random(count, start, end, letters, numbers, chars, RANDOM); } }