home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 July / PCpro_2005_07.ISO / files / wintools / FullSync / FullSyncInstaller.exe / org / apache / regexp / RETest.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-03-08  |  7.7 KB  |  316 lines

  1. package org.apache.regexp;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.InputStreamReader;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.PrintWriter;
  12.  
  13. public class RETest {
  14.    static final boolean showSuccesses = false;
  15.    static final String NEW_LINE = System.getProperty("line.separator");
  16.    // $FF: renamed from: r org.apache.regexp.RE
  17.    RE field_0 = new RE();
  18.    REDebugCompiler compiler = new REDebugCompiler();
  19.    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'};
  20.    REProgram re1;
  21.    String expr;
  22.    // $FF: renamed from: n int
  23.    int field_1;
  24.    int failures;
  25.  
  26.    public static void main(String[] var0) {
  27.       try {
  28.          test(var0);
  29.       } catch (Exception var2) {
  30.          ((Throwable)var2).printStackTrace();
  31.       }
  32.  
  33.    }
  34.  
  35.    public static boolean test(String[] var0) throws Exception {
  36.       RETest var1 = new RETest();
  37.       if (var0.length == 2) {
  38.          var1.runInteractiveTests(var0[1]);
  39.       } else if (var0.length == 1) {
  40.          var1.runAutomatedTests(var0[0]);
  41.       } else {
  42.          System.out.println("Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])");
  43.          System.out.println("By Default will run automated tests from file 'docs/RETest.txt' ...");
  44.          System.out.println();
  45.          var1.runAutomatedTests("docs/RETest.txt");
  46.       }
  47.  
  48.       return var1.failures == 0;
  49.    }
  50.  
  51.    public RETest() {
  52.       this.re1 = new REProgram(this.re1Instructions);
  53.       this.field_1 = 0;
  54.       this.failures = 0;
  55.    }
  56.  
  57.    void runInteractiveTests(String var1) {
  58.       try {
  59.          this.field_0.setProgram(this.compiler.compile(var1));
  60.          this.say("" + NEW_LINE + "" + var1 + "" + NEW_LINE + "");
  61.          PrintWriter var2 = new PrintWriter(System.out);
  62.          this.compiler.dumpProgram(var2);
  63.          var2.flush();
  64.          boolean var3 = true;
  65.  
  66.          while(var3) {
  67.             BufferedReader var4 = new BufferedReader(new InputStreamReader(System.in));
  68.             System.out.print("> ");
  69.             System.out.flush();
  70.             String var5 = var4.readLine();
  71.             if (var5 != null) {
  72.                if (this.field_0.match(var5)) {
  73.                   this.say("Match successful.");
  74.                } else {
  75.                   this.say("Match failed.");
  76.                }
  77.  
  78.                this.showParens(this.field_0);
  79.             } else {
  80.                var3 = false;
  81.                System.out.println();
  82.             }
  83.          }
  84.       } catch (Exception var6) {
  85.          this.say("Error: " + ((Throwable)var6).toString());
  86.          ((Throwable)var6).printStackTrace();
  87.       }
  88.  
  89.    }
  90.  
  91.    void die(String var1) {
  92.       this.say("FATAL ERROR: " + var1);
  93.       System.exit(0);
  94.    }
  95.  
  96.    void fail(String var1) {
  97.       ++this.failures;
  98.       this.say("" + NEW_LINE + "");
  99.       this.say("*******************************************************");
  100.       this.say("*********************  FAILURE!  **********************");
  101.       this.say("*******************************************************");
  102.       this.say("" + NEW_LINE + "");
  103.       this.say(var1);
  104.       this.say("");
  105.       PrintWriter var2 = new PrintWriter(System.out);
  106.       this.compiler.dumpProgram(var2);
  107.       var2.flush();
  108.       this.say("" + NEW_LINE + "");
  109.    }
  110.  
  111.    void success(String var1) {
  112.    }
  113.  
  114.    void say(String var1) {
  115.       System.out.println(var1);
  116.    }
  117.  
  118.    void show() {
  119.       this.say("" + NEW_LINE + "-----------------------" + NEW_LINE + "");
  120.       this.say("Expression #" + this.field_1 + " \"" + this.expr + "\" ");
  121.    }
  122.  
  123.    void showParens(RE var1) {
  124.       for(int var2 = 0; var2 < var1.getParenCount(); ++var2) {
  125.          this.say("$" + var2 + " = " + var1.getParen(var2));
  126.       }
  127.  
  128.    }
  129.  
  130.    void runAutomatedTests(String var1) throws Exception {
  131.       long var2 = System.currentTimeMillis();
  132.       RE var4 = new RE(this.re1);
  133.       this.say("a*b");
  134.       this.say("aaaab = " + var4.match("aaab"));
  135.       this.showParens(var4);
  136.       this.say("b = " + var4.match("b"));
  137.       this.showParens(var4);
  138.       this.say("c = " + var4.match("c"));
  139.       this.showParens(var4);
  140.       this.say("ccccaaaaab = " + var4.match("ccccaaaaab"));
  141.       this.showParens(var4);
  142.       var4 = new RE("a*b");
  143.       String[] var5 = var4.split("xxxxaabxxxxbyyyyaaabzzz");
  144.       var4 = new RE("x+");
  145.       var5 = var4.grep(var5);
  146.  
  147.       for(int var6 = 0; var6 < var5.length; ++var6) {
  148.          System.out.println("s[" + var6 + "] = " + var5[var6]);
  149.       }
  150.  
  151.       var4 = new RE("a*b");
  152.       String var7 = var4.subst("aaaabfooaaabgarplyaaabwackyb", "-");
  153.       System.out.println("s = " + var7);
  154.       this.runAutomatedTests();
  155.       File var8 = new File(var1);
  156.       if (!var8.exists()) {
  157.          throw new Exception("Could not find: " + var1);
  158.       } else {
  159.          BufferedReader var9 = new BufferedReader(new FileReader(var8));
  160.  
  161.          try {
  162.             while(var9.ready()) {
  163.                String var10 = "";
  164.  
  165.                while(var9.ready()) {
  166.                   var10 = var9.readLine();
  167.                   if (var10 == null) {
  168.                      break;
  169.                   }
  170.  
  171.                   var10 = var10.trim();
  172.                   if (var10.startsWith("#")) {
  173.                      break;
  174.                   }
  175.  
  176.                   if (!var10.equals("")) {
  177.                      System.out.println("Script error.  Line = " + var10);
  178.                      System.exit(0);
  179.                   }
  180.                }
  181.  
  182.                if (!var9.ready()) {
  183.                   break;
  184.                }
  185.  
  186.                this.expr = var9.readLine();
  187.                ++this.field_1;
  188.                this.say("");
  189.                this.say(this.field_1 + ". " + this.expr);
  190.                this.say("");
  191.  
  192.                try {
  193.                   var4.setProgram(this.compiler.compile(this.expr));
  194.                } catch (Exception var25) {
  195.                   String var11 = var9.readLine().trim();
  196.                   if (var11.equals("ERR")) {
  197.                      this.say("   Match: ERR");
  198.                      this.success("Produces an error (" + ((Throwable)var25).toString() + "), as expected.");
  199.                      continue;
  200.                   }
  201.  
  202.                   String var13 = ((Throwable)var25).getMessage() == null ? ((Throwable)var25).toString() : ((Throwable)var25).getMessage();
  203.                   this.fail("Produces an unexpected exception \"" + var13 + "\"");
  204.                   ((Throwable)var25).printStackTrace();
  205.                } catch (Error var26) {
  206.                   this.fail("Compiler threw fatal error \"" + ((Throwable)var26).getMessage() + "\"");
  207.                   ((Throwable)var26).printStackTrace();
  208.                }
  209.  
  210.                String var12 = var9.readLine().trim();
  211.                this.say("   Match against: '" + var12 + "'");
  212.                if (var12.equals("ERR")) {
  213.                   this.fail("Was expected to be an error, but wasn't.");
  214.                } else {
  215.                   try {
  216.                      boolean var35 = var4.match(var12);
  217.                      String var34 = var9.readLine().trim();
  218.                      if (!var35) {
  219.                         this.say("   Match: NO");
  220.                         if (var34.equals("YES")) {
  221.                            this.fail("Did not match \"" + var12 + "\", when expected to.");
  222.                         } else if (var34.equals("NO")) {
  223.                            this.success("Did not match \"" + var12 + "\", as expected.");
  224.                         } else {
  225.                            this.die("Test script error!");
  226.                         }
  227.                      } else {
  228.                         this.say("   Match: YES");
  229.                         if (var34.equals("NO")) {
  230.                            this.fail("Matched \"" + var12 + "\", when not expected to.");
  231.                         } else if (!var34.equals("YES")) {
  232.                            this.die("Test script error!");
  233.                         } else {
  234.                            this.success("Matched \"" + var12 + "\", as expected:");
  235.                            this.say("   Paren count: " + var4.getParenCount());
  236.  
  237.                            for(int var14 = 0; var14 < var4.getParenCount(); ++var14) {
  238.                               String var15 = var9.readLine().trim();
  239.                               this.say("   Paren " + var14 + " : " + var4.getParen(var14));
  240.                               if ((var15.length() != 0 || var4.getParen(var14) != null) && !var15.equals(var4.getParen(var14))) {
  241.                                  this.fail("Register " + var14 + " should be = \"" + var15 + "\", but is \"" + var4.getParen(var14) + "\" instead.");
  242.                               }
  243.                            }
  244.                         }
  245.                      }
  246.                   } catch (Exception var23) {
  247.                      this.fail("Matcher threw exception: " + ((Throwable)var23).toString());
  248.                      ((Throwable)var23).printStackTrace();
  249.                   } catch (Error var24) {
  250.                      this.fail("Matcher threw fatal error \"" + ((Throwable)var24).getMessage() + "\"");
  251.                      ((Throwable)var24).printStackTrace();
  252.                   }
  253.                }
  254.             }
  255.          } finally {
  256.             var9.close();
  257.          }
  258.  
  259.          System.out.println(NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - var2) + " ms.");
  260.          System.out.println(NEW_LINE + "Tests complete.  " + this.field_1 + " tests, " + this.failures + " failure(s).");
  261.       }
  262.    }
  263.  
  264.    void runAutomatedTests() throws Exception {
  265.       RE var1 = new RE("(a*)b");
  266.       this.say("Serialized/deserialized (a*)b");
  267.       ByteArrayOutputStream var2 = new ByteArrayOutputStream(128);
  268.       (new ObjectOutputStream(var2)).writeObject(var1);
  269.       ByteArrayInputStream var3 = new ByteArrayInputStream(var2.toByteArray());
  270.       var1 = (RE)(new ObjectInputStream(var3)).readObject();
  271.       if (!var1.match("aaab")) {
  272.          this.fail("Did not match 'aaab' with deserialized RE.");
  273.       }
  274.  
  275.       this.say("aaaab = true");
  276.       this.showParens(var1);
  277.       var2.reset();
  278.       this.say("Deserialized (a*)b");
  279.       (new ObjectOutputStream(var2)).writeObject(var1);
  280.       var3 = new ByteArrayInputStream(var2.toByteArray());
  281.       var1 = (RE)(new ObjectInputStream(var3)).readObject();
  282.       if (var1.getParenCount() != 0) {
  283.          this.fail("Has parens after deserialization.");
  284.       }
  285.  
  286.       if (!var1.match("aaab")) {
  287.          this.fail("Did not match 'aaab' with deserialized RE.");
  288.       }
  289.  
  290.       this.say("aaaab = true");
  291.       this.showParens(var1);
  292.       var1 = new RE("abc(\\w*)");
  293.       this.say("MATCH_CASEINDEPENDENT abc(\\w*)");
  294.       var1.setMatchFlags(1);
  295.       this.say("abc(d*)");
  296.       if (!var1.match("abcddd")) {
  297.          this.fail("Did not match 'abcddd'.");
  298.       }
  299.  
  300.       this.say("abcddd = true");
  301.       this.showParens(var1);
  302.       if (!var1.match("aBcDDdd")) {
  303.          this.fail("Did not match 'aBcDDdd'.");
  304.       }
  305.  
  306.       this.say("aBcDDdd = true");
  307.       this.showParens(var1);
  308.       if (!var1.match("ABCDDDDD")) {
  309.          this.fail("Did not match 'ABCDDDDD'.");
  310.       }
  311.  
  312.       this.say("ABCDDDDD = true");
  313.       this.showParens(var1);
  314.    }
  315. }
  316.