1 /*
2  * Copyright (c) 2017, 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 8159240
27  * @summary Check if types and names with whitespaces are collapsed by
28  *          XSOM schema parser
29  * @compile -XDignore.symbol.file WhitespacesTest.java
30  * @run testng WhitespacesTest
31  *
32  */
33 
34 import com.sun.xml.internal.xsom.parser.XSOMParser;
35 import java.io.File;
36 import java.nio.file.Paths;
37 import javax.xml.parsers.SAXParserFactory;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40 import org.xml.sax.ErrorHandler;
41 import org.xml.sax.SAXException;
42 import org.xml.sax.SAXParseException;
43 
44 public class WhitespacesTest {
45 
46     @Test(dataProvider = "TestSchemaFiles")
testWhitespacesCollapse(String schemaFile)47     public void testWhitespacesCollapse(String schemaFile) throws Exception {
48         XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
49         XsomErrorHandler eh = new XsomErrorHandler();
50 
51         parser.setErrorHandler(eh);
52         parser.parse(getSchemaFile(schemaFile));
53 
54         if (eh.gotError) {
55             throw new RuntimeException("XSOM parser encountered error", eh.e);
56         }
57     }
58 
59     // Get location of schema file located in test source directory
getSchemaFile(String filename)60     private static File getSchemaFile(String filename) {
61         String testSrc = System.getProperty("test.src", ".");
62         return Paths.get(testSrc).resolve(filename).toFile();
63     }
64 
65     @DataProvider(name = "TestSchemaFiles")
dataTestSchemaFiles()66     private Object[][] dataTestSchemaFiles() {
67         return new Object[][] {
68             {"complexType.xsd"},
69             {"complexTypeExtension.xsd"},
70             {"complexTypeRestriction.xsd"},
71             {"identityConstraint.xsd"},
72             {"particlesAndAttributes.xsd"},
73             {"simpleType.xsd"}
74         };
75 
76     }
77 
78     // Test XSOM error handler
79     private static class XsomErrorHandler implements ErrorHandler {
80 
81         public boolean gotError = false;
82         public Exception e = null;
83 
84         @Override
warning(SAXParseException ex)85         public void warning(SAXParseException ex) throws SAXException {
86             System.err.println("XSOM warning:" + ex);
87         }
88 
89         @Override
error(SAXParseException ex)90         public void error(SAXParseException ex) throws SAXException {
91             System.err.println("XSOM error:" + ex);
92             e = ex;
93             gotError = true;
94         }
95 
96         @Override
fatalError(SAXParseException ex)97         public void fatalError(SAXParseException ex) throws SAXException {
98             System.err.println("XSOM fatal error:" + ex);
99             e = ex;
100             gotError = true;
101         }
102     }
103 }
104