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