RETestpublic class RETest extends Object Data driven (and optionally interactive) testing harness to exercise regular
expression compiler and matching engine. |
Fields Summary |
---|
static final boolean | showSuccesses | static final String | NEW_LINE | REDebugCompiler | compiler | int | testCount | int | failures |
Constructors Summary |
---|
public RETest()Constructor
|
Methods Summary |
---|
public void | assertEquals(java.lang.String message, java.lang.String expected, java.lang.String actual)
if (expected != null && !expected.equals(actual)
|| actual != null && !actual.equals(expected))
{
fail(message + " (expected \"" + expected
+ "\", actual \"" + actual + "\")");
}
| public void | assertEquals(java.lang.String message, int expected, int actual)
if (expected != actual) {
fail(message + " (expected \"" + expected
+ "\", actual \"" + actual + "\")");
}
| void | die(java.lang.String s)Exit with a fatal error.
say("FATAL ERROR: " + s);
System.exit(-1);
| void | fail(java.lang.StringBuffer log, java.lang.String s)Fail with an error. Will print a big failure message to System.out.
System.out.print(log.toString());
fail(s);
| void | fail(java.lang.String s)Fail with an error. Will print a big failure message to System.out.
failures++;
say("" + NEW_LINE + "");
say("*******************************************************");
say("********************* FAILURE! **********************");
say("*******************************************************");
say("" + NEW_LINE + "");
say(s);
say("");
// make sure the writer gets flushed.
if (compiler != null) {
PrintWriter writer = new PrintWriter( System.out );
compiler.dumpProgram( writer );
writer.flush();
say("" + NEW_LINE + "");
}
| private java.lang.String | findNextTest(java.io.BufferedReader br)Finds next test description in a given script.
String number = "";
while (br.ready())
{
number = br.readLine();
if (number == null)
{
break;
}
number = number.trim();
if (number.startsWith("#"))
{
break;
}
if (!number.equals(""))
{
say("Script error. Line = " + number);
System.exit(-1);
}
}
return number;
| private boolean | getExpectedResult(java.lang.String yesno)Converts yesno string to boolean.
if ("NO".equals(yesno))
{
return false;
}
else if ("YES".equals(yesno))
{
return true;
}
else
{
// Bad test script
die("Test script error!");
return false; //to please javac
}
| private com.sun.org.apache.regexp.internal.RETestCase | getNextTestCase(java.io.BufferedReader br)Creates testcase for the next test description in the script file.
// Find next re test case
final String tag = findNextTest(br);
// Are we done?
if (!br.ready())
{
return null;
}
// Get expression
final String expr = br.readLine();
// Get test information
final String matchAgainst = br.readLine();
final boolean badPattern = "ERR".equals(matchAgainst);
boolean shouldMatch = false;
int expectedParenCount = 0;
String[] expectedParens = null;
if (!badPattern) {
shouldMatch = getExpectedResult(br.readLine().trim());
if (shouldMatch) {
expectedParenCount = Integer.parseInt(br.readLine().trim());
expectedParens = new String[expectedParenCount];
for (int i = 0; i < expectedParenCount; i++) {
expectedParens[i] = br.readLine();
}
}
}
return new RETestCase(this, tag, expr, matchAgainst, badPattern,
shouldMatch, expectedParens);
| public static void | main(java.lang.String[] args)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.
try
{
if (!test( args )) {
System.exit(1);
}
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
| void | runAutomatedTests(java.lang.String testDocument)Run automated tests in RETest.txt file (from Perl 4.0 test battery)
long ms = System.currentTimeMillis();
// Some unit tests
testPrecompiledRE();
testSplitAndGrep();
testSubst();
testOther();
// Test from script file
File testInput = new File(testDocument);
if (! testInput.exists()) {
throw new Exception ("Could not find: " + testDocument);
}
BufferedReader br = new BufferedReader(new FileReader(testInput));
try
{
// While input is available, parse lines
while (br.ready())
{
RETestCase testcase = getNextTestCase(br);
if (testcase != null) {
testcase.runTest();
}
}
}
finally
{
br.close();
}
// Show match time
say(NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - ms) + " ms.");
// Print final results
if (failures > 0) {
say("*************** THERE ARE FAILURES! *******************");
}
say("Tests complete. " + testCount + " tests, " + failures + " failure(s).");
| void | runInteractiveTests(java.lang.String expr)Compile and test matching against a single expression
RE r = new RE();
try
{
// Compile expression
r.setProgram(compiler.compile(expr));
// Show expression
say("" + NEW_LINE + "" + expr + "" + NEW_LINE + "");
// Show program for compiled expression
PrintWriter writer = new PrintWriter( System.out );
compiler.dumpProgram( writer );
writer.flush();
boolean running = true;
// Test matching against compiled expression
while ( running )
{
// Read from keyboard
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("> ");
System.out.flush();
String match = br.readLine();
if ( match != null )
{
// Try a match against the keyboard input
if (r.match(match))
{
say("Match successful.");
}
else
{
say("Match failed.");
}
// Show subparen registers
showParens(r);
}
else
{
running = false;
System.out.println();
}
}
}
catch (Exception e)
{
say("Error: " + e.toString());
e.printStackTrace();
}
| void | say(java.lang.String s)Say something to standard out
System.out.println(s);
| void | showParens(RE r)Dump parenthesized subexpressions found by a regular expression matcher object
// Loop through each paren
for (int i = 0; i < r.getParenCount(); i++)
{
// Show paren register
say("$" + i + " = " + r.getParen(i));
}
| public static boolean | test(java.lang.String[] args)Testing entrypoint.
RETest test = new RETest();
// Run interactive tests against a single regexp
if (args.length == 2)
{
test.runInteractiveTests(args[1]);
}
else if (args.length == 1)
{
// Run automated tests
test.runAutomatedTests(args[0]);
}
else
{
System.out.println( "Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])" );
System.out.println( "By Default will run automated tests from file 'docs/RETest.txt' ..." );
System.out.println();
test.runAutomatedTests("docs/RETest.txt");
}
return test.failures == 0;
| void | testOther()Run automated unit test
// Serialization test 1: Compile regexp and serialize/deserialize it
RE r = new RE("(a*)b");
say("Serialized/deserialized (a*)b");
ByteArrayOutputStream out = new ByteArrayOutputStream(128);
new ObjectOutputStream(out).writeObject(r);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
r = (RE)new ObjectInputStream(in).readObject();
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
out.reset();
say("Deserialized (a*)b");
new ObjectOutputStream(out).writeObject(r);
in = new ByteArrayInputStream(out.toByteArray());
r = (RE)new ObjectInputStream(in).readObject();
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*)");
r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
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");
r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
if (!r.match("AaAaaaBAAAAAA"))
{
fail("Did not match 'AaAaaaBAAAAAA'.");
} else {
say("AaAaaaBAAAAAA = true");
showParens(r);
}
r = new RE("[A-Z]*");
r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
if (!r.match("CaBgDe12"))
{
fail("Did not match 'CaBgDe12'.");
} else {
say("CaBgDe12 = true");
showParens(r);
}
// Test MATCH_MULTILINE. Test for eol/bol symbols.
r = new RE("^abc$", RE.MATCH_MULTILINE);
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.
r = new RE("^a.*b$", RE.MATCH_MULTILINE);
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,
};
REProgram re1 = new REProgram(re1Instructions);
// Simple test of pre-compiled regular expressions
RE r = new RE(re1);
say("a*b");
boolean result = r.match("aaab");
say("aaab = " + result);
showParens(r);
if (!result) {
fail("\"aaab\" doesn't match to precompiled \"a*b\"");
}
result = r.match("b");
say("b = " + result);
showParens(r);
if (!result) {
fail("\"b\" doesn't match to precompiled \"a*b\"");
}
result = r.match("c");
say("c = " + result);
showParens(r);
if (result) {
fail("\"c\" matches to precompiled \"a*b\"");
}
result = r.match("ccccaaaaab");
say("ccccaaaaab = " + result);
showParens(r);
if (!result) {
fail("\"ccccaaaaab\" doesn't match to precompiled \"a*b\"");
}
| private void | testSplitAndGrep()
String[] expected = {"xxxx", "xxxx", "yyyy", "zzz"};
RE r = new RE("a*b");
String[] s = r.split("xxxxaabxxxxbyyyyaaabzzz");
for (int i = 0; i < expected.length && i < s.length; i++) {
assertEquals("Wrong splitted part", expected[i], s[i]);
}
assertEquals("Wrong number of splitted parts", expected.length,
s.length);
r = new RE("x+");
expected = new String[] {"xxxx", "xxxx"};
s = r.grep(s);
for (int i = 0; i < s.length; i++)
{
say("s[" + i + "] = " + s[i]);
assertEquals("Grep fails", expected[i], s[i]);
}
assertEquals("Wrong number of string found by grep", expected.length,
s.length);
| private void | testSubst()
RE r = new RE("a*b");
String expected = "-foo-garply-wacky-";
String actual = r.subst("aaaabfooaaabgarplyaaabwackyb", "-");
assertEquals("Wrong result of substitution in \"a*b\"", expected, actual);
// Test subst() with backreferences
r = new RE("http://[\\.\\w\\-\\?/~_@&=%]+");
actual = r.subst("visit us: http://www.apache.org!",
"1234<a href=\"$0\">$0</a>", RE.REPLACE_BACKREFERENCES);
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("(.*?)=(.*)");
actual = r.subst("variable=value",
"$1_test_$212", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "variable_test_value12", actual);
// Test subst() with NO backreferences
r = new RE("^a$");
actual = r.subst("a",
"b", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "b", actual);
// Test subst() with NO backreferences
r = new RE("^a$", RE.MATCH_MULTILINE);
actual = r.subst("\r\na\r\n",
"b", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "\r\nb\r\n", actual);
|
|