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