0N/A/*
2362N/A * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4120694
0N/A * @summary Test new methods in StringBuffer
0N/A *
0N/A */
0N/A
0N/Aimport java.lang.*;
0N/Aimport java.util.*;
0N/A
0N/Apublic class SBBasher {
0N/A public static void main(String[] args) throws Exception {
0N/A SBBasher basher = new SBBasher();
0N/A
0N/A for (int iterations=0; iterations<100; iterations++) {
0N/A String testString = basher.generateTestString();
0N/A boolean result = basher.Test1(testString);
0N/A if (result == false)
0N/A throw new RuntimeException("Substring or replace failure.");
0N/A }
0N/A
0N/A for (int iterations=0; iterations<100; iterations++) {
0N/A String testString = basher.generateTestString();
0N/A boolean result = basher.Test2(testString);
0N/A if (result == false)
0N/A throw new RuntimeException("Insert or delete failure.");
0N/A }
0N/A
0N/A for (int iterations=0; iterations<100; iterations++) {
0N/A String testString = basher.generateTestString();
0N/A boolean result = basher.Test3(testString);
0N/A if (result == false)
0N/A throw new RuntimeException("Insert, delete or replace failure.");
0N/A }
0N/A
0N/A }
0N/A
0N/A private int getRandomIndex(int constraint1, int constraint2) {
0N/A int range = constraint2 - constraint1;
0N/A int x = generator.nextInt();
0N/A return constraint1 + Math.abs(x % range);
0N/A }
0N/A
0N/A static Random generator = new Random();
0N/A
0N/A private String generateTestString() {
0N/A StringBuffer aNewString = new StringBuffer(120);
0N/A int aNewLength = getRandomIndex(1,100);
0N/A for(int y=0; y<aNewLength; y++) {
0N/A int achar = generator.nextInt();
0N/A char test = (char)(achar);
0N/A aNewString.append(test);
0N/A }
0N/A return aNewString.toString();
0N/A }
0N/A
0N/A /**
0N/A * first test, get substring of a random string
0N/A * and replace same spot with same substring
0N/A * then check for equality with original
0N/A * this tests both substrings and the replace method
0N/A */
0N/A private boolean Test1(String before) {
0N/A StringBuffer bashed = new StringBuffer(before);
0N/A String slice;
0N/A for (int i=0; i<100; i++) {
0N/A int startIndex = getRandomIndex(0, before.length());
0N/A int endIndex = getRandomIndex(startIndex, before.length());
0N/A if (endIndex < bashed.length()) {
0N/A slice = bashed.substring(startIndex, endIndex);
0N/A }
0N/A else {
0N/A slice = bashed.substring(startIndex);
0N/A }
0N/A bashed.replace(startIndex, endIndex, slice);
0N/A }
0N/A String after = bashed.toString();
0N/A if (!before.equals(after))
0N/A return false;
0N/A else
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * second test, delete random section of string
0N/A * then insert same section back again
0N/A * then check for equality with original
0N/A * this tests both substrings, both deletes and insert method
0N/A */
0N/A private boolean Test2(String before) {
0N/A StringBuffer bashed = new StringBuffer(before);
0N/A String slice;
0N/A for (int i=0; i<100; i++) {
0N/A int startIndex = getRandomIndex(0, before.length());
0N/A int endIndex = getRandomIndex(startIndex, before.length());
0N/A if (endIndex < bashed.length())
0N/A slice = bashed.substring(startIndex, endIndex);
0N/A else
0N/A slice = bashed.substring(startIndex);
0N/A if (slice.length() == 1)
0N/A bashed.deleteCharAt(startIndex);
0N/A else
0N/A bashed.delete(startIndex, endIndex);
0N/A bashed.insert(startIndex, slice.toCharArray(), 0, slice.length());
0N/A }
0N/A String after = bashed.toString();
0N/A if (!before.equals(after))
0N/A return false;
0N/A else
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Third test, clone string and make sure that the
0N/A * replace operation is equivalent to a delete followed
0N/A * by an insert with the equivalent arguments
0N/A * this tests replace, delete and insert
0N/A */
0N/A private boolean Test3(String before) {
0N/A StringBuffer bashed1 = new StringBuffer(before);
0N/A StringBuffer bashed2 = new StringBuffer(before);
0N/A int startIndex = getRandomIndex(0, bashed1.length());
0N/A int endIndex = getRandomIndex(startIndex, bashed2.length());
0N/A
0N/A String insertString = generateTestString();
0N/A
0N/A bashed1.delete(startIndex, endIndex);
0N/A bashed1.insert(startIndex, insertString);
0N/A bashed2.replace(startIndex, endIndex, insertString);
0N/A
0N/A String result1 = bashed1.toString();
0N/A String result2 = bashed2.toString();
0N/A if (!result1.equals(result2))
0N/A return false;
0N/A else
0N/A return true;
0N/A }
0N/A
0N/A}