< prev index next >

test/javax/xml/jaxp/unittest/validation/Bug6773084Test.java

Print this page




  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 package validation;
  25 
  26 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  27 
  28 import java.io.File;
  29 import java.io.FileFilter;
  30 import java.io.IOException;
  31 import java.util.concurrent.BrokenBarrierException;
  32 import java.util.concurrent.CyclicBarrier;
  33 import java.util.concurrent.ExecutorService;
  34 import java.util.concurrent.Executors;

  35 
  36 import javax.xml.XMLConstants;
  37 import javax.xml.parsers.DocumentBuilder;
  38 import javax.xml.parsers.DocumentBuilderFactory;
  39 import javax.xml.parsers.ParserConfigurationException;
  40 import javax.xml.transform.Source;
  41 import javax.xml.transform.dom.DOMSource;
  42 import javax.xml.transform.stream.StreamSource;
  43 import javax.xml.validation.Schema;
  44 import javax.xml.validation.SchemaFactory;
  45 import javax.xml.validation.Validator;
  46 
  47 import org.testng.Assert;
  48 import org.testng.annotations.Listeners;
  49 import org.testng.annotations.Test;
  50 import org.w3c.dom.Document;
  51 import org.xml.sax.ErrorHandler;
  52 import org.xml.sax.SAXException;
  53 import org.xml.sax.SAXParseException;
  54 
  55 /*
  56  * @test
  57  * @bug 6773084
  58  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  59  * @run testng/othervm -DrunSecMngr=true validation.Bug6773084Test
  60  * @run testng/othervm validation.Bug6773084Test
  61  * @summary Test Schema object is thread safe.
  62  */
  63 @Listeners({jaxp.library.FilePolicy.class})
  64 public class Bug6773084Test {
  65     static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  66     static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
  67 
  68     private static final int NTHREADS = 25;
  69     private static final ExecutorService EXEC = Executors.newCachedThreadPool();
  70 
  71     private static final CyclicBarrier BARRIER = new CyclicBarrier(NTHREADS);

  72 
  73     public static final String IN_FOLDER = Bug6773084Test.class.getResource("Bug6773084In").getPath();
  74     public static final String XSD_PATH = Bug6773084Test.class.getResource("Bug6773084.xsd").getPath();
  75 
  76     private static Schema schema;
  77 
  78     @Test
  79     public void test() throws Exception {
  80         SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  81         Source schemaFile = new StreamSource(XSD_PATH);
  82         try {
  83             schema = factory.newSchema(schemaFile);
  84         } catch (SAXException e) {
  85             e.printStackTrace();
  86             System.exit(-1);
  87         }
  88 
  89         File incoming = new File(IN_FOLDER);
  90         File[] files = incoming.listFiles(new FileFilter() {
  91             public boolean accept(File file) {
  92                 return file.isFile() && file.getName().endsWith(".xml");
  93             }
  94         });
  95 



  96         for (int i = 0; i < files.length; i++) {
  97             EXEC.execute(new XMLValiddator(files[i], i));
  98         }
  99         runWithAllPerm(() -> EXEC.shutdown());
 100 
 101     }
 102 
 103     private static class XMLValiddator implements Runnable {
 104 
 105         private File file;
 106         private int index;
 107 
 108         public XMLValiddator(File file, int index) {
 109             this.file = file;
 110             this.index = index;
 111         }
 112 
 113         public void run() {
 114 
 115             try {
 116                 System.out.printf("Waiting for barrier: %s%n", index);
 117                 BARRIER.await();
 118                 System.out.println("Validating....");
 119 
 120                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 121                 factory.setNamespaceAware(true);
 122 
 123                 DocumentBuilder builder = factory.newDocumentBuilder();
 124                 Document document = builder.parse(file);
 125 
 126                 Validator validator = schema.newValidator();
 127                 validator.setErrorHandler(new ErrorHandlerImpl());
 128                 validator.validate(new DOMSource(document));
 129 
 130             } catch (IOException e) {
 131                 e.printStackTrace();
 132             } catch (SAXException e) {
 133                 e.printStackTrace();
 134                 Assert.fail("Test failed.");
 135             } catch (ParserConfigurationException e) {
 136                 e.printStackTrace();
 137             } catch (BrokenBarrierException e) {
 138                 e.printStackTrace();
 139             } catch (InterruptedException e) {
 140                 e.printStackTrace();
 141             }
 142 
 143         }
 144     }
 145 
 146     private static class ErrorHandlerImpl implements ErrorHandler {
 147 
 148         public void warning(SAXParseException exception) throws SAXException {
 149             System.out
 150                     .printf("**Parsing Warning. Line: %s  URI: %s  Message: %s%n", exception.getLineNumber(), exception.getSystemId(), exception.getMessage());
 151         }
 152 
 153         public void error(SAXParseException exception) throws SAXException {
 154             String msg = String.format("**Parsing Error. Line: %s  URI: %s  Message: %s%n", exception.getLineNumber(), exception.getSystemId(),
 155                     exception.getMessage());
 156             System.out.println(msg);


  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 package validation;
  25 
  26 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  27 
  28 import java.io.File;
  29 import java.io.FileFilter;
  30 import java.io.IOException;
  31 import java.util.concurrent.BrokenBarrierException;
  32 import java.util.concurrent.CyclicBarrier;
  33 import java.util.concurrent.ExecutorService;
  34 import java.util.concurrent.Executors;
  35 import java.util.concurrent.TimeUnit;
  36 
  37 import javax.xml.XMLConstants;

  38 import javax.xml.parsers.DocumentBuilderFactory;

  39 import javax.xml.transform.Source;
  40 import javax.xml.transform.dom.DOMSource;
  41 import javax.xml.transform.stream.StreamSource;
  42 import javax.xml.validation.Schema;
  43 import javax.xml.validation.SchemaFactory;
  44 import javax.xml.validation.Validator;
  45 
  46 import org.testng.Assert;
  47 import org.testng.annotations.Listeners;
  48 import org.testng.annotations.Test;
  49 import org.w3c.dom.Document;
  50 import org.xml.sax.ErrorHandler;
  51 import org.xml.sax.SAXException;
  52 import org.xml.sax.SAXParseException;
  53 
  54 /*
  55  * @test
  56  * @bug 6773084
  57  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  58  * @run testng/othervm -DrunSecMngr=true validation.Bug6773084Test
  59  * @run testng/othervm validation.Bug6773084Test
  60  * @summary Test Schema object is thread safe.
  61  */
  62 @Listeners({jaxp.library.FilePolicy.class})
  63 public class Bug6773084Test {
  64     static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  65     static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
  66 
  67     private static final int NTHREADS = 25;
  68     private static final ExecutorService EXEC = Executors.newCachedThreadPool();
  69 
  70     private static final CyclicBarrier BARRIER = new CyclicBarrier(NTHREADS);
  71     private static final int TIMEOUT = 110;
  72 
  73     public static final String IN_FOLDER = Bug6773084Test.class.getResource("Bug6773084In").getPath();
  74     public static final String XSD_PATH = Bug6773084Test.class.getResource("Bug6773084.xsd").getPath();
  75 
  76     private static Schema schema;
  77 
  78     @Test
  79     public void test() throws Exception {
  80         SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  81         Source schemaFile = new StreamSource(XSD_PATH);
  82         try {
  83             schema = factory.newSchema(schemaFile);
  84         } catch (SAXException e) {
  85             e.printStackTrace();
  86             System.exit(-1);
  87         }
  88 
  89         File incoming = new File(IN_FOLDER);
  90         File[] files = incoming.listFiles(new FileFilter() {
  91             public boolean accept(File file) {
  92                 return file.isFile() && file.getName().endsWith(".xml");
  93             }
  94         });
  95 
  96         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  97         dbf.setNamespaceAware(true);
  98 
  99         for (int i = 0; i < files.length; i++) {
 100             EXEC.execute(new XMLValiddator(dbf.newDocumentBuilder().parse(files[i]), i));
 101         }
 102         runWithAllPerm(() -> EXEC.shutdown());
 103         EXEC.awaitTermination(TIMEOUT, TimeUnit.SECONDS);
 104     }
 105 
 106     private static class XMLValiddator implements Runnable {
 107 
 108         private Document document;
 109         private int index;
 110 
 111         public XMLValiddator(Document document, int index) {
 112             this.document = document;
 113             this.index = index;
 114         }
 115 
 116         public void run() {
 117 
 118             try {
 119                 System.out.printf("Waiting for barrier: %s%n", index);
 120                 BARRIER.await();
 121                 System.out.println("Validating....");
 122 






 123                 Validator validator = schema.newValidator();
 124                 validator.setErrorHandler(new ErrorHandlerImpl());
 125                 validator.validate(new DOMSource(document));

 126             } catch (IOException e) {
 127                 e.printStackTrace();
 128             } catch (SAXException e) {
 129                 e.printStackTrace();
 130                 Assert.fail("Test failed.");


 131             } catch (BrokenBarrierException e) {
 132                 e.printStackTrace();
 133             } catch (InterruptedException e) {
 134                 e.printStackTrace();
 135             }
 136 
 137         }
 138     }
 139 
 140     private static class ErrorHandlerImpl implements ErrorHandler {
 141 
 142         public void warning(SAXParseException exception) throws SAXException {
 143             System.out
 144                     .printf("**Parsing Warning. Line: %s  URI: %s  Message: %s%n", exception.getLineNumber(), exception.getSystemId(), exception.getMessage());
 145         }
 146 
 147         public void error(SAXParseException exception) throws SAXException {
 148             String msg = String.format("**Parsing Error. Line: %s  URI: %s  Message: %s%n", exception.getLineNumber(), exception.getSystemId(),
 149                     exception.getMessage());
 150             System.out.println(msg);
< prev index next >