Path: blob/master/test/jdk/javax/xml/jaxp/XPath/8035577/Regex.java
41154 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 803557726* @summary Tests for xpath regular expression methods.27* @modules java.xml/com.sun.org.apache.xerces.internal.impl.xpath.regex28* @run main Regex29* @author [email protected]30*/3132import com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression;33import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;3435public class Regex {3637public static void main(String[] args) {38testIntersect();39}4041static void testIntersect() {42// The use of intersection operator & is not allowed in43// XML schema. Consequently, the intersection operator44// can never be called except for internal API usage.45// Following test illustrates this.46try{47new RegularExpression("(?[b-d]&[a-r])", "X");48throw new RuntimeException ("Xerces XPath Regex: " +49"intersection not allowed in XML schema mode, " +50"exception expected above.");51}52catch (ParseException e) {53// Empty, expecting an exception54}5556// 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-----o60// o-----o61RegularExpression ce = new RegularExpression("(?[a-e]&[c-r])");62if (!(ce.matches("c") && ce.matches("d") && ce.matches("e"))) {63throw new RuntimeException("Xerces XPath Regex Error: " +64"[c-e] expected to match c,d,e.");65}6667if (ce.matches("b") || ce.matches("f")) {68throw new RuntimeException("Xerces XPath Regex Error: " +69"[c-e] not expected to match b or f.");70}7172// Test the expected behavior after fixing the typo.73// o------o74// o-------------o75RegularExpression bd = new RegularExpression("(?[b-d]&[a-r])");76if (!(bd.matches("b") && bd.matches("c") && bd.matches("d"))) {77throw new RuntimeException("Xerces XPath Regex Error: " +78"[b-d] expected to match b,c,d.");79}8081if (bd.matches("e") || bd.matches("a")) {82throw new RuntimeException("Xerces XPath Regex Error: " +83"[b-d] not expected to match a or e.");84}8586// Bug fix for first range ends later than second range.87// o--------o88// o--o89RegularExpression bd2 = new RegularExpression("(?[a-r]&[b-d])");90if (!(bd.matches("b") && bd.matches("c") && bd.matches("d"))) {91throw new RuntimeException("Xerces XPath Regex Error: " +92"[b-d] expected to match b,c,d, test 2.");93}9495if (bd2.matches("e") || bd2.matches("a")) {96throw new RuntimeException("Xerces XPath Regex Error: " +97"[b-d] not expected to match a or e, test 2.");98}99100// o-----o101// o----o102RegularExpression dh = new RegularExpression("(?[d-z]&[a-h])");103if (!(dh.matches("d") && dh.matches("e") && dh.matches("h"))) {104throw new RuntimeException("Xerces XPath Regex Error: " +105"[d-h] expected to match d,e,h.");106}107108if (dh.matches("c") || bd2.matches("i")) {109throw new RuntimeException("Xerces XPath Regex Error: " +110"[d-h] not expected to match c or i.");111}112113// Test code improvement, addition of src2+=2 to one of the114// conditions. In this case, src1 leftover from matching115// first portion of src2 is re-used to match against the next116// portion of src2.117// o--------------o118// o--o o--o119RegularExpression dfhk = new RegularExpression("(?[b-r]&[d-fh-k])");120if (!(dfhk.matches("d") && dfhk.matches("f") && dfhk.matches("h") && dfhk.matches("k"))) {121throw new RuntimeException("Xerces XPath Regex Error: " +122"[d-fh-k] expected to match d,f,h,k.");123}124125if (dfhk.matches("c") || dfhk.matches("g") || dfhk.matches("l")) {126throw new RuntimeException("Xerces XPath Regex Error: " +127"[d-fh-k] not expected to match c,g,l.");128}129130// random tests131// o------------o132// o-----o o--o133RegularExpression cfhk = new RegularExpression("(?[c-r]&[b-fh-k])");134if (!(cfhk.matches("c") && cfhk.matches("f") && cfhk.matches("h") && cfhk.matches("k"))) {135throw new RuntimeException("Xerces XPath Regex Error: " +136"[c-fh-k] expected to match c,f,h,k.");137}138139if (cfhk.matches("b") || cfhk.matches("g") || cfhk.matches("l")) {140throw new RuntimeException("Xerces XPath Regex Error: " +141"[c-fh-k] not expected to match b,g,l.");142}143144// o----------o145// o-----------o146// o----o o---o147RegularExpression ekor = new RegularExpression("(?[a-r]&[e-z]&[c-ko-s])");148if (!(ekor.matches("e") && ekor.matches("k") && ekor.matches("o") && ekor.matches("r"))) {149throw new RuntimeException("Xerces XPath Regex Error: " +150"[e-ko-r] expected to match e,k,o,r.");151}152153if (ekor.matches("d") || ekor.matches("l") || ekor.matches("s")) {154throw new RuntimeException("Xerces XPath Regex Error: " +155"[e-ko-r] not expected to match d,l,s.");156}157158}159160}161162163