src/com/sun/org/apache/regexp/internal/RETest.java

changeset 2116
aaee9ae4799a
parent 2090
3b8ebb957957
parent 2115
ba503169016f
child 2117
a5f920b6d2b5
     1.1 --- a/src/com/sun/org/apache/regexp/internal/RETest.java	Sat Oct 24 16:18:47 2020 +0800
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,883 +0,0 @@
     1.4 -/*
     1.5 - * reserved comment block
     1.6 - * DO NOT REMOVE OR ALTER!
     1.7 - */
     1.8 -/*
     1.9 - * Copyright 1999-2004 The Apache Software Foundation.
    1.10 - *
    1.11 - * Licensed under the Apache License, Version 2.0 (the "License");
    1.12 - * you may not use this file except in compliance with the License.
    1.13 - * You may obtain a copy of the License at
    1.14 - *
    1.15 - *     http://www.apache.org/licenses/LICENSE-2.0
    1.16 - *
    1.17 - * Unless required by applicable law or agreed to in writing, software
    1.18 - * distributed under the License is distributed on an "AS IS" BASIS,
    1.19 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.20 - * See the License for the specific language governing permissions and
    1.21 - * limitations under the License.
    1.22 - */
    1.23 -
    1.24 -package com.sun.org.apache.regexp.internal;
    1.25 -
    1.26 -import java.io.BufferedReader;
    1.27 -import java.io.FileReader;
    1.28 -import java.io.InputStreamReader;
    1.29 -import java.io.PrintWriter;
    1.30 -import java.io.File;
    1.31 -import java.io.ByteArrayOutputStream;
    1.32 -import java.io.ObjectOutputStream;
    1.33 -import java.io.ByteArrayInputStream;
    1.34 -import java.io.ObjectInputStream;
    1.35 -import java.io.StringBufferInputStream;
    1.36 -import java.io.StringReader;
    1.37 -import java.io.IOException;
    1.38 -
    1.39 -/**
    1.40 - * Data driven (and optionally interactive) testing harness to exercise regular
    1.41 - * expression compiler and matching engine.
    1.42 - *
    1.43 - * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
    1.44 - * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    1.45 - * @author <a href="mailto:gholam@xtra.co.nz">Michael McCallum</a>
    1.46 - */
    1.47 -public class RETest
    1.48 -{
    1.49 -    // True if we want to see output from success cases
    1.50 -    static final boolean showSuccesses = false;
    1.51 -
    1.52 -    // A new line character.
    1.53 -    static final String NEW_LINE = System.getProperty( "line.separator" );
    1.54 -
    1.55 -    // Construct a debug compiler
    1.56 -    REDebugCompiler compiler = new REDebugCompiler();
    1.57 -
    1.58 -    /**
    1.59 -     * Main program entrypoint.  If an argument is given, it will be compiled
    1.60 -     * and interactive matching will ensue.  If no argument is given, the
    1.61 -     * file RETest.txt will be used as automated testing input.
    1.62 -     * @param args Command line arguments (optional regular expression)
    1.63 -     */
    1.64 -    public static void main(String[] args)
    1.65 -    {
    1.66 -        try
    1.67 -        {
    1.68 -            if (!test( args )) {
    1.69 -                System.exit(1);
    1.70 -            }
    1.71 -        }
    1.72 -        catch (Exception e)
    1.73 -        {
    1.74 -            e.printStackTrace();
    1.75 -            System.exit(1);
    1.76 -        }
    1.77 -    }
    1.78 -
    1.79 -    /**
    1.80 -     * Testing entrypoint.
    1.81 -     * @param args Command line arguments
    1.82 -     * @exception Exception thrown in case of error
    1.83 -     */
    1.84 -    public static boolean test( String[] args ) throws Exception
    1.85 -    {
    1.86 -        RETest test = new RETest();
    1.87 -        // Run interactive tests against a single regexp
    1.88 -        if (args.length == 2)
    1.89 -        {
    1.90 -            test.runInteractiveTests(args[1]);
    1.91 -        }
    1.92 -        else if (args.length == 1)
    1.93 -        {
    1.94 -            // Run automated tests
    1.95 -            test.runAutomatedTests(args[0]);
    1.96 -        }
    1.97 -        else
    1.98 -        {
    1.99 -            System.out.println( "Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])" );
   1.100 -            System.out.println( "By Default will run automated tests from file 'docs/RETest.txt' ..." );
   1.101 -            System.out.println();
   1.102 -            test.runAutomatedTests("docs/RETest.txt");
   1.103 -        }
   1.104 -        return test.failures == 0;
   1.105 -    }
   1.106 -
   1.107 -    /**
   1.108 -     * Constructor
   1.109 -     */
   1.110 -    public RETest()
   1.111 -    {
   1.112 -    }
   1.113 -
   1.114 -    /**
   1.115 -     * Compile and test matching against a single expression
   1.116 -     * @param expr Expression to compile and test
   1.117 -     */
   1.118 -    void runInteractiveTests(String expr)
   1.119 -    {
   1.120 -        RE r = new RE();
   1.121 -        try
   1.122 -        {
   1.123 -            // Compile expression
   1.124 -            r.setProgram(compiler.compile(expr));
   1.125 -
   1.126 -            // Show expression
   1.127 -            say("" + NEW_LINE + "" + expr + "" + NEW_LINE + "");
   1.128 -
   1.129 -            // Show program for compiled expression
   1.130 -            PrintWriter writer = new PrintWriter( System.out );
   1.131 -            compiler.dumpProgram( writer );
   1.132 -            writer.flush();
   1.133 -
   1.134 -            boolean running = true;
   1.135 -            // Test matching against compiled expression
   1.136 -            while ( running )
   1.137 -            {
   1.138 -                // Read from keyboard
   1.139 -                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   1.140 -                System.out.print("> ");
   1.141 -                System.out.flush();
   1.142 -                String match = br.readLine();
   1.143 -
   1.144 -                if ( match != null )
   1.145 -                {
   1.146 -                    // Try a match against the keyboard input
   1.147 -                    if (r.match(match))
   1.148 -                    {
   1.149 -                        say("Match successful.");
   1.150 -                    }
   1.151 -                    else
   1.152 -                    {
   1.153 -                        say("Match failed.");
   1.154 -                    }
   1.155 -
   1.156 -                    // Show subparen registers
   1.157 -                    showParens(r);
   1.158 -                }
   1.159 -                else
   1.160 -                {
   1.161 -                    running = false;
   1.162 -                    System.out.println();
   1.163 -                }
   1.164 -            }
   1.165 -        }
   1.166 -        catch (Exception e)
   1.167 -        {
   1.168 -            say("Error: " + e.toString());
   1.169 -            e.printStackTrace();
   1.170 -        }
   1.171 -    }
   1.172 -
   1.173 -    /**
   1.174 -     * Exit with a fatal error.
   1.175 -     * @param s Last famous words before exiting
   1.176 -     */
   1.177 -    void die(String s)
   1.178 -    {
   1.179 -        say("FATAL ERROR: " + s);
   1.180 -        System.exit(-1);
   1.181 -    }
   1.182 -
   1.183 -    /**
   1.184 -     * Fail with an error. Will print a big failure message to System.out.
   1.185 -     *
   1.186 -     * @param log Output before failure
   1.187 -     * @param s Failure description
   1.188 -     */
   1.189 -    void fail(StringBuffer log, String s)
   1.190 -    {
   1.191 -        System.out.print(log.toString());
   1.192 -        fail(s);
   1.193 -    }
   1.194 -
   1.195 -    /**
   1.196 -     * Fail with an error. Will print a big failure message to System.out.
   1.197 -     *
   1.198 -     * @param s Failure description
   1.199 -     */
   1.200 -    void fail(String s)
   1.201 -    {
   1.202 -        failures++;
   1.203 -        say("" + NEW_LINE + "");
   1.204 -        say("*******************************************************");
   1.205 -        say("*********************  FAILURE!  **********************");
   1.206 -        say("*******************************************************");
   1.207 -        say("" + NEW_LINE + "");
   1.208 -        say(s);
   1.209 -        say("");
   1.210 -        // make sure the writer gets flushed.
   1.211 -        if (compiler != null) {
   1.212 -            PrintWriter writer = new PrintWriter( System.out );
   1.213 -            compiler.dumpProgram( writer );
   1.214 -            writer.flush();
   1.215 -            say("" + NEW_LINE + "");
   1.216 -        }
   1.217 -    }
   1.218 -
   1.219 -    /**
   1.220 -     * Say something to standard out
   1.221 -     * @param s What to say
   1.222 -     */
   1.223 -    void say(String s)
   1.224 -    {
   1.225 -        System.out.println(s);
   1.226 -    }
   1.227 -
   1.228 -    /**
   1.229 -     * Dump parenthesized subexpressions found by a regular expression matcher object
   1.230 -     * @param r Matcher object with results to show
   1.231 -     */
   1.232 -    void showParens(RE r)
   1.233 -    {
   1.234 -        // Loop through each paren
   1.235 -        for (int i = 0; i < r.getParenCount(); i++)
   1.236 -        {
   1.237 -            // Show paren register
   1.238 -            say("$" + i + " = " + r.getParen(i));
   1.239 -        }
   1.240 -    }
   1.241 -
   1.242 -    /*
   1.243 -     * number in automated test
   1.244 -     */
   1.245 -    int testCount = 0;
   1.246 -
   1.247 -    /*
   1.248 -     * Count of failures in automated test
   1.249 -     */
   1.250 -    int failures = 0;
   1.251 -
   1.252 -    /**
   1.253 -     * Run automated tests in RETest.txt file (from Perl 4.0 test battery)
   1.254 -     * @exception Exception thrown in case of error
   1.255 -     */
   1.256 -    void runAutomatedTests(String testDocument) throws Exception
   1.257 -    {
   1.258 -        long ms = System.currentTimeMillis();
   1.259 -
   1.260 -        // Some unit tests
   1.261 -        testPrecompiledRE();
   1.262 -        testSplitAndGrep();
   1.263 -        testSubst();
   1.264 -        testOther();
   1.265 -
   1.266 -        // Test from script file
   1.267 -        File testInput = new File(testDocument);
   1.268 -        if (! testInput.exists()) {
   1.269 -            throw new Exception ("Could not find: " + testDocument);
   1.270 -        }
   1.271 -
   1.272 -        BufferedReader br = new BufferedReader(new FileReader(testInput));
   1.273 -        try
   1.274 -        {
   1.275 -            // While input is available, parse lines
   1.276 -            while (br.ready())
   1.277 -            {
   1.278 -                RETestCase testcase = getNextTestCase(br);
   1.279 -                if (testcase != null) {
   1.280 -                    testcase.runTest();
   1.281 -                }
   1.282 -            }
   1.283 -        }
   1.284 -        finally
   1.285 -        {
   1.286 -            br.close();
   1.287 -        }
   1.288 -
   1.289 -        // Show match time
   1.290 -        say(NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - ms) + " ms.");
   1.291 -
   1.292 -        // Print final results
   1.293 -        if (failures > 0) {
   1.294 -            say("*************** THERE ARE FAILURES! *******************");
   1.295 -        }
   1.296 -        say("Tests complete.  " + testCount + " tests, " + failures + " failure(s).");
   1.297 -    }
   1.298 -
   1.299 -    /**
   1.300 -     * Run automated unit test
   1.301 -     * @exception Exception thrown in case of error
   1.302 -     */
   1.303 -    void testOther() throws Exception
   1.304 -    {
   1.305 -        // Serialization test 1: Compile regexp and serialize/deserialize it
   1.306 -        RE r = new RE("(a*)b");
   1.307 -        say("Serialized/deserialized (a*)b");
   1.308 -        ByteArrayOutputStream out = new ByteArrayOutputStream(128);
   1.309 -        new ObjectOutputStream(out).writeObject(r);
   1.310 -        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
   1.311 -        r = (RE)new ObjectInputStream(in).readObject();
   1.312 -        if (!r.match("aaab"))
   1.313 -        {
   1.314 -            fail("Did not match 'aaab' with deserialized RE.");
   1.315 -        } else {
   1.316 -            say("aaaab = true");
   1.317 -            showParens(r);
   1.318 -        }
   1.319 -
   1.320 -        // Serialization test 2: serialize/deserialize used regexp
   1.321 -        out.reset();
   1.322 -        say("Deserialized (a*)b");
   1.323 -        new ObjectOutputStream(out).writeObject(r);
   1.324 -        in = new ByteArrayInputStream(out.toByteArray());
   1.325 -        r = (RE)new ObjectInputStream(in).readObject();
   1.326 -        if (r.getParenCount() != 0)
   1.327 -        {
   1.328 -            fail("Has parens after deserialization.");
   1.329 -        }
   1.330 -        if (!r.match("aaab"))
   1.331 -        {
   1.332 -            fail("Did not match 'aaab' with deserialized RE.");
   1.333 -        } else {
   1.334 -            say("aaaab = true");
   1.335 -            showParens(r);
   1.336 -        }
   1.337 -
   1.338 -        // Test MATCH_CASEINDEPENDENT
   1.339 -        r = new RE("abc(\\w*)");
   1.340 -        say("MATCH_CASEINDEPENDENT abc(\\w*)");
   1.341 -        r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
   1.342 -        say("abc(d*)");
   1.343 -        if (!r.match("abcddd"))
   1.344 -        {
   1.345 -            fail("Did not match 'abcddd'.");
   1.346 -        } else {
   1.347 -            say("abcddd = true");
   1.348 -            showParens(r);
   1.349 -        }
   1.350 -
   1.351 -        if (!r.match("aBcDDdd"))
   1.352 -        {
   1.353 -            fail("Did not match 'aBcDDdd'.");
   1.354 -        } else {
   1.355 -            say("aBcDDdd = true");
   1.356 -            showParens(r);
   1.357 -        }
   1.358 -
   1.359 -        if (!r.match("ABCDDDDD"))
   1.360 -        {
   1.361 -            fail("Did not match 'ABCDDDDD'.");
   1.362 -        } else {
   1.363 -            say("ABCDDDDD = true");
   1.364 -            showParens(r);
   1.365 -        }
   1.366 -
   1.367 -        r = new RE("(A*)b\\1");
   1.368 -        r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
   1.369 -        if (!r.match("AaAaaaBAAAAAA"))
   1.370 -        {
   1.371 -            fail("Did not match 'AaAaaaBAAAAAA'.");
   1.372 -        } else {
   1.373 -            say("AaAaaaBAAAAAA = true");
   1.374 -            showParens(r);
   1.375 -        }
   1.376 -
   1.377 -        r = new RE("[A-Z]*");
   1.378 -        r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
   1.379 -        if (!r.match("CaBgDe12"))
   1.380 -        {
   1.381 -            fail("Did not match 'CaBgDe12'.");
   1.382 -        } else {
   1.383 -            say("CaBgDe12 = true");
   1.384 -            showParens(r);
   1.385 -        }
   1.386 -
   1.387 -        // Test MATCH_MULTILINE. Test for eol/bol symbols.
   1.388 -        r = new RE("^abc$", RE.MATCH_MULTILINE);
   1.389 -        if (!r.match("\nabc")) {
   1.390 -            fail("\"\\nabc\" doesn't match \"^abc$\"");
   1.391 -        }
   1.392 -        if (!r.match("\rabc")) {
   1.393 -            fail("\"\\rabc\" doesn't match \"^abc$\"");
   1.394 -        }
   1.395 -        if (!r.match("\r\nabc")) {
   1.396 -            fail("\"\\r\\nabc\" doesn't match \"^abc$\"");
   1.397 -        }
   1.398 -        if (!r.match("\u0085abc")) {
   1.399 -            fail("\"\\u0085abc\" doesn't match \"^abc$\"");
   1.400 -        }
   1.401 -        if (!r.match("\u2028abc")) {
   1.402 -            fail("\"\\u2028abc\" doesn't match \"^abc$\"");
   1.403 -        }
   1.404 -        if (!r.match("\u2029abc")) {
   1.405 -            fail("\"\\u2029abc\" doesn't match \"^abc$\"");
   1.406 -        }
   1.407 -
   1.408 -        // Test MATCH_MULTILINE. Test that '.' does not matches new line.
   1.409 -        r = new RE("^a.*b$", RE.MATCH_MULTILINE);
   1.410 -        if (r.match("a\nb")) {
   1.411 -            fail("\"a\\nb\" matches \"^a.*b$\"");
   1.412 -        }
   1.413 -        if (r.match("a\rb")) {
   1.414 -            fail("\"a\\rb\" matches \"^a.*b$\"");
   1.415 -        }
   1.416 -        if (r.match("a\r\nb")) {
   1.417 -            fail("\"a\\r\\nb\" matches \"^a.*b$\"");
   1.418 -        }
   1.419 -        if (r.match("a\u0085b")) {
   1.420 -            fail("\"a\\u0085b\" matches \"^a.*b$\"");
   1.421 -        }
   1.422 -        if (r.match("a\u2028b")) {
   1.423 -            fail("\"a\\u2028b\" matches \"^a.*b$\"");
   1.424 -        }
   1.425 -        if (r.match("a\u2029b")) {
   1.426 -            fail("\"a\\u2029b\" matches \"^a.*b$\"");
   1.427 -        }
   1.428 -    }
   1.429 -
   1.430 -    private void testPrecompiledRE()
   1.431 -    {
   1.432 -        // Pre-compiled regular expression "a*b"
   1.433 -        char[] re1Instructions =
   1.434 -        {
   1.435 -            0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
   1.436 -            0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
   1.437 -            0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
   1.438 -            0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
   1.439 -            0x0000,
   1.440 -        };
   1.441 -
   1.442 -        REProgram re1 = new REProgram(re1Instructions);
   1.443 -
   1.444 -        // Simple test of pre-compiled regular expressions
   1.445 -        RE r = new RE(re1);
   1.446 -        say("a*b");
   1.447 -        boolean result = r.match("aaab");
   1.448 -        say("aaab = " + result);
   1.449 -        showParens(r);
   1.450 -        if (!result) {
   1.451 -            fail("\"aaab\" doesn't match to precompiled \"a*b\"");
   1.452 -        }
   1.453 -
   1.454 -        result = r.match("b");
   1.455 -        say("b = " + result);
   1.456 -        showParens(r);
   1.457 -        if (!result) {
   1.458 -            fail("\"b\" doesn't match to precompiled \"a*b\"");
   1.459 -        }
   1.460 -
   1.461 -        result = r.match("c");
   1.462 -        say("c = " + result);
   1.463 -        showParens(r);
   1.464 -        if (result) {
   1.465 -            fail("\"c\" matches to precompiled \"a*b\"");
   1.466 -        }
   1.467 -
   1.468 -        result = r.match("ccccaaaaab");
   1.469 -        say("ccccaaaaab = " + result);
   1.470 -        showParens(r);
   1.471 -        if (!result) {
   1.472 -            fail("\"ccccaaaaab\" doesn't match to precompiled \"a*b\"");
   1.473 -        }
   1.474 -    }
   1.475 -
   1.476 -    private void testSplitAndGrep()
   1.477 -    {
   1.478 -        String[] expected = {"xxxx", "xxxx", "yyyy", "zzz"};
   1.479 -        RE r = new RE("a*b");
   1.480 -        String[] s = r.split("xxxxaabxxxxbyyyyaaabzzz");
   1.481 -        for (int i = 0; i < expected.length && i < s.length; i++) {
   1.482 -            assertEquals("Wrong splitted part", expected[i], s[i]);
   1.483 -        }
   1.484 -        assertEquals("Wrong number of splitted parts", expected.length,
   1.485 -                     s.length);
   1.486 -
   1.487 -        r = new RE("x+");
   1.488 -        expected = new String[] {"xxxx", "xxxx"};
   1.489 -        s = r.grep(s);
   1.490 -        for (int i = 0; i < s.length; i++)
   1.491 -        {
   1.492 -            say("s[" + i + "] = " + s[i]);
   1.493 -            assertEquals("Grep fails", expected[i], s[i]);
   1.494 -        }
   1.495 -        assertEquals("Wrong number of string found by grep", expected.length,
   1.496 -                     s.length);
   1.497 -    }
   1.498 -
   1.499 -    private void testSubst()
   1.500 -    {
   1.501 -        RE r = new RE("a*b");
   1.502 -        String expected = "-foo-garply-wacky-";
   1.503 -        String actual = r.subst("aaaabfooaaabgarplyaaabwackyb", "-");
   1.504 -        assertEquals("Wrong result of substitution in \"a*b\"", expected, actual);
   1.505 -
   1.506 -        // Test subst() with backreferences
   1.507 -        r = new RE("http://[\\.\\w\\-\\?/~_@&=%]+");
   1.508 -        actual = r.subst("visit us: http://www.apache.org!",
   1.509 -                         "1234<a href=\"$0\">$0</a>", RE.REPLACE_BACKREFERENCES);
   1.510 -        assertEquals("Wrong subst() result", "visit us: 1234<a href=\"http://www.apache.org\">http://www.apache.org</a>!", actual);
   1.511 -
   1.512 -        // Test subst() with backreferences without leading characters
   1.513 -        // before first backreference
   1.514 -        r = new RE("(.*?)=(.*)");
   1.515 -        actual = r.subst("variable=value",
   1.516 -                         "$1_test_$212", RE.REPLACE_BACKREFERENCES);
   1.517 -        assertEquals("Wrong subst() result", "variable_test_value12", actual);
   1.518 -
   1.519 -        // Test subst() with NO backreferences
   1.520 -        r = new RE("^a$");
   1.521 -        actual = r.subst("a",
   1.522 -                         "b", RE.REPLACE_BACKREFERENCES);
   1.523 -        assertEquals("Wrong subst() result", "b", actual);
   1.524 -
   1.525 -        // Test subst() with NO backreferences
   1.526 -        r = new RE("^a$", RE.MATCH_MULTILINE);
   1.527 -        actual = r.subst("\r\na\r\n",
   1.528 -                         "b", RE.REPLACE_BACKREFERENCES);
   1.529 -        assertEquals("Wrong subst() result", "\r\nb\r\n", actual);
   1.530 -    }
   1.531 -
   1.532 -    public void assertEquals(String message, String expected, String actual)
   1.533 -    {
   1.534 -        if (expected != null && !expected.equals(actual)
   1.535 -            || actual != null && !actual.equals(expected))
   1.536 -        {
   1.537 -            fail(message + " (expected \"" + expected
   1.538 -                 + "\", actual \"" + actual + "\")");
   1.539 -        }
   1.540 -    }
   1.541 -
   1.542 -    public void assertEquals(String message, int expected, int actual)
   1.543 -    {
   1.544 -        if (expected != actual) {
   1.545 -            fail(message + " (expected \"" + expected
   1.546 -                 + "\", actual \"" + actual + "\")");
   1.547 -        }
   1.548 -    }
   1.549 -
   1.550 -    /**
   1.551 -     * Converts yesno string to boolean.
   1.552 -     * @param yesno string representation of expected result
   1.553 -     * @return true if yesno is "YES", false if yesno is "NO"
   1.554 -     *         stops program otherwise.
   1.555 -     */
   1.556 -    private boolean getExpectedResult(String yesno)
   1.557 -    {
   1.558 -        if ("NO".equals(yesno))
   1.559 -        {
   1.560 -            return false;
   1.561 -        }
   1.562 -        else if ("YES".equals(yesno))
   1.563 -        {
   1.564 -            return true;
   1.565 -        }
   1.566 -        else
   1.567 -        {
   1.568 -            // Bad test script
   1.569 -            die("Test script error!");
   1.570 -            return false; //to please javac
   1.571 -        }
   1.572 -    }
   1.573 -
   1.574 -    /**
   1.575 -     * Finds next test description in a given script.
   1.576 -     * @param br <code>BufferedReader</code> for a script file
   1.577 -     * @return strign tag for next test description
   1.578 -     * @exception IOException if some io problems occured
   1.579 -     */
   1.580 -    private String findNextTest(BufferedReader br) throws IOException
   1.581 -    {
   1.582 -        String number = "";
   1.583 -
   1.584 -        while (br.ready())
   1.585 -        {
   1.586 -            number = br.readLine();
   1.587 -            if (number == null)
   1.588 -            {
   1.589 -                break;
   1.590 -            }
   1.591 -            number = number.trim();
   1.592 -            if (number.startsWith("#"))
   1.593 -            {
   1.594 -                break;
   1.595 -            }
   1.596 -            if (!number.equals(""))
   1.597 -            {
   1.598 -                say("Script error.  Line = " + number);
   1.599 -                System.exit(-1);
   1.600 -            }
   1.601 -        }
   1.602 -        return number;
   1.603 -    }
   1.604 -
   1.605 -    /**
   1.606 -     * Creates testcase for the next test description in the script file.
   1.607 -     * @param br <code>BufferedReader</code> for script file.
   1.608 -     * @return a new tescase or null.
   1.609 -     * @exception IOException if some io problems occured
   1.610 -     */
   1.611 -    private RETestCase getNextTestCase(BufferedReader br) throws IOException
   1.612 -    {
   1.613 -        // Find next re test case
   1.614 -        final String tag = findNextTest(br);
   1.615 -
   1.616 -        // Are we done?
   1.617 -        if (!br.ready())
   1.618 -        {
   1.619 -            return null;
   1.620 -        }
   1.621 -
   1.622 -        // Get expression
   1.623 -        final String expr = br.readLine();
   1.624 -
   1.625 -        // Get test information
   1.626 -        final String matchAgainst = br.readLine();
   1.627 -        final boolean badPattern = "ERR".equals(matchAgainst);
   1.628 -        boolean shouldMatch = false;
   1.629 -        int expectedParenCount = 0;
   1.630 -        String[] expectedParens = null;
   1.631 -
   1.632 -        if (!badPattern) {
   1.633 -            shouldMatch = getExpectedResult(br.readLine().trim());
   1.634 -            if (shouldMatch) {
   1.635 -                expectedParenCount = Integer.parseInt(br.readLine().trim());
   1.636 -                expectedParens = new String[expectedParenCount];
   1.637 -                for (int i = 0; i < expectedParenCount; i++) {
   1.638 -                    expectedParens[i] = br.readLine();
   1.639 -                }
   1.640 -            }
   1.641 -        }
   1.642 -
   1.643 -        return new RETestCase(this, tag, expr, matchAgainst, badPattern,
   1.644 -                              shouldMatch, expectedParens);
   1.645 -    }
   1.646 -}
   1.647 -
   1.648 -final class RETestCase
   1.649 -{
   1.650 -    final private StringBuffer log = new StringBuffer();
   1.651 -    final private int number;
   1.652 -    final private String tag; // number from script file
   1.653 -    final private String pattern;
   1.654 -    final private String toMatch;
   1.655 -    final private boolean badPattern;
   1.656 -    final private boolean shouldMatch;
   1.657 -    final private String[] parens;
   1.658 -    final private RETest test;
   1.659 -    private RE regexp;
   1.660 -
   1.661 -    public RETestCase(RETest test, String tag, String pattern,
   1.662 -                      String toMatch, boolean badPattern,
   1.663 -                      boolean shouldMatch, String[] parens)
   1.664 -    {
   1.665 -        this.number = ++test.testCount;
   1.666 -        this.test = test;
   1.667 -        this.tag = tag;
   1.668 -        this.pattern = pattern;
   1.669 -        this.toMatch = toMatch;
   1.670 -        this.badPattern = badPattern;
   1.671 -        this.shouldMatch = shouldMatch;
   1.672 -        if (parens != null) {
   1.673 -            this.parens = new String[parens.length];
   1.674 -            for (int i = 0; i < parens.length; i++) {
   1.675 -                this.parens[i] = parens[i];
   1.676 -            }
   1.677 -        } else {
   1.678 -            this.parens = null;
   1.679 -        }
   1.680 -    }
   1.681 -
   1.682 -    public void runTest()
   1.683 -    {
   1.684 -        test.say(tag + "(" + number + "): " + pattern);
   1.685 -        if (testCreation()) {
   1.686 -            testMatch();
   1.687 -        }
   1.688 -    }
   1.689 -
   1.690 -    boolean testCreation()
   1.691 -    {
   1.692 -        try
   1.693 -        {
   1.694 -            // Compile it
   1.695 -            regexp = new RE();
   1.696 -            regexp.setProgram(test.compiler.compile(pattern));
   1.697 -            // Expression didn't cause an expected error
   1.698 -            if (badPattern)
   1.699 -            {
   1.700 -                test.fail(log, "Was expected to be an error, but wasn't.");
   1.701 -                return false;
   1.702 -            }
   1.703 -
   1.704 -            return true;
   1.705 -        }
   1.706 -        // Some expressions *should* cause exceptions to be thrown
   1.707 -        catch (Exception e)
   1.708 -        {
   1.709 -            // If it was supposed to be an error, report success and continue
   1.710 -            if (badPattern)
   1.711 -            {
   1.712 -                log.append("   Match: ERR\n");
   1.713 -                success("Produces an error (" + e.toString() + "), as expected.");
   1.714 -                return false;
   1.715 -            }
   1.716 -
   1.717 -            // Wasn't supposed to be an error
   1.718 -            String message = (e.getMessage() == null) ? e.toString() : e.getMessage();
   1.719 -            test.fail(log, "Produces an unexpected exception \"" + message + "\"");
   1.720 -            e.printStackTrace();
   1.721 -        }
   1.722 -        catch (Error e)
   1.723 -        {
   1.724 -            // Internal error happened
   1.725 -            test.fail(log, "Compiler threw fatal error \"" + e.getMessage() + "\"");
   1.726 -            e.printStackTrace();
   1.727 -        }
   1.728 -
   1.729 -        return false;
   1.730 -    }
   1.731 -
   1.732 -    private void testMatch()
   1.733 -    {
   1.734 -        log.append("   Match against: '" + toMatch + "'\n");
   1.735 -        // Try regular matching
   1.736 -        try
   1.737 -        {
   1.738 -            // Match against the string
   1.739 -            boolean result = regexp.match(toMatch);
   1.740 -            log.append("   Matched: " + (result ? "YES" : "NO") + "\n");
   1.741 -
   1.742 -            // Check result, parens, and iterators
   1.743 -            if (checkResult(result) && (!shouldMatch || checkParens()))
   1.744 -            {
   1.745 -                // test match(CharacterIterator, int)
   1.746 -                // for every CharacterIterator implementation.
   1.747 -                log.append("   Match using StringCharacterIterator\n");
   1.748 -                if (!tryMatchUsingCI(new StringCharacterIterator(toMatch)))
   1.749 -                    return;
   1.750 -
   1.751 -                log.append("   Match using CharacterArrayCharacterIterator\n");
   1.752 -                if (!tryMatchUsingCI(new CharacterArrayCharacterIterator(toMatch.toCharArray(), 0, toMatch.length())))
   1.753 -                    return;
   1.754 -
   1.755 -                log.append("   Match using StreamCharacterIterator\n");
   1.756 -                if (!tryMatchUsingCI(new StreamCharacterIterator(new StringBufferInputStream(toMatch))))
   1.757 -                    return;
   1.758 -
   1.759 -                log.append("   Match using ReaderCharacterIterator\n");
   1.760 -                if (!tryMatchUsingCI(new ReaderCharacterIterator(new StringReader(toMatch))))
   1.761 -                    return;
   1.762 -            }
   1.763 -        }
   1.764 -        // Matcher blew it
   1.765 -        catch(Exception e)
   1.766 -        {
   1.767 -            test.fail(log, "Matcher threw exception: " + e.toString());
   1.768 -            e.printStackTrace();
   1.769 -        }
   1.770 -        // Internal error
   1.771 -        catch(Error e)
   1.772 -        {
   1.773 -            test.fail(log, "Matcher threw fatal error \"" + e.getMessage() + "\"");
   1.774 -            e.printStackTrace();
   1.775 -        }
   1.776 -    }
   1.777 -
   1.778 -    private boolean checkResult(boolean result)
   1.779 -    {
   1.780 -        // Write status
   1.781 -        if (result == shouldMatch) {
   1.782 -            success((shouldMatch ? "Matched" : "Did not match")
   1.783 -                    + " \"" + toMatch + "\", as expected:");
   1.784 -            return true;
   1.785 -        } else {
   1.786 -            if (shouldMatch) {
   1.787 -                test.fail(log, "Did not match \"" + toMatch + "\", when expected to.");
   1.788 -            } else {
   1.789 -                test.fail(log, "Matched \"" + toMatch + "\", when not expected to.");
   1.790 -            }
   1.791 -            return false;
   1.792 -        }
   1.793 -    }
   1.794 -
   1.795 -    private boolean checkParens()
   1.796 -    {
   1.797 -        // Show subexpression registers
   1.798 -        if (RETest.showSuccesses)
   1.799 -        {
   1.800 -            test.showParens(regexp);
   1.801 -        }
   1.802 -
   1.803 -        log.append("   Paren count: " + regexp.getParenCount() + "\n");
   1.804 -        if (!assertEquals(log, "Wrong number of parens", parens.length, regexp.getParenCount()))
   1.805 -        {
   1.806 -            return false;
   1.807 -        }
   1.808 -
   1.809 -        // Check registers against expected contents
   1.810 -        for (int p = 0; p < regexp.getParenCount(); p++)
   1.811 -        {
   1.812 -            log.append("   Paren " + p + ": " + regexp.getParen(p) + "\n");
   1.813 -
   1.814 -            // Compare expected result with actual
   1.815 -            if ("null".equals(parens[p]) && regexp.getParen(p) == null)
   1.816 -            {
   1.817 -                // Consider "null" in test file equal to null
   1.818 -                continue;
   1.819 -            }
   1.820 -            if (!assertEquals(log, "Wrong register " + p, parens[p], regexp.getParen(p)))
   1.821 -            {
   1.822 -                return false;
   1.823 -            }
   1.824 -        }
   1.825 -
   1.826 -        return true;
   1.827 -    }
   1.828 -
   1.829 -    boolean tryMatchUsingCI(CharacterIterator matchAgainst)
   1.830 -    {
   1.831 -        try {
   1.832 -            boolean result = regexp.match(matchAgainst, 0);
   1.833 -            log.append("   Match: " + (result ? "YES" : "NO") + "\n");
   1.834 -            return checkResult(result) && (!shouldMatch || checkParens());
   1.835 -        }
   1.836 -        // Matcher blew it
   1.837 -        catch(Exception e)
   1.838 -        {
   1.839 -            test.fail(log, "Matcher threw exception: " + e.toString());
   1.840 -            e.printStackTrace();
   1.841 -        }
   1.842 -        // Internal error
   1.843 -        catch(Error e)
   1.844 -        {
   1.845 -            test.fail(log, "Matcher threw fatal error \"" + e.getMessage() + "\"");
   1.846 -            e.printStackTrace();
   1.847 -        }
   1.848 -        return false;
   1.849 -    }
   1.850 -
   1.851 -    public boolean assertEquals(StringBuffer log, String message, String expected, String actual)
   1.852 -    {
   1.853 -        if (expected != null && !expected.equals(actual)
   1.854 -            || actual != null && !actual.equals(expected))
   1.855 -        {
   1.856 -            test.fail(log, message + " (expected \"" + expected
   1.857 -                      + "\", actual \"" + actual + "\")");
   1.858 -            return false;
   1.859 -        }
   1.860 -        return true;
   1.861 -    }
   1.862 -
   1.863 -    public boolean assertEquals(StringBuffer log, String message, int expected, int actual)
   1.864 -    {
   1.865 -        if (expected != actual) {
   1.866 -            test.fail(log, message + " (expected \"" + expected
   1.867 -                      + "\", actual \"" + actual + "\")");
   1.868 -            return false;
   1.869 -        }
   1.870 -        return true;
   1.871 -    }
   1.872 -
   1.873 -    /**
   1.874 -     * Show a success
   1.875 -     * @param s Success story
   1.876 -     */
   1.877 -    void success(String s)
   1.878 -    {
   1.879 -        if (RETest.showSuccesses)
   1.880 -        {
   1.881 -            test.say("" + RETest.NEW_LINE + "-----------------------" + RETest.NEW_LINE + "");
   1.882 -            test.say("Expression #" + (number) + " \"" + pattern + "\" ");
   1.883 -            test.say("Success: " + s);
   1.884 -        }
   1.885 -    }
   1.886 -}

mercurial