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