test/java/util/regex/RegExTest.java

Print this page




  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  * @summary tests RegExp framework
  27  * @author Mike McCloskey
  28  * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
  29  * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
  30  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
  31  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
  32  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
  33  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
  34  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
  35  * 8027645 8035076
  36  */
  37 
  38 import java.util.regex.*;
  39 import java.util.Random;
  40 import java.io.*;
  41 import java.util.*;
  42 import java.nio.CharBuffer;
  43 import java.util.function.Predicate;
  44 
  45 /**
  46  * This is a test class created to check the operation of
  47  * the Pattern and Matcher classes.
  48  */
  49 public class RegExTest {
  50 
  51     private static Random generator = new Random();
  52     private static boolean failure = false;
  53     private static int failCount = 0;
  54     private static String firstFailure = null;
  55 


  58      *
  59      */
  60     public static void main(String[] args) throws Exception {
  61         // Most of the tests are in a file
  62         processFile("TestCases.txt");
  63         //processFile("PerlCases.txt");
  64         processFile("BMPTestCases.txt");
  65         processFile("SupplementaryTestCases.txt");
  66 
  67         // These test many randomly generated char patterns
  68         bm();
  69         slice();
  70 
  71         // These are hard to put into the file
  72         escapes();
  73         blankInput();
  74 
  75         // Substitition tests on randomly generated sequences
  76         globalSubstitute();
  77         stringbufferSubstitute();


  78         substitutionBasher();

  79 
  80         // Canonical Equivalence
  81         ceTest();
  82 
  83         // Anchors
  84         anchorTest();
  85 
  86         // boolean match calls
  87         matchesTest();
  88         lookingAtTest();
  89 
  90         // Pattern API
  91         patternMatchesTest();
  92 
  93         // Misc
  94         lookbehindTest();
  95         nullArgumentTest();
  96         backRefTest();
  97         groupCaptureTest();
  98         caretTest();


 279     private static boolean check(Runnable test) {
 280         try {
 281             test.run();
 282             failCount++;
 283             return false;
 284         } catch (NullPointerException npe) {
 285             return true;
 286         }
 287     }
 288 
 289     private static void nullArgumentTest() {
 290         check(new Runnable() { public void run() { Pattern.compile(null); }});
 291         check(new Runnable() { public void run() { Pattern.matches(null, null); }});
 292         check(new Runnable() { public void run() { Pattern.matches("xyz", null);}});
 293         check(new Runnable() { public void run() { Pattern.quote(null);}});
 294         check(new Runnable() { public void run() { Pattern.compile("xyz").split(null);}});
 295         check(new Runnable() { public void run() { Pattern.compile("xyz").matcher(null);}});
 296 
 297         final Matcher m = Pattern.compile("xyz").matcher("xyz");
 298         m.matches();
 299         check(new Runnable() { public void run() { m.appendTail(null);}});

 300         check(new Runnable() { public void run() { m.replaceAll(null);}});
 301         check(new Runnable() { public void run() { m.replaceFirst(null);}});
 302         check(new Runnable() { public void run() { m.appendReplacement(null, null);}});

 303         check(new Runnable() { public void run() { m.reset(null);}});
 304         check(new Runnable() { public void run() { Matcher.quoteReplacement(null);}});
 305         //check(new Runnable() { public void run() { m.usePattern(null);}});
 306 
 307         report("Null Argument");
 308     }
 309 
 310     // This is for bug6635133
 311     // Test if surrogate pair in Unicode escapes can be handled correctly.
 312     private static void surrogatesInClassTest() throws Exception {
 313         Pattern pattern = Pattern.compile("[\\ud834\\udd21-\\ud834\\udd24]");
 314         Matcher matcher = pattern.matcher("\ud834\udd22");
 315         if (!matcher.find())
 316             failCount++;
 317 
 318         report("Surrogate pair in Unicode escape");
 319     }
 320 
 321     // This is for bug6990617
 322     // Test if Pattern.RemoveQEQuoting works correctly if the octal unicode


2956         if (!result.toString().equals(toSupplementaries("zzzabwab5wef")))
2957             failCount++;
2958 
2959         // Check nothing has been appended into the output buffer if
2960         // the replacement string triggers IllegalArgumentException.
2961         p = Pattern.compile("(abc)");
2962         m = p.matcher("abcd");
2963         result = new StringBuffer();
2964         m.find();
2965         try {
2966             m.appendReplacement(result, ("xyz$g"));
2967             failCount++;
2968         } catch (IllegalArgumentException iae) {
2969             if (result.length() != 0)
2970                 failCount++;
2971         }
2972 
2973         report("SB Substitution");
2974     }
2975 
























































































































































































































































































2976     /*
2977      * 5 groups of characters are created to make a substitution string.
2978      * A base string will be created including random lead chars, the
2979      * substitution string, and random trailing chars.
2980      * A pattern containing the 5 groups is searched for and replaced with:
2981      * random group + random string + random group.
2982      * The results are checked for correctness.
2983      */
2984     private static void substitutionBasher() {
2985         for (int runs = 0; runs<1000; runs++) {
2986             // Create a base string to work in
2987             int leadingChars = generator.nextInt(10);
2988             StringBuffer baseBuffer = new StringBuffer(100);
2989             String leadingString = getRandomAlphaString(leadingChars);
2990             baseBuffer.append(leadingString);
2991 
2992             // Create 5 groups of random number of random chars
2993             // Create the string to substitute
2994             // Create the pattern string to search for
2995             StringBuffer bufferToSub = new StringBuffer(25);


3042             // Do the replacement
3043             String result = m.replaceAll(replacement);
3044 
3045             // Construct expected result
3046             StringBuffer bufferToRes = new StringBuffer();
3047             bufferToRes.append(leadingString);
3048             bufferToRes.append(groups[groupIndex1]);
3049             bufferToRes.append(randomMidString);
3050             bufferToRes.append(groups[groupIndex2]);
3051             bufferToRes.append(trailingString);
3052             String expectedResult = bufferToRes.toString();
3053 
3054             // Check results
3055             if (!result.equals(expectedResult))
3056                 failCount++;
3057         }
3058 
3059         report("Substitution Basher");
3060     }
3061 























































































3062     /**
3063      * Checks the handling of some escape sequences that the Pattern
3064      * class should process instead of the java compiler. These are
3065      * not in the file because the escapes should be be processed
3066      * by the Pattern class when the regex is compiled.
3067      */
3068     private static void escapes() throws Exception {
3069         Pattern p = Pattern.compile("\\043");
3070         Matcher m = p.matcher("#");
3071         if (!m.find())
3072             failCount++;
3073 
3074         p = Pattern.compile("\\x23");
3075         m = p.matcher("#");
3076         if (!m.find())
3077             failCount++;
3078 
3079         p = Pattern.compile("\\u0023");
3080         m = p.matcher("#");
3081         if (!m.find())




  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  * @summary tests RegExp framework
  27  * @author Mike McCloskey
  28  * @bug 4481568 4482696 4495089 4504687 4527731 4599621 4631553 4619345
  29  * 4630911 4672616 4711773 4727935 4750573 4792284 4803197 4757029 4808962
  30  * 4872664 4803179 4892980 4900747 4945394 4938995 4979006 4994840 4997476
  31  * 5013885 5003322 4988891 5098443 5110268 6173522 4829857 5027748 6376940
  32  * 6358731 6178785 6284152 6231989 6497148 6486934 6233084 6504326 6635133
  33  * 6350801 6676425 6878475 6919132 6931676 6948903 6990617 7014645 7039066
  34  * 7067045 7014640 7189363 8007395 8013252 8013254 8012646 8023647 6559590
  35  * 8027645 8035076 8039124
  36  */
  37 
  38 import java.util.regex.*;
  39 import java.util.Random;
  40 import java.io.*;
  41 import java.util.*;
  42 import java.nio.CharBuffer;
  43 import java.util.function.Predicate;
  44 
  45 /**
  46  * This is a test class created to check the operation of
  47  * the Pattern and Matcher classes.
  48  */
  49 public class RegExTest {
  50 
  51     private static Random generator = new Random();
  52     private static boolean failure = false;
  53     private static int failCount = 0;
  54     private static String firstFailure = null;
  55 


  58      *
  59      */
  60     public static void main(String[] args) throws Exception {
  61         // Most of the tests are in a file
  62         processFile("TestCases.txt");
  63         //processFile("PerlCases.txt");
  64         processFile("BMPTestCases.txt");
  65         processFile("SupplementaryTestCases.txt");
  66 
  67         // These test many randomly generated char patterns
  68         bm();
  69         slice();
  70 
  71         // These are hard to put into the file
  72         escapes();
  73         blankInput();
  74 
  75         // Substitition tests on randomly generated sequences
  76         globalSubstitute();
  77         stringbufferSubstitute();
  78         stringbuilderSubstitute();
  79 
  80         substitutionBasher();
  81         substitutionBasher2();
  82 
  83         // Canonical Equivalence
  84         ceTest();
  85 
  86         // Anchors
  87         anchorTest();
  88 
  89         // boolean match calls
  90         matchesTest();
  91         lookingAtTest();
  92 
  93         // Pattern API
  94         patternMatchesTest();
  95 
  96         // Misc
  97         lookbehindTest();
  98         nullArgumentTest();
  99         backRefTest();
 100         groupCaptureTest();
 101         caretTest();


 282     private static boolean check(Runnable test) {
 283         try {
 284             test.run();
 285             failCount++;
 286             return false;
 287         } catch (NullPointerException npe) {
 288             return true;
 289         }
 290     }
 291 
 292     private static void nullArgumentTest() {
 293         check(new Runnable() { public void run() { Pattern.compile(null); }});
 294         check(new Runnable() { public void run() { Pattern.matches(null, null); }});
 295         check(new Runnable() { public void run() { Pattern.matches("xyz", null);}});
 296         check(new Runnable() { public void run() { Pattern.quote(null);}});
 297         check(new Runnable() { public void run() { Pattern.compile("xyz").split(null);}});
 298         check(new Runnable() { public void run() { Pattern.compile("xyz").matcher(null);}});
 299 
 300         final Matcher m = Pattern.compile("xyz").matcher("xyz");
 301         m.matches();
 302         check(new Runnable() { public void run() { m.appendTail((StringBuffer)null);}});
 303         check(new Runnable() { public void run() { m.appendTail((StringBuilder)null);}});
 304         check(new Runnable() { public void run() { m.replaceAll(null);}});
 305         check(new Runnable() { public void run() { m.replaceFirst(null);}});
 306         check(new Runnable() { public void run() { m.appendReplacement((StringBuffer)null, null);}});
 307         check(new Runnable() { public void run() { m.appendReplacement((StringBuilder)null, null);}});
 308         check(new Runnable() { public void run() { m.reset(null);}});
 309         check(new Runnable() { public void run() { Matcher.quoteReplacement(null);}});
 310         //check(new Runnable() { public void run() { m.usePattern(null);}});
 311 
 312         report("Null Argument");
 313     }
 314 
 315     // This is for bug6635133
 316     // Test if surrogate pair in Unicode escapes can be handled correctly.
 317     private static void surrogatesInClassTest() throws Exception {
 318         Pattern pattern = Pattern.compile("[\\ud834\\udd21-\\ud834\\udd24]");
 319         Matcher matcher = pattern.matcher("\ud834\udd22");
 320         if (!matcher.find())
 321             failCount++;
 322 
 323         report("Surrogate pair in Unicode escape");
 324     }
 325 
 326     // This is for bug6990617
 327     // Test if Pattern.RemoveQEQuoting works correctly if the octal unicode


2961         if (!result.toString().equals(toSupplementaries("zzzabwab5wef")))
2962             failCount++;
2963 
2964         // Check nothing has been appended into the output buffer if
2965         // the replacement string triggers IllegalArgumentException.
2966         p = Pattern.compile("(abc)");
2967         m = p.matcher("abcd");
2968         result = new StringBuffer();
2969         m.find();
2970         try {
2971             m.appendReplacement(result, ("xyz$g"));
2972             failCount++;
2973         } catch (IllegalArgumentException iae) {
2974             if (result.length() != 0)
2975                 failCount++;
2976         }
2977 
2978         report("SB Substitution");
2979     }
2980 
2981     /**
2982      * Tests the usage of Matcher.appendReplacement() with literal
2983      * and group substitutions.
2984      */
2985     private static void stringbuilderSubstitute() throws Exception {
2986         // SB substitution with literal
2987         String blah = "zzzblahzzz";
2988         Pattern p = Pattern.compile("blah");
2989         Matcher m = p.matcher(blah);
2990         StringBuilder result = new StringBuilder();
2991         try {
2992             m.appendReplacement(result, "blech");
2993             failCount++;
2994         } catch (IllegalStateException e) {
2995         }
2996         m.find();
2997         m.appendReplacement(result, "blech");
2998         if (!result.toString().equals("zzzblech"))
2999             failCount++;
3000 
3001         m.appendTail(result);
3002         if (!result.toString().equals("zzzblechzzz"))
3003             failCount++;
3004 
3005         // SB substitution with groups
3006         blah = "zzzabcdzzz";
3007         p = Pattern.compile("(ab)(cd)*");
3008         m = p.matcher(blah);
3009         result = new StringBuilder();
3010         try {
3011             m.appendReplacement(result, "$1");
3012             failCount++;
3013         } catch (IllegalStateException e) {
3014         }
3015         m.find();
3016         m.appendReplacement(result, "$1");
3017         if (!result.toString().equals("zzzab"))
3018             failCount++;
3019 
3020         m.appendTail(result);
3021         if (!result.toString().equals("zzzabzzz"))
3022             failCount++;
3023 
3024         // SB substitution with 3 groups
3025         blah = "zzzabcdcdefzzz";
3026         p = Pattern.compile("(ab)(cd)*(ef)");
3027         m = p.matcher(blah);
3028         result = new StringBuilder();
3029         try {
3030             m.appendReplacement(result, "$1w$2w$3");
3031             failCount++;
3032         } catch (IllegalStateException e) {
3033         }
3034         m.find();
3035         m.appendReplacement(result, "$1w$2w$3");
3036         if (!result.toString().equals("zzzabwcdwef"))
3037             failCount++;
3038 
3039         m.appendTail(result);
3040         if (!result.toString().equals("zzzabwcdwefzzz"))
3041             failCount++;
3042 
3043         // SB substitution with groups and three matches
3044         // skipping middle match
3045         blah = "zzzabcdzzzabcddzzzabcdzzz";
3046         p = Pattern.compile("(ab)(cd*)");
3047         m = p.matcher(blah);
3048         result = new StringBuilder();
3049         try {
3050             m.appendReplacement(result, "$1");
3051             failCount++;
3052         } catch (IllegalStateException e) {
3053         }
3054         m.find();
3055         m.appendReplacement(result, "$1");
3056         if (!result.toString().equals("zzzab"))
3057             failCount++;
3058 
3059         m.find();
3060         m.find();
3061         m.appendReplacement(result, "$2");
3062         if (!result.toString().equals("zzzabzzzabcddzzzcd"))
3063             failCount++;
3064 
3065         m.appendTail(result);
3066         if (!result.toString().equals("zzzabzzzabcddzzzcdzzz"))
3067             failCount++;
3068 
3069         // Check to make sure escaped $ is ignored
3070         blah = "zzzabcdcdefzzz";
3071         p = Pattern.compile("(ab)(cd)*(ef)");
3072         m = p.matcher(blah);
3073         result = new StringBuilder();
3074         m.find();
3075         m.appendReplacement(result, "$1w\\$2w$3");
3076         if (!result.toString().equals("zzzabw$2wef"))
3077             failCount++;
3078 
3079         m.appendTail(result);
3080         if (!result.toString().equals("zzzabw$2wefzzz"))
3081             failCount++;
3082 
3083         // Check to make sure a reference to nonexistent group causes error
3084         blah = "zzzabcdcdefzzz";
3085         p = Pattern.compile("(ab)(cd)*(ef)");
3086         m = p.matcher(blah);
3087         result = new StringBuilder();
3088         m.find();
3089         try {
3090             m.appendReplacement(result, "$1w$5w$3");
3091             failCount++;
3092         } catch (IndexOutOfBoundsException ioobe) {
3093             // Correct result
3094         }
3095 
3096         // Check double digit group references
3097         blah = "zzz123456789101112zzz";
3098         p = Pattern.compile("(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)");
3099         m = p.matcher(blah);
3100         result = new StringBuilder();
3101         m.find();
3102         m.appendReplacement(result, "$1w$11w$3");
3103         if (!result.toString().equals("zzz1w11w3"))
3104             failCount++;
3105 
3106         // Check to make sure it backs off $15 to $1 if only three groups
3107         blah = "zzzabcdcdefzzz";
3108         p = Pattern.compile("(ab)(cd)*(ef)");
3109         m = p.matcher(blah);
3110         result = new StringBuilder();
3111         m.find();
3112         m.appendReplacement(result, "$1w$15w$3");
3113         if (!result.toString().equals("zzzabwab5wef"))
3114             failCount++;
3115 
3116 
3117         // Supplementary character test
3118         // SB substitution with literal
3119         blah = toSupplementaries("zzzblahzzz");
3120         p = Pattern.compile(toSupplementaries("blah"));
3121         m = p.matcher(blah);
3122         result = new StringBuilder();
3123         try {
3124             m.appendReplacement(result, toSupplementaries("blech"));
3125             failCount++;
3126         } catch (IllegalStateException e) {
3127         }
3128         m.find();
3129         m.appendReplacement(result, toSupplementaries("blech"));
3130         if (!result.toString().equals(toSupplementaries("zzzblech")))
3131             failCount++;
3132         m.appendTail(result);
3133         if (!result.toString().equals(toSupplementaries("zzzblechzzz")))
3134             failCount++;
3135 
3136         // SB substitution with groups
3137         blah = toSupplementaries("zzzabcdzzz");
3138         p = Pattern.compile(toSupplementaries("(ab)(cd)*"));
3139         m = p.matcher(blah);
3140         result = new StringBuilder();
3141         try {
3142             m.appendReplacement(result, "$1");
3143             failCount++;
3144         } catch (IllegalStateException e) {
3145         }
3146         m.find();
3147         m.appendReplacement(result, "$1");
3148         if (!result.toString().equals(toSupplementaries("zzzab")))
3149             failCount++;
3150 
3151         m.appendTail(result);
3152         if (!result.toString().equals(toSupplementaries("zzzabzzz")))
3153             failCount++;
3154 
3155         // SB substitution with 3 groups
3156         blah = toSupplementaries("zzzabcdcdefzzz");
3157         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
3158         m = p.matcher(blah);
3159         result = new StringBuilder();
3160         try {
3161             m.appendReplacement(result, toSupplementaries("$1w$2w$3"));
3162             failCount++;
3163         } catch (IllegalStateException e) {
3164         }
3165         m.find();
3166         m.appendReplacement(result, toSupplementaries("$1w$2w$3"));
3167         if (!result.toString().equals(toSupplementaries("zzzabwcdwef")))
3168             failCount++;
3169 
3170         m.appendTail(result);
3171         if (!result.toString().equals(toSupplementaries("zzzabwcdwefzzz")))
3172             failCount++;
3173 
3174         // SB substitution with groups and three matches
3175         // skipping middle match
3176         blah = toSupplementaries("zzzabcdzzzabcddzzzabcdzzz");
3177         p = Pattern.compile(toSupplementaries("(ab)(cd*)"));
3178         m = p.matcher(blah);
3179         result = new StringBuilder();
3180         try {
3181             m.appendReplacement(result, "$1");
3182             failCount++;
3183         } catch (IllegalStateException e) {
3184         }
3185         m.find();
3186         m.appendReplacement(result, "$1");
3187         if (!result.toString().equals(toSupplementaries("zzzab")))
3188             failCount++;
3189 
3190         m.find();
3191         m.find();
3192         m.appendReplacement(result, "$2");
3193         if (!result.toString().equals(toSupplementaries("zzzabzzzabcddzzzcd")))
3194             failCount++;
3195 
3196         m.appendTail(result);
3197         if (!result.toString().equals(toSupplementaries("zzzabzzzabcddzzzcdzzz")))
3198             failCount++;
3199 
3200         // Check to make sure escaped $ is ignored
3201         blah = toSupplementaries("zzzabcdcdefzzz");
3202         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
3203         m = p.matcher(blah);
3204         result = new StringBuilder();
3205         m.find();
3206         m.appendReplacement(result, toSupplementaries("$1w\\$2w$3"));
3207         if (!result.toString().equals(toSupplementaries("zzzabw$2wef")))
3208             failCount++;
3209 
3210         m.appendTail(result);
3211         if (!result.toString().equals(toSupplementaries("zzzabw$2wefzzz")))
3212             failCount++;
3213 
3214         // Check to make sure a reference to nonexistent group causes error
3215         blah = toSupplementaries("zzzabcdcdefzzz");
3216         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
3217         m = p.matcher(blah);
3218         result = new StringBuilder();
3219         m.find();
3220         try {
3221             m.appendReplacement(result, toSupplementaries("$1w$5w$3"));
3222             failCount++;
3223         } catch (IndexOutOfBoundsException ioobe) {
3224             // Correct result
3225         }
3226         // Check double digit group references
3227         blah = toSupplementaries("zzz123456789101112zzz");
3228         p = Pattern.compile("(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)");
3229         m = p.matcher(blah);
3230         result = new StringBuilder();
3231         m.find();
3232         m.appendReplacement(result, toSupplementaries("$1w$11w$3"));
3233         if (!result.toString().equals(toSupplementaries("zzz1w11w3")))
3234             failCount++;
3235 
3236         // Check to make sure it backs off $15 to $1 if only three groups
3237         blah = toSupplementaries("zzzabcdcdefzzz");
3238         p = Pattern.compile(toSupplementaries("(ab)(cd)*(ef)"));
3239         m = p.matcher(blah);
3240         result = new StringBuilder();
3241         m.find();
3242         m.appendReplacement(result, toSupplementaries("$1w$15w$3"));
3243         if (!result.toString().equals(toSupplementaries("zzzabwab5wef")))
3244             failCount++;
3245         // Check nothing has been appended into the output buffer if
3246         // the replacement string triggers IllegalArgumentException.
3247         p = Pattern.compile("(abc)");
3248         m = p.matcher("abcd");
3249         result = new StringBuilder();
3250         m.find();
3251         try {
3252             m.appendReplacement(result, ("xyz$g"));
3253             failCount++;
3254         } catch (IllegalArgumentException iae) {
3255             if (result.length() != 0)
3256                 failCount++;
3257         }
3258         report("SB Substitution 2");
3259     }
3260 
3261     /*
3262      * 5 groups of characters are created to make a substitution string.
3263      * A base string will be created including random lead chars, the
3264      * substitution string, and random trailing chars.
3265      * A pattern containing the 5 groups is searched for and replaced with:
3266      * random group + random string + random group.
3267      * The results are checked for correctness.
3268      */
3269     private static void substitutionBasher() {
3270         for (int runs = 0; runs<1000; runs++) {
3271             // Create a base string to work in
3272             int leadingChars = generator.nextInt(10);
3273             StringBuffer baseBuffer = new StringBuffer(100);
3274             String leadingString = getRandomAlphaString(leadingChars);
3275             baseBuffer.append(leadingString);
3276 
3277             // Create 5 groups of random number of random chars
3278             // Create the string to substitute
3279             // Create the pattern string to search for
3280             StringBuffer bufferToSub = new StringBuffer(25);


3327             // Do the replacement
3328             String result = m.replaceAll(replacement);
3329 
3330             // Construct expected result
3331             StringBuffer bufferToRes = new StringBuffer();
3332             bufferToRes.append(leadingString);
3333             bufferToRes.append(groups[groupIndex1]);
3334             bufferToRes.append(randomMidString);
3335             bufferToRes.append(groups[groupIndex2]);
3336             bufferToRes.append(trailingString);
3337             String expectedResult = bufferToRes.toString();
3338 
3339             // Check results
3340             if (!result.equals(expectedResult))
3341                 failCount++;
3342         }
3343 
3344         report("Substitution Basher");
3345     }
3346 
3347     /*
3348      * 5 groups of characters are created to make a substitution string.
3349      * A base string will be created including random lead chars, the
3350      * substitution string, and random trailing chars.
3351      * A pattern containing the 5 groups is searched for and replaced with:
3352      * random group + random string + random group.
3353      * The results are checked for correctness.
3354      */
3355     private static void substitutionBasher2() {
3356         for (int runs = 0; runs<1000; runs++) {
3357             // Create a base string to work in
3358             int leadingChars = generator.nextInt(10);
3359             StringBuilder baseBuffer = new StringBuilder(100);
3360             String leadingString = getRandomAlphaString(leadingChars);
3361             baseBuffer.append(leadingString);
3362 
3363             // Create 5 groups of random number of random chars
3364             // Create the string to substitute
3365             // Create the pattern string to search for
3366             StringBuilder bufferToSub = new StringBuilder(25);
3367             StringBuilder bufferToPat = new StringBuilder(50);
3368             String[] groups = new String[5];
3369             for(int i=0; i<5; i++) {
3370                 int aGroupSize = generator.nextInt(5)+1;
3371                 groups[i] = getRandomAlphaString(aGroupSize);
3372                 bufferToSub.append(groups[i]);
3373                 bufferToPat.append('(');
3374                 bufferToPat.append(groups[i]);
3375                 bufferToPat.append(')');
3376             }
3377             String stringToSub = bufferToSub.toString();
3378             String pattern = bufferToPat.toString();
3379 
3380             // Place sub string into working string at random index
3381             baseBuffer.append(stringToSub);
3382 
3383             // Append random chars to end
3384             int trailingChars = generator.nextInt(10);
3385             String trailingString = getRandomAlphaString(trailingChars);
3386             baseBuffer.append(trailingString);
3387             String baseString = baseBuffer.toString();
3388 
3389             // Create test pattern and matcher
3390             Pattern p = Pattern.compile(pattern);
3391             Matcher m = p.matcher(baseString);
3392 
3393             // Reject candidate if pattern happens to start early
3394             m.find();
3395             if (m.start() < leadingChars)
3396                 continue;
3397 
3398             // Reject candidate if more than one match
3399             if (m.find())
3400                 continue;
3401 
3402             // Construct a replacement string with :
3403             // random group + random string + random group
3404             StringBuilder bufferToRep = new StringBuilder();
3405             int groupIndex1 = generator.nextInt(5);
3406             bufferToRep.append("$" + (groupIndex1 + 1));
3407             String randomMidString = getRandomAlphaString(5);
3408             bufferToRep.append(randomMidString);
3409             int groupIndex2 = generator.nextInt(5);
3410             bufferToRep.append("$" + (groupIndex2 + 1));
3411             String replacement = bufferToRep.toString();
3412 
3413             // Do the replacement
3414             String result = m.replaceAll(replacement);
3415 
3416             // Construct expected result
3417             StringBuilder bufferToRes = new StringBuilder();
3418             bufferToRes.append(leadingString);
3419             bufferToRes.append(groups[groupIndex1]);
3420             bufferToRes.append(randomMidString);
3421             bufferToRes.append(groups[groupIndex2]);
3422             bufferToRes.append(trailingString);
3423             String expectedResult = bufferToRes.toString();
3424 
3425             // Check results
3426             if (!result.equals(expectedResult)) {
3427                 failCount++;
3428             }
3429         }
3430 
3431         report("Substitution Basher 2");
3432     }
3433 
3434     /**
3435      * Checks the handling of some escape sequences that the Pattern
3436      * class should process instead of the java compiler. These are
3437      * not in the file because the escapes should be be processed
3438      * by the Pattern class when the regex is compiled.
3439      */
3440     private static void escapes() throws Exception {
3441         Pattern p = Pattern.compile("\\043");
3442         Matcher m = p.matcher("#");
3443         if (!m.find())
3444             failCount++;
3445 
3446         p = Pattern.compile("\\x23");
3447         m = p.matcher("#");
3448         if (!m.find())
3449             failCount++;
3450 
3451         p = Pattern.compile("\\u0023");
3452         m = p.matcher("#");
3453         if (!m.find())