jdk/test/javax/xml/jaxp/XPath/8035577/Regex.java
changeset 23555 679ad1863be3
child 30820 0d4717a011d3
equal deleted inserted replaced
23554:f65fec81a347 23555:679ad1863be3
       
     1 /*
       
     2  * Copyright (c) 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  * @bug 8035577
       
    27  * @summary Tests for xpath regular expression methods.
       
    28  * @run main Regex
       
    29  * @author david.x.li@oracle.com
       
    30  */
       
    31 
       
    32 import com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression;
       
    33 import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
       
    34 
       
    35 public class Regex {
       
    36 
       
    37     public static void main(String[] args) {
       
    38         testIntersect();
       
    39     }
       
    40 
       
    41     static void testIntersect() {
       
    42         // The use of intersection operator & is not allowed in
       
    43         // XML schema.  Consequently, the intersection operator
       
    44         // can never be called except for internal API usage.
       
    45         // Following test illustrates this.
       
    46         try{
       
    47             new RegularExpression("(?[b-d]&[a-r])", "X");
       
    48             throw new RuntimeException ("Xerces XPath Regex: " +
       
    49                 "intersection not allowed in XML schema mode, " +
       
    50                 "exception expected above.");
       
    51         }
       
    52         catch (ParseException e) {
       
    53             // Empty, expecting an exception
       
    54         }
       
    55 
       
    56         // Bug 8035577: verifying a typo fix in RangeToken.intersectRanges.
       
    57         // Note: Each test case has a diagram showing the ranges being tested.
       
    58         // Following test case will trigger the typo.
       
    59         // o-----o
       
    60         //    o-----o
       
    61         RegularExpression ce = new RegularExpression("(?[a-e]&[c-r])");
       
    62         if (!(ce.matches("c") && ce.matches("d") && ce.matches("e"))) {
       
    63             throw new RuntimeException("Xerces XPath Regex Error: " +
       
    64                 "[c-e] expected to match c,d,e.");
       
    65         }
       
    66 
       
    67         if (ce.matches("b") || ce.matches("f")) {
       
    68             throw new RuntimeException("Xerces XPath Regex Error: " +
       
    69                 "[c-e] not expected to match b or f.");
       
    70         }
       
    71 
       
    72         // Test the expected behavior after fixing the typo.
       
    73         //    o------o
       
    74         // o-------------o
       
    75         RegularExpression bd = new RegularExpression("(?[b-d]&[a-r])");
       
    76         if (!(bd.matches("b") && bd.matches("c") && bd.matches("d"))) {
       
    77             throw new RuntimeException("Xerces XPath Regex Error: " +
       
    78                 "[b-d] expected to match b,c,d.");
       
    79         }
       
    80 
       
    81         if (bd.matches("e") || bd.matches("a")) {
       
    82             throw new RuntimeException("Xerces XPath Regex Error: " +
       
    83                 "[b-d] not expected to match a or e.");
       
    84         }
       
    85 
       
    86         // Bug fix for first range ends later than second range.
       
    87         // o--------o
       
    88         //    o--o
       
    89         RegularExpression bd2 = new RegularExpression("(?[a-r]&[b-d])");
       
    90         if (!(bd.matches("b") && bd.matches("c") && bd.matches("d"))) {
       
    91             throw new RuntimeException("Xerces XPath Regex Error: " +
       
    92                 "[b-d] expected to match b,c,d, test 2.");
       
    93         }
       
    94 
       
    95         if (bd2.matches("e") || bd2.matches("a")) {
       
    96             throw new RuntimeException("Xerces XPath Regex Error: " +
       
    97                 "[b-d] not expected to match a or e, test 2.");
       
    98         }
       
    99 
       
   100         //    o-----o
       
   101         // o----o
       
   102         RegularExpression dh = new RegularExpression("(?[d-z]&[a-h])");
       
   103         if (!(dh.matches("d") && dh.matches("e") && dh.matches("h"))) {
       
   104             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   105                 "[d-h] expected to match d,e,h.");
       
   106         }
       
   107 
       
   108         if (dh.matches("c") || bd2.matches("i")) {
       
   109             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   110                 "[d-h] not expected to match c or i.");
       
   111         }
       
   112 
       
   113         // Test code improvement, addition of src2+=2 to one of the
       
   114         // conditions.  In this case, src1 leftover from matching
       
   115         // first portion of src2 is re-used to match against the next
       
   116         // portion of src2.
       
   117         // o--------------o
       
   118         //   o--o  o--o
       
   119         RegularExpression dfhk = new RegularExpression("(?[b-r]&[d-fh-k])");
       
   120         if (!(dfhk.matches("d") && dfhk.matches("f") && dfhk.matches("h") && dfhk.matches("k"))) {
       
   121             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   122                 "[d-fh-k] expected to match d,f,h,k.");
       
   123         }
       
   124 
       
   125         if (dfhk.matches("c") || dfhk.matches("g") || dfhk.matches("l")) {
       
   126             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   127                 "[d-fh-k] not expected to match c,g,l.");
       
   128         }
       
   129 
       
   130         // random tests
       
   131         //    o------------o
       
   132         // o-----o  o--o
       
   133         RegularExpression cfhk = new RegularExpression("(?[c-r]&[b-fh-k])");
       
   134         if (!(cfhk.matches("c") && cfhk.matches("f") && cfhk.matches("h") && cfhk.matches("k"))) {
       
   135             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   136                 "[c-fh-k] expected to match c,f,h,k.");
       
   137         }
       
   138 
       
   139         if (cfhk.matches("b") || cfhk.matches("g") || cfhk.matches("l")) {
       
   140             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   141                 "[c-fh-k] not expected to match b,g,l.");
       
   142         }
       
   143 
       
   144         // o----------o
       
   145         //    o-----------o
       
   146         //  o----o  o---o
       
   147         RegularExpression ekor = new RegularExpression("(?[a-r]&[e-z]&[c-ko-s])");
       
   148         if (!(ekor.matches("e") && ekor.matches("k") && ekor.matches("o") && ekor.matches("r"))) {
       
   149             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   150                 "[e-ko-r] expected to match e,k,o,r.");
       
   151         }
       
   152 
       
   153         if (ekor.matches("d") || ekor.matches("l") || ekor.matches("s")) {
       
   154             throw new RuntimeException("Xerces XPath Regex Error: " +
       
   155                 "[e-ko-r] not expected to match d,l,s.");
       
   156         }
       
   157 
       
   158     }
       
   159 
       
   160 }