1 /*
   2  * Copyright (c) 2000, 2004, 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 /*
  25  * @test
  26  * @bug 4242309 4982981
  27  * @summary Test equals and contentEquals in String
  28  * @key randomness
  29  */
  30 import java.util.Random;
  31 import java.nio.CharBuffer;
  32 
  33 // Yes, I used cut and paste for this test. Yes more code
  34 // sharing could have been accomplished.
  35 public class ContentEquals {
  36 
  37     private static Random rnd = new Random();
  38     private static final int ITERATIONS = 1000;
  39     private static final int STR_LEN = 20;
  40 
  41     public static void main(String[] args) throws Exception {
  42         testStringBuffer();
  43         testStringBuilder();
  44         testString();
  45         testCharSequence();
  46     }
  47 
  48     // Test a StringBuffer, which uses the old method from 1.4
  49     public static void testStringBuffer() throws Exception {
  50         for (int i=0; i<ITERATIONS; i++) {
  51             int length = rnd.nextInt(STR_LEN) + 1;
  52             StringBuffer testStringBuffer = new StringBuffer();
  53             for(int x=0; x<length; x++) {
  54                 char aChar = (char)rnd.nextInt();
  55                 testStringBuffer.append(aChar);
  56             }
  57             String testString = testStringBuffer.toString();
  58             char c = testStringBuffer.charAt(0);
  59             testStringBuffer.setCharAt(0, 'c');
  60             testStringBuffer.setCharAt(0, c);
  61             if (!testString.contentEquals(testStringBuffer))
  62                 throw new RuntimeException("ContentsEqual failure");
  63         }
  64     }
  65 
  66     // Test StringBuilder as a CharSequence using new method in 1.5
  67     public static void testStringBuilder() throws Exception {
  68         for (int i=0; i<ITERATIONS; i++) {
  69             int length = rnd.nextInt(STR_LEN) + 1;
  70             StringBuilder testStringBuilder = new StringBuilder();
  71             for(int x=0; x<length; x++) {
  72                 char aChar = (char)rnd.nextInt();
  73                 testStringBuilder.append(aChar);
  74             }
  75             String testString = testStringBuilder.toString();
  76             char c = testStringBuilder.charAt(0);
  77             testStringBuilder.setCharAt(0, 'c');
  78             testStringBuilder.setCharAt(0, c);
  79             if (!testString.contentEquals(testStringBuilder))
  80                 throw new RuntimeException("ContentsEqual failure");
  81         }
  82     }
  83 
  84     // Test a String as a CharSequence. This takes a different codepath in
  85     // the new method in 1.5
  86     public static void testString() throws Exception {
  87         for (int i=0; i<ITERATIONS; i++) {
  88             int length = rnd.nextInt(STR_LEN) + 1;
  89             StringBuilder testStringBuilder = new StringBuilder();
  90             for(int x=0; x<length; x++) {
  91                 char aChar = (char)rnd.nextInt();
  92                 testStringBuilder.append(aChar);
  93             }
  94             String testString = testStringBuilder.toString();
  95             char c = testStringBuilder.charAt(0);
  96             testStringBuilder.setCharAt(0, 'c');
  97             testStringBuilder.setCharAt(0, c);
  98             if (!testString.contentEquals(testStringBuilder.toString()))
  99                 throw new RuntimeException("ContentsEqual failure");
 100         }
 101     }
 102 
 103     // Test a CharSequence that is not an AbstractStringBuilder,
 104     // this takes a different codepath in the new method in 1.5
 105     public static void testCharSequence() throws Exception {
 106         for (int i=0; i<ITERATIONS; i++) {
 107             int length = rnd.nextInt(STR_LEN) + 1;
 108             StringBuilder testStringBuilder = new StringBuilder();
 109             for(int x=0; x<length; x++) {
 110                 char aChar = (char)rnd.nextInt();
 111                 testStringBuilder.append(aChar);
 112             }
 113             String testString = testStringBuilder.toString();
 114             char c = testStringBuilder.charAt(0);
 115             testStringBuilder.setCharAt(0, 'c');
 116             testStringBuilder.setCharAt(0, c);
 117             CharBuffer buf = CharBuffer.wrap(testStringBuilder.toString());
 118             if (!testString.contentEquals(buf))
 119                 throw new RuntimeException("ContentsEqual failure");
 120         }
 121     }
 122 }