1 /*
   2  * Copyright (c) 2016, 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 that whitespaces within schema types and names are
  28  *          collapsed by XSOM parser
  29  * @modules jdk.xml.bind
  30  *          jdk.xml.bind/com.sun.xml.internal.xsom
  31  *          jdk.xml.bind/com.sun.xml.internal.xsom.parser
  32  * @run testng TestXSOM
  33  *
  34  */
  35 
  36 import com.sun.xml.internal.xsom.XSSchemaSet;
  37 import com.sun.xml.internal.xsom.parser.XSOMParser;
  38 import java.io.File;
  39 import java.nio.file.Paths;
  40 import javax.xml.parsers.SAXParserFactory;
  41 import org.testng.Assert;
  42 import org.testng.annotations.DataProvider;
  43 import org.testng.annotations.Test;
  44 import org.xml.sax.ErrorHandler;
  45 import org.xml.sax.SAXException;
  46 import org.xml.sax.SAXParseException;
  47 
  48 public class TestXSOM {
  49 
  50     @Test(dataProvider = "WhitespacedTypes")
  51     public void testWhitespacesCollapse(String schemaFile) throws Exception {
  52         XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
  53         XsomErrorHandler eh = new XsomErrorHandler();
  54 
  55         parser.setErrorHandler(eh);
  56         parser.parse(getSchemaFile(schemaFile));
  57         XSSchemaSet sset = parser.getResult();
  58 
  59         Assert.assertFalse(eh.gotError);
  60         Assert.assertNotNull(sset);
  61     }
  62 
  63     // Get location of schema file located in test source directory
  64     private static File getSchemaFile(String filename) {
  65         String testSrc = System.getProperty("test.src");
  66         if (testSrc == null) {
  67             testSrc = ".";
  68         }
  69         return Paths.get(testSrc).resolve(filename).toFile();
  70     }
  71 
  72     @DataProvider(name = "WhitespacedTypes")
  73     private Object[][] dataWhitespacedTypes() {
  74         return new Object[][]{
  75             {"SimpleType.xsd"},
  76             {"ComplexType.xsd"},
  77             {"ComplexTypeExtension.xsd"},
  78             {"ComplexTypeRestriction.xsd"},
  79             {"IdentityConstraint.xsd"},
  80             {"ParticlesAndAttributes.xsd"}
  81         };
  82 
  83     }
  84 
  85     // Test XSOM error handler
  86     private static class XsomErrorHandler implements ErrorHandler {
  87 
  88         public boolean gotError = false;
  89 
  90         @Override
  91         public void warning(SAXParseException ex) throws SAXException {
  92             System.out.println("XSOM warning:" + ex);
  93         }
  94 
  95         @Override
  96         public void error(SAXParseException ex) throws SAXException {
  97             System.out.println("XSOM error:" + ex);
  98             gotError = true;
  99         }
 100 
 101         @Override
 102         public void fatalError(SAXParseException ex) throws SAXException {
 103             System.out.println("XSOM fatal error:" + ex);
 104             gotError = true;
 105         }
 106     }
 107 }
 108 
 109 
 110