1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  @test
  26  @summary Tests for the 1.4 regex package
  27  @summary com.apple.junit.java.util.regex;
  28  @run junit BasicMatching
  29  */
  30 
  31 import junit.framework.*;
  32 
  33 import java.util.regex.Matcher;
  34 import java.util.regex.Pattern;
  35 
  36 public class BasicMatching extends TestCase {
  37 
  38     public static Test suite() {
  39         return new TestSuite(BasicMatching.class);
  40     }
  41 
  42     public static void main (String[] args) throws RuntimeException {
  43         TestResult tr = junit.textui.TestRunner.run(suite());
  44         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  45             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  46         }
  47     }
  48 
  49     public static String kGettysburgAddress  =
  50         "Four score and seven years ago our fathers brought forth on this \n" +
  51         "continent a new nation, conceived in liberty and dedicated to the \n" +
  52         "proposition that all men are created equal. Now we are engaged in \n" +
  53         "a great civil war, testing whether that nation or any nation so \n" +
  54         "conceived and so dedicated can long endure. We are met on a great \n" +
  55         "battlefield of that war. We have come to dedicate a portion of \n" +
  56         "that field as a final resting-place for those who here gave their \n" +
  57         "lives that that nation might live. It is altogether fitting and \n" +
  58         "proper that we should do this. But in a larger sense, we cannot \n" +
  59         "dedicate, we cannot consecrate, we cannot hallow this ground. \n" +
  60         "The brave men, living and dead who struggled here have consecrated \n" +
  61         "it far above our poor power to add or detract. The world will \n" +
  62         "little note nor long remember what we say here, but it can never \n" +
  63         "forget what they did here. It is for us the living rather to be \n" +
  64         "dedicated here to the unfinished work which they who fought here\n" +
  65         "have thus far so nobly advanced. It is rather for us to be here \n" +
  66         "dedicated to the great task remaining before us--that from these \n" +
  67         "honored dead we take increased devotion to that cause for which \n" +
  68         "they gave the last full measure of devotion--that we here highly \n" +
  69         "resolve that these dead shall not have died in vain, that this \n" +
  70         "nation under God shall have a new birth of freedom, and that \n" +
  71         "government of the people, by the people, for the people shall \n" +
  72         "not perish from the earth.\n";
  73 
  74     class rx {
  75          String pattern;
  76          boolean match;
  77          int flags = 0;
  78 
  79         // Getters and Setters
  80         public String getPattern() { return pattern; }
  81         public boolean getMatch() { return match; }
  82         public int getFlags() { return flags; }
  83         public void setPattern(String s) { pattern = s; }
  84         public void setMatch(boolean b) { match = b; }
  85         public void setFlags(int f) { flags = f; }
  86 
  87         public String toString() { return ("\"" + pattern + "\", " + ((match == true)?"true":"false") + ((flags==0)?"":", flags " + flags)); }
  88 
  89         public rx(String s, boolean b) {
  90             pattern = s;
  91             match = b;
  92         }
  93 
  94         public rx(String s, boolean b, int f) {
  95             pattern = s;
  96             match = b;
  97             flags = f;
  98         }
  99 
 100     };
 101 
 102     // See http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Pattern.html for lots of great regular expression info
 103 
 104     public rx[] pattern = {
 105         new rx("father", true),
 106         new rx("con*secrate", true),
 107         new rx("con.+secrate", false),
 108         new rx("dedicate.*ground", true),
 109         new rx("dedicate.*brave", false),
 110         new rx("dedicate.*brave", true, Pattern.DOTALL),
 111         new rx("c.n.e.v.d", true),
 112         new rx("c.n.e.v.e", false),
 113         new rx("F[a-z][a-z]r score", true),
 114         new rx("F[a-z][a-z&&[^r]] score", false),
 115         new rx("\\d", false),
 116         new rx("here$", true, Pattern.MULTILINE),
 117         new rx("foul", false)
 118     };
 119 
 120     public boolean doRegexTest (String regexpattern, int flags, String regexmatcher) {
 121         Pattern p = Pattern.compile (regexpattern, flags);
 122         Matcher m = p.matcher(regexmatcher);
 123         return (m.find());
 124     }
 125 
 126     public void testBasicMatching () {
 127         for (int i = 0; i < pattern.length; i++ ) {
 128             boolean answer = doRegexTest(pattern[i].getPattern(), pattern[i].getFlags(), kGettysburgAddress);
 129             String error = "Unexpected result: \"" + pattern[i].getPattern() + "\" did not evaluate to " + pattern[i].getMatch() + " with flags set to " + pattern[i].getFlags();
 130             assertTrue( error, answer == pattern[i].getMatch() );
 131         }
 132     }
 133 }