/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Data driven (and optionally interactive) testing harness to exercise regular
* expression compiler and matching engine.
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:gholam@xtra.co.nz">Michael McCallum</a>
*/
public class RETest
{
// True if we want to see output from success cases
static final boolean showSuccesses = false;
// A new line character.
// Construct a debug compiler
/**
* Main program entrypoint. If an argument is given, it will be compiled
* and interactive matching will ensue. If no argument is given, the
* file RETest.txt will be used as automated testing input.
* @param args Command line arguments (optional regular expression)
*/
{
try
{
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Testing entrypoint.
* @param args Command line arguments
* @exception Exception thrown in case of error
*/
{
// Run interactive tests against a single regexp
{
}
{
// Run automated tests
}
else
{
}
}
/**
* Constructor
*/
public RETest()
{
}
/**
* Compile and test matching against a single expression
* @param expr Expression to compile and test
*/
{
try
{
// Compile expression
// Show expression
// Show program for compiled expression
boolean running = true;
// Test matching against compiled expression
while ( running )
{
// Read from keyboard
{
// Try a match against the keyboard input
{
say("Match successful.");
}
else
{
say("Match failed.");
}
// Show subparen registers
showParens(r);
}
else
{
running = false;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Exit with a fatal error.
* @param s Last famous words before exiting
*/
{
say("FATAL ERROR: " + s);
}
/**
* Fail with an error. Will print a big failure message to System.out.
*
* @param log Output before failure
* @param s Failure description
*/
{
fail(s);
}
/**
* Fail with an error. Will print a big failure message to System.out.
*
* @param s Failure description
*/
{
failures++;
say("*******************************************************");
say("********************* FAILURE! **********************");
say("*******************************************************");
say(s);
say("");
// make sure the writer gets flushed.
}
}
/**
* Say something to standard out
* @param s What to say
*/
{
}
/**
* Dump parenthesized subexpressions found by a regular expression matcher object
* @param r Matcher object with results to show
*/
{
// Loop through each paren
for (int i = 0; i < r.getParenCount(); i++)
{
// Show paren register
}
}
/*
* number in automated test
*/
/*
* Count of failures in automated test
*/
/**
* Run automated tests in RETest.txt file (from Perl 4.0 test battery)
* @exception Exception thrown in case of error
*/
{
// Some unit tests
testSubst();
testOther();
// Test from script file
}
try
{
// While input is available, parse lines
{
}
}
}
finally
{
}
// Show match time
// Print final results
if (failures > 0) {
say("*************** THERE ARE FAILURES! *******************");
}
}
/**
* Run automated unit test
* @exception Exception thrown in case of error
*/
{
// Serialization test 1: Compile regexp and serialize/deserialize it
say("Serialized/deserialized (a*)b");
if (!r.match("aaab"))
{
fail("Did not match 'aaab' with deserialized RE.");
} else {
say("aaaab = true");
showParens(r);
}
// Serialization test 2: serialize/deserialize used regexp
say("Deserialized (a*)b");
if (r.getParenCount() != 0)
{
fail("Has parens after deserialization.");
}
if (!r.match("aaab"))
{
fail("Did not match 'aaab' with deserialized RE.");
} else {
say("aaaab = true");
showParens(r);
}
// Test MATCH_CASEINDEPENDENT
r = new RE("abc(\\w*)");
say("MATCH_CASEINDEPENDENT abc(\\w*)");
say("abc(d*)");
if (!r.match("abcddd"))
{
fail("Did not match 'abcddd'.");
} else {
say("abcddd = true");
showParens(r);
}
if (!r.match("aBcDDdd"))
{
fail("Did not match 'aBcDDdd'.");
} else {
say("aBcDDdd = true");
showParens(r);
}
if (!r.match("ABCDDDDD"))
{
fail("Did not match 'ABCDDDDD'.");
} else {
say("ABCDDDDD = true");
showParens(r);
}
r = new RE("(A*)b\\1");
if (!r.match("AaAaaaBAAAAAA"))
{
fail("Did not match 'AaAaaaBAAAAAA'.");
} else {
say("AaAaaaBAAAAAA = true");
showParens(r);
}
r = new RE("[A-Z]*");
if (!r.match("CaBgDe12"))
{
fail("Did not match 'CaBgDe12'.");
} else {
say("CaBgDe12 = true");
showParens(r);
}
if (!r.match("\nabc")) {
fail("\"\\nabc\" doesn't match \"^abc$\"");
}
if (!r.match("\rabc")) {
fail("\"\\rabc\" doesn't match \"^abc$\"");
}
if (!r.match("\r\nabc")) {
fail("\"\\r\\nabc\" doesn't match \"^abc$\"");
}
if (!r.match("\u0085abc")) {
fail("\"\\u0085abc\" doesn't match \"^abc$\"");
}
if (!r.match("\u2028abc")) {
fail("\"\\u2028abc\" doesn't match \"^abc$\"");
}
if (!r.match("\u2029abc")) {
fail("\"\\u2029abc\" doesn't match \"^abc$\"");
}
// Test MATCH_MULTILINE. Test that '.' does not matches new line.
if (r.match("a\nb")) {
fail("\"a\\nb\" matches \"^a.*b$\"");
}
if (r.match("a\rb")) {
fail("\"a\\rb\" matches \"^a.*b$\"");
}
if (r.match("a\r\nb")) {
fail("\"a\\r\\nb\" matches \"^a.*b$\"");
}
if (r.match("a\u0085b")) {
fail("\"a\\u0085b\" matches \"^a.*b$\"");
}
if (r.match("a\u2028b")) {
fail("\"a\\u2028b\" matches \"^a.*b$\"");
}
if (r.match("a\u2029b")) {
fail("\"a\\u2029b\" matches \"^a.*b$\"");
}
}
private void testPrecompiledRE()
{
// Pre-compiled regular expression "a*b"
char[] re1Instructions =
{
0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
0x0000,
};
// Simple test of pre-compiled regular expressions
say("a*b");
showParens(r);
if (!result) {
fail("\"aaab\" doesn't match to precompiled \"a*b\"");
}
showParens(r);
if (!result) {
fail("\"b\" doesn't match to precompiled \"a*b\"");
}
showParens(r);
if (result) {
fail("\"c\" matches to precompiled \"a*b\"");
}
showParens(r);
if (!result) {
fail("\"ccccaaaaab\" doesn't match to precompiled \"a*b\"");
}
}
private void testSplitAndGrep()
{
}
s.length);
r = new RE("x+");
s = r.grep(s);
for (int i = 0; i < s.length; i++)
{
}
s.length);
}
private void testSubst()
{
// Test subst() with backreferences
r = new RE("http://[\\.\\w\\-\\?/~_@&=%]+");
assertEquals("Wrong subst() result", "visit us: 1234<a href=\"http://www.apache.org\">http://www.apache.org</a>!", actual);
// Test subst() with backreferences without leading characters
// before first backreference
r = new RE("(.*?)=(.*)");
// Test subst() with NO backreferences
r = new RE("^a$");
// Test subst() with NO backreferences
}
{
{
}
}
{
}
}
/**
* Converts yesno string to boolean.
* @param yesno string representation of expected result
* @return true if yesno is "YES", false if yesno is "NO"
* stops program otherwise.
*/
{
{
return false;
}
{
return true;
}
else
{
// Bad test script
die("Test script error!");
return false; //to please javac
}
}
/**
* Finds next test description in a given script.
* @param br <code>BufferedReader</code> for a script file
* @return strign tag for next test description
* @exception IOException if some io problems occured
*/
{
{
{
break;
}
{
break;
}
{
}
}
return number;
}
/**
* Creates testcase for the next test description in the script file.
* @param br <code>BufferedReader</code> for script file.
* @return a new tescase or null.
* @exception IOException if some io problems occured
*/
{
// Find next re test case
// Are we done?
{
return null;
}
// Get expression
// Get test information
boolean shouldMatch = false;
int expectedParenCount = 0;
if (!badPattern) {
if (shouldMatch) {
for (int i = 0; i < expectedParenCount; i++) {
}
}
}
}
}
final class RETestCase
{
final private int number;
final private boolean badPattern;
final private boolean shouldMatch;
{
this.badPattern = badPattern;
this.shouldMatch = shouldMatch;
}
} else {
}
}
public void runTest()
{
if (testCreation()) {
testMatch();
}
}
boolean testCreation()
{
try
{
// Compile it
// Expression didn't cause an expected error
if (badPattern)
{
return false;
}
return true;
}
// Some expressions *should* cause exceptions to be thrown
catch (Exception e)
{
// If it was supposed to be an error, report success and continue
if (badPattern)
{
return false;
}
// Wasn't supposed to be an error
e.printStackTrace();
}
catch (Error e)
{
// Internal error happened
e.printStackTrace();
}
return false;
}
private void testMatch()
{
// Try regular matching
try
{
// Match against the string
// Check result, parens, and iterators
{
// test match(CharacterIterator, int)
// for every CharacterIterator implementation.
return;
if (!tryMatchUsingCI(new CharacterArrayCharacterIterator(toMatch.toCharArray(), 0, toMatch.length())))
return;
return;
return;
}
}
// Matcher blew it
catch(Exception e)
{
e.printStackTrace();
}
// Internal error
catch(Error e)
{
e.printStackTrace();
}
}
{
// Write status
if (result == shouldMatch) {
return true;
} else {
if (shouldMatch) {
} else {
}
return false;
}
}
private boolean checkParens()
{
// Show subexpression registers
if (RETest.showSuccesses)
{
}
{
return false;
}
// Check registers against expected contents
{
// Compare expected result with actual
{
// Consider "null" in test file equal to null
continue;
}
{
return false;
}
}
return true;
}
{
try {
}
// Matcher blew it
catch(Exception e)
{
e.printStackTrace();
}
// Internal error
catch(Error e)
{
e.printStackTrace();
}
return false;
}
{
{
return false;
}
return true;
}
{
return false;
}
return true;
}
/**
* Show a success
* @param s Success story
*/
{
if (RETest.showSuccesses)
{
}
}
}