0N/A/*
2362N/A * Copyright (c) 2000, 2004, 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 4242309 4982981
0N/A * @summary Test equals and contentEquals in String
0N/A */
0N/Aimport java.util.Random;
0N/Aimport java.nio.CharBuffer;
0N/A
0N/A// Yes, I used cut and paste for this test. Yes more code
0N/A// sharing could have been accomplished.
0N/Apublic class ContentEquals {
0N/A
0N/A private static Random rnd = new Random();
0N/A private static final int ITERATIONS = 1000;
0N/A private static final int STR_LEN = 20;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A testStringBuffer();
0N/A testStringBuilder();
0N/A testString();
0N/A testCharSequence();
0N/A }
0N/A
0N/A // Test a StringBuffer, which uses the old method from 1.4
0N/A public static void testStringBuffer() throws Exception {
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A int length = rnd.nextInt(STR_LEN) + 1;
0N/A StringBuffer testStringBuffer = new StringBuffer();
0N/A for(int x=0; x<length; x++) {
0N/A char aChar = (char)rnd.nextInt();
0N/A testStringBuffer.append(aChar);
0N/A }
0N/A String testString = testStringBuffer.toString();
0N/A char c = testStringBuffer.charAt(0);
0N/A testStringBuffer.setCharAt(0, 'c');
0N/A testStringBuffer.setCharAt(0, c);
0N/A if (!testString.contentEquals(testStringBuffer))
0N/A throw new RuntimeException("ContentsEqual failure");
0N/A }
0N/A }
0N/A
0N/A // Test StringBuilder as a CharSequence using new method in 1.5
0N/A public static void testStringBuilder() throws Exception {
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A int length = rnd.nextInt(STR_LEN) + 1;
0N/A StringBuilder testStringBuilder = new StringBuilder();
0N/A for(int x=0; x<length; x++) {
0N/A char aChar = (char)rnd.nextInt();
0N/A testStringBuilder.append(aChar);
0N/A }
0N/A String testString = testStringBuilder.toString();
0N/A char c = testStringBuilder.charAt(0);
0N/A testStringBuilder.setCharAt(0, 'c');
0N/A testStringBuilder.setCharAt(0, c);
0N/A if (!testString.contentEquals(testStringBuilder))
0N/A throw new RuntimeException("ContentsEqual failure");
0N/A }
0N/A }
0N/A
0N/A // Test a String as a CharSequence. This takes a different codepath in
0N/A // the new method in 1.5
0N/A public static void testString() throws Exception {
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A int length = rnd.nextInt(STR_LEN) + 1;
0N/A StringBuilder testStringBuilder = new StringBuilder();
0N/A for(int x=0; x<length; x++) {
0N/A char aChar = (char)rnd.nextInt();
0N/A testStringBuilder.append(aChar);
0N/A }
0N/A String testString = testStringBuilder.toString();
0N/A char c = testStringBuilder.charAt(0);
0N/A testStringBuilder.setCharAt(0, 'c');
0N/A testStringBuilder.setCharAt(0, c);
0N/A if (!testString.contentEquals(testStringBuilder.toString()))
0N/A throw new RuntimeException("ContentsEqual failure");
0N/A }
0N/A }
0N/A
0N/A // Test a CharSequence that is not an AbstractStringBuilder,
0N/A // this takes a different codepath in the new method in 1.5
0N/A public static void testCharSequence() throws Exception {
0N/A for (int i=0; i<ITERATIONS; i++) {
0N/A int length = rnd.nextInt(STR_LEN) + 1;
0N/A StringBuilder testStringBuilder = new StringBuilder();
0N/A for(int x=0; x<length; x++) {
0N/A char aChar = (char)rnd.nextInt();
0N/A testStringBuilder.append(aChar);
0N/A }
0N/A String testString = testStringBuilder.toString();
0N/A char c = testStringBuilder.charAt(0);
0N/A testStringBuilder.setCharAt(0, 'c');
0N/A testStringBuilder.setCharAt(0, c);
0N/A CharBuffer buf = CharBuffer.wrap(testStringBuilder.toString());
0N/A if (!testString.contentEquals(buf))
0N/A throw new RuntimeException("ContentsEqual failure");
0N/A }
0N/A }
0N/A}