1 /* 2 * Copyright (c) 2014, 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 package stream; 25 26 import java.io.FileInputStream; 27 import java.io.FileOutputStream; 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 31 import javax.xml.stream.XMLInputFactory; 32 import javax.xml.stream.XMLOutputFactory; 33 import javax.xml.stream.XMLStreamReader; 34 import javax.xml.stream.XMLStreamWriter; 35 36 import org.testng.Assert; 37 import org.testng.annotations.Listeners; 38 import org.testng.annotations.Test; 39 40 /* 41 * @test 42 * @bug 6688002 43 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 44 * @run testng/othervm -DrunSecMngr=true stream.Bug6688002Test 45 * @run testng/othervm stream.Bug6688002Test 46 * @summary Test single instance of XMLOutputFactory/XMLInputFactory create multiple Writer/Readers in parallel. 47 */ 48 @Listeners({jaxp.library.FilePolicy.class}) 49 public class Bug6688002Test { 50 51 private static final XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); 52 private static final XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 53 private static final int NO_THREADS = 3; 54 55 @Test 56 public void testMultiThread() throws Exception { 57 Thread[] threads = new Thread[NO_THREADS]; 58 for (int i = 0; i < NO_THREADS; i++) { 59 threads[i] = new Thread(new MyRunnable(i)); 60 } 61 for (int i = 0; i < NO_THREADS; i++) { 62 threads[i].start(); 63 } 64 for (int i = 0; i < NO_THREADS; i++) { 65 threads[i].join(); 66 } 67 } 68 69 public class MyRunnable implements Runnable { 70 final int no; 71 72 MyRunnable(int no) { 73 this.no = no; 74 } 75 76 public void run() { 77 try { 78 FileOutputStream fos = new FileOutputStream("" + no); 79 XMLStreamWriter w = getWriter(fos); 80 // System.out.println("Writer="+w+" Thread="+Thread.currentThread()); 81 w.writeStartDocument(); 82 w.writeStartElement("hello"); 83 for (int j = 0; j < 50; j++) { 84 w.writeStartElement("a" + j); 85 w.writeEndElement(); 86 } 87 w.writeEndElement(); 88 w.writeEndDocument(); 89 w.close(); 90 fos.close(); 91 92 FileInputStream fis = new FileInputStream("" + no); 93 XMLStreamReader r = getReader(fis); 94 while (r.hasNext()) { 95 r.next(); 96 } 97 r.close(); 98 fis.close(); 99 } catch (Exception e) { 100 Assert.fail(e.getMessage()); 101 } 102 } 103 } 104 105 public static/* synchronized */XMLStreamReader getReader(InputStream is) throws Exception { 106 return inputFactory.createXMLStreamReader(is); 107 // return XMLStreamReaderFactory.create(null, is, true); 108 } 109 110 public static/* synchronized */XMLStreamWriter getWriter(OutputStream os) throws Exception { 111 return outputFactory.createXMLStreamWriter(os); 112 // return XMLStreamWriterFactory.createXMLStreamWriter(os); 113 } 114 115 } 116