home *** CD-ROM | disk | FTP | other *** search
- package org.apache.regexp;
-
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileReader;
- import java.io.InputStreamReader;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.PrintWriter;
-
- public class RETest {
- static final boolean showSuccesses = false;
- static final String NEW_LINE = System.getProperty("line.separator");
- // $FF: renamed from: r org.apache.regexp.RE
- RE field_0 = new RE();
- REDebugCompiler compiler = new REDebugCompiler();
- char[] re1Instructions = new char[]{'|', '\u0000', '\u001a', '|', '\u0000', '\r', 'A', '\u0001', '\u0004', 'a', '|', '\u0000', '\u0003', 'G', '\u0000', '\ufff6', '|', '\u0000', '\u0003', 'N', '\u0000', '\u0003', 'A', '\u0001', '\u0004', 'b', 'E', '\u0000', '\u0000'};
- REProgram re1;
- String expr;
- // $FF: renamed from: n int
- int field_1;
- int failures;
-
- public static void main(String[] var0) {
- try {
- test(var0);
- } catch (Exception var2) {
- ((Throwable)var2).printStackTrace();
- }
-
- }
-
- public static boolean test(String[] var0) throws Exception {
- RETest var1 = new RETest();
- if (var0.length == 2) {
- var1.runInteractiveTests(var0[1]);
- } else if (var0.length == 1) {
- var1.runAutomatedTests(var0[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();
- var1.runAutomatedTests("docs/RETest.txt");
- }
-
- return var1.failures == 0;
- }
-
- public RETest() {
- this.re1 = new REProgram(this.re1Instructions);
- this.field_1 = 0;
- this.failures = 0;
- }
-
- void runInteractiveTests(String var1) {
- try {
- this.field_0.setProgram(this.compiler.compile(var1));
- this.say("" + NEW_LINE + "" + var1 + "" + NEW_LINE + "");
- PrintWriter var2 = new PrintWriter(System.out);
- this.compiler.dumpProgram(var2);
- var2.flush();
- boolean var3 = true;
-
- while(var3) {
- BufferedReader var4 = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("> ");
- System.out.flush();
- String var5 = var4.readLine();
- if (var5 != null) {
- if (this.field_0.match(var5)) {
- this.say("Match successful.");
- } else {
- this.say("Match failed.");
- }
-
- this.showParens(this.field_0);
- } else {
- var3 = false;
- System.out.println();
- }
- }
- } catch (Exception var6) {
- this.say("Error: " + ((Throwable)var6).toString());
- ((Throwable)var6).printStackTrace();
- }
-
- }
-
- void die(String var1) {
- this.say("FATAL ERROR: " + var1);
- System.exit(0);
- }
-
- void fail(String var1) {
- ++this.failures;
- this.say("" + NEW_LINE + "");
- this.say("*******************************************************");
- this.say("********************* FAILURE! **********************");
- this.say("*******************************************************");
- this.say("" + NEW_LINE + "");
- this.say(var1);
- this.say("");
- PrintWriter var2 = new PrintWriter(System.out);
- this.compiler.dumpProgram(var2);
- var2.flush();
- this.say("" + NEW_LINE + "");
- }
-
- void success(String var1) {
- }
-
- void say(String var1) {
- System.out.println(var1);
- }
-
- void show() {
- this.say("" + NEW_LINE + "-----------------------" + NEW_LINE + "");
- this.say("Expression #" + this.field_1 + " \"" + this.expr + "\" ");
- }
-
- void showParens(RE var1) {
- for(int var2 = 0; var2 < var1.getParenCount(); ++var2) {
- this.say("$" + var2 + " = " + var1.getParen(var2));
- }
-
- }
-
- void runAutomatedTests(String var1) throws Exception {
- long var2 = System.currentTimeMillis();
- RE var4 = new RE(this.re1);
- this.say("a*b");
- this.say("aaaab = " + var4.match("aaab"));
- this.showParens(var4);
- this.say("b = " + var4.match("b"));
- this.showParens(var4);
- this.say("c = " + var4.match("c"));
- this.showParens(var4);
- this.say("ccccaaaaab = " + var4.match("ccccaaaaab"));
- this.showParens(var4);
- var4 = new RE("a*b");
- String[] var5 = var4.split("xxxxaabxxxxbyyyyaaabzzz");
- var4 = new RE("x+");
- var5 = var4.grep(var5);
-
- for(int var6 = 0; var6 < var5.length; ++var6) {
- System.out.println("s[" + var6 + "] = " + var5[var6]);
- }
-
- var4 = new RE("a*b");
- String var7 = var4.subst("aaaabfooaaabgarplyaaabwackyb", "-");
- System.out.println("s = " + var7);
- this.runAutomatedTests();
- File var8 = new File(var1);
- if (!var8.exists()) {
- throw new Exception("Could not find: " + var1);
- } else {
- BufferedReader var9 = new BufferedReader(new FileReader(var8));
-
- try {
- while(var9.ready()) {
- String var10 = "";
-
- while(var9.ready()) {
- var10 = var9.readLine();
- if (var10 == null) {
- break;
- }
-
- var10 = var10.trim();
- if (var10.startsWith("#")) {
- break;
- }
-
- if (!var10.equals("")) {
- System.out.println("Script error. Line = " + var10);
- System.exit(0);
- }
- }
-
- if (!var9.ready()) {
- break;
- }
-
- this.expr = var9.readLine();
- ++this.field_1;
- this.say("");
- this.say(this.field_1 + ". " + this.expr);
- this.say("");
-
- try {
- var4.setProgram(this.compiler.compile(this.expr));
- } catch (Exception var25) {
- String var11 = var9.readLine().trim();
- if (var11.equals("ERR")) {
- this.say(" Match: ERR");
- this.success("Produces an error (" + ((Throwable)var25).toString() + "), as expected.");
- continue;
- }
-
- String var13 = ((Throwable)var25).getMessage() == null ? ((Throwable)var25).toString() : ((Throwable)var25).getMessage();
- this.fail("Produces an unexpected exception \"" + var13 + "\"");
- ((Throwable)var25).printStackTrace();
- } catch (Error var26) {
- this.fail("Compiler threw fatal error \"" + ((Throwable)var26).getMessage() + "\"");
- ((Throwable)var26).printStackTrace();
- }
-
- String var12 = var9.readLine().trim();
- this.say(" Match against: '" + var12 + "'");
- if (var12.equals("ERR")) {
- this.fail("Was expected to be an error, but wasn't.");
- } else {
- try {
- boolean var35 = var4.match(var12);
- String var34 = var9.readLine().trim();
- if (!var35) {
- this.say(" Match: NO");
- if (var34.equals("YES")) {
- this.fail("Did not match \"" + var12 + "\", when expected to.");
- } else if (var34.equals("NO")) {
- this.success("Did not match \"" + var12 + "\", as expected.");
- } else {
- this.die("Test script error!");
- }
- } else {
- this.say(" Match: YES");
- if (var34.equals("NO")) {
- this.fail("Matched \"" + var12 + "\", when not expected to.");
- } else if (!var34.equals("YES")) {
- this.die("Test script error!");
- } else {
- this.success("Matched \"" + var12 + "\", as expected:");
- this.say(" Paren count: " + var4.getParenCount());
-
- for(int var14 = 0; var14 < var4.getParenCount(); ++var14) {
- String var15 = var9.readLine().trim();
- this.say(" Paren " + var14 + " : " + var4.getParen(var14));
- if ((var15.length() != 0 || var4.getParen(var14) != null) && !var15.equals(var4.getParen(var14))) {
- this.fail("Register " + var14 + " should be = \"" + var15 + "\", but is \"" + var4.getParen(var14) + "\" instead.");
- }
- }
- }
- }
- } catch (Exception var23) {
- this.fail("Matcher threw exception: " + ((Throwable)var23).toString());
- ((Throwable)var23).printStackTrace();
- } catch (Error var24) {
- this.fail("Matcher threw fatal error \"" + ((Throwable)var24).getMessage() + "\"");
- ((Throwable)var24).printStackTrace();
- }
- }
- }
- } finally {
- var9.close();
- }
-
- System.out.println(NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - var2) + " ms.");
- System.out.println(NEW_LINE + "Tests complete. " + this.field_1 + " tests, " + this.failures + " failure(s).");
- }
- }
-
- void runAutomatedTests() throws Exception {
- RE var1 = new RE("(a*)b");
- this.say("Serialized/deserialized (a*)b");
- ByteArrayOutputStream var2 = new ByteArrayOutputStream(128);
- (new ObjectOutputStream(var2)).writeObject(var1);
- ByteArrayInputStream var3 = new ByteArrayInputStream(var2.toByteArray());
- var1 = (RE)(new ObjectInputStream(var3)).readObject();
- if (!var1.match("aaab")) {
- this.fail("Did not match 'aaab' with deserialized RE.");
- }
-
- this.say("aaaab = true");
- this.showParens(var1);
- var2.reset();
- this.say("Deserialized (a*)b");
- (new ObjectOutputStream(var2)).writeObject(var1);
- var3 = new ByteArrayInputStream(var2.toByteArray());
- var1 = (RE)(new ObjectInputStream(var3)).readObject();
- if (var1.getParenCount() != 0) {
- this.fail("Has parens after deserialization.");
- }
-
- if (!var1.match("aaab")) {
- this.fail("Did not match 'aaab' with deserialized RE.");
- }
-
- this.say("aaaab = true");
- this.showParens(var1);
- var1 = new RE("abc(\\w*)");
- this.say("MATCH_CASEINDEPENDENT abc(\\w*)");
- var1.setMatchFlags(1);
- this.say("abc(d*)");
- if (!var1.match("abcddd")) {
- this.fail("Did not match 'abcddd'.");
- }
-
- this.say("abcddd = true");
- this.showParens(var1);
- if (!var1.match("aBcDDdd")) {
- this.fail("Did not match 'aBcDDdd'.");
- }
-
- this.say("aBcDDdd = true");
- this.showParens(var1);
- if (!var1.match("ABCDDDDD")) {
- this.fail("Did not match 'ABCDDDDD'.");
- }
-
- this.say("ABCDDDDD = true");
- this.showParens(var1);
- }
- }
-