1 /*
   2  * Copyright (c) 1999, 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 javax.xml.transform.ptests;
  25 
  26 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  27 
  28 import java.io.BufferedWriter;
  29 import java.io.ByteArrayInputStream;
  30 import java.io.ByteArrayOutputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.OutputStream;
  34 import java.io.OutputStreamWriter;
  35 import java.nio.file.Files;
  36 import java.nio.file.Paths;
  37 import java.util.function.Supplier;
  38 
  39 import javax.xml.parsers.DocumentBuilder;
  40 import javax.xml.parsers.DocumentBuilderFactory;
  41 import javax.xml.stream.XMLEventWriter;
  42 import javax.xml.stream.XMLInputFactory;
  43 import javax.xml.stream.XMLOutputFactory;
  44 import javax.xml.stream.XMLStreamException;
  45 import javax.xml.stream.XMLStreamReader;
  46 import javax.xml.stream.XMLStreamWriter;
  47 import javax.xml.transform.Result;
  48 import javax.xml.transform.Source;
  49 import javax.xml.transform.Transformer;
  50 import javax.xml.transform.TransformerConfigurationException;
  51 import javax.xml.transform.TransformerFactory;
  52 import javax.xml.transform.dom.DOMSource;
  53 import javax.xml.transform.sax.SAXResult;
  54 import javax.xml.transform.sax.SAXSource;
  55 import javax.xml.transform.stax.StAXResult;
  56 import javax.xml.transform.stax.StAXSource;
  57 import javax.xml.transform.stream.StreamResult;
  58 import javax.xml.transform.stream.StreamSource;
  59 
  60 import org.testng.annotations.BeforeClass;
  61 import org.testng.annotations.DataProvider;
  62 import org.testng.annotations.Listeners;
  63 import org.testng.annotations.Test;
  64 import org.w3c.dom.Document;
  65 import org.xml.sax.Attributes;
  66 import org.xml.sax.ContentHandler;
  67 import org.xml.sax.InputSource;
  68 import org.xml.sax.Locator;
  69 import org.xml.sax.SAXException;
  70 
  71 /*
  72  * @test
  73  * @library /javax/xml/jaxp/libs
  74  * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.TransformTest
  75  * @run testng/othervm javax.xml.transform.ptests.TransformTest
  76  * @summary Tests for variable combination of Transformer.transform(Source, Result)
  77  */
  78 @Test(singleThreaded = true)
  79 @Listeners({jaxp.library.FilePolicy.class})
  80 public class TransformTest {
  81 
  82     /*
  83      * Initialize the share objects.
  84      */
  85     @BeforeClass
  86     public void setup() throws Exception {
  87         ifac = XMLInputFactory.newInstance();
  88         ofac = XMLOutputFactory.newInstance();
  89         tfac = TransformerFactory.newInstance();
  90 
  91         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  92         dbf.setNamespaceAware(true);
  93         db = dbf.newDocumentBuilder();
  94 
  95         xml = Files.readAllBytes(Paths.get(XML_DIR + "cities.xml"));
  96         template = Files.readAllBytes(Paths.get(XML_DIR + "cities.xsl"));
  97 
  98         xmlDoc = db.parse(xmlInputStream());
  99     }
 100 
 101     @DataProvider(name = "input-provider")
 102     public Object[][] prepareTestCombination() throws Exception {
 103 
 104         Supplier<Source> staxStreamSource = () -> new StAXSource(getXMLStreamReader());
 105         Supplier<Source> staxEventSource = this::getStAXEventSource;
 106         Supplier<Source> domSource = () -> new DOMSource(xmlDoc);
 107         Supplier<Source> saxSource = () -> new SAXSource(new InputSource(xmlInputStream()));
 108         Supplier<Source> streamSource = () -> new StreamSource(xmlInputStream());
 109 
 110         Supplier<Result> staxStreamResult = () -> new StAXResult(getXMLStreamWriter());
 111         Supplier<Result> staxEventResult = () -> new StAXResult(getXMLEventWriter());
 112         Supplier<Result> saxResult = this::getHandlerSAXResult;
 113         Supplier<Result> streamResult = () -> new StreamResult(transOutputStream());
 114 
 115         Transformer domTemplateTransformer = createTransformer(getDomTemplate());
 116         Transformer saxTemplateTransformer = createTransformer(getSAXTemplate());
 117         Transformer streamTemplateTransformer = createTransformer(getStreamTemplate());
 118         Transformer noTemplateTransformer = createTransformer(null);
 119         Transformer staxStreamTemplateTransformer = createTransformer(getStAXStreamTemplate());
 120         Transformer staxEventTemplateTransformer = createTransformer(getStAXEventTemplate());
 121 
 122         return new Object[][] {
 123                 // StAX Stream
 124                 { staxStreamSource, staxStreamResult, domTemplateTransformer },
 125                 { staxStreamSource, staxStreamResult, saxTemplateTransformer },
 126                 { staxStreamSource, staxStreamResult, streamTemplateTransformer },
 127                 { staxStreamSource, staxStreamResult, noTemplateTransformer },
 128                 { staxStreamSource, staxStreamResult, staxStreamTemplateTransformer },
 129                 { staxStreamSource, saxResult, domTemplateTransformer },
 130                 { staxStreamSource, streamResult, domTemplateTransformer },
 131                 { domSource, staxStreamResult, domTemplateTransformer },
 132                 { saxSource, staxStreamResult, domTemplateTransformer },
 133                 { streamSource, staxStreamResult, domTemplateTransformer },
 134                 { staxStreamSource, streamResult, saxTemplateTransformer },
 135                 { domSource, staxStreamResult, saxTemplateTransformer },
 136                 { saxSource, staxStreamResult, saxTemplateTransformer },
 137                 { streamSource, staxStreamResult, saxTemplateTransformer },
 138                 { staxStreamSource, streamResult, streamTemplateTransformer },
 139                 { domSource, staxStreamResult, streamTemplateTransformer },
 140                 { saxSource, staxStreamResult, streamTemplateTransformer },
 141                 { streamSource, staxStreamResult, streamTemplateTransformer },
 142                 // StAX Event
 143                 { staxEventSource, staxEventResult, domTemplateTransformer },
 144                 { staxEventSource, staxEventResult, saxTemplateTransformer },
 145                 { staxEventSource, staxEventResult, streamTemplateTransformer },
 146                 { staxEventSource, staxEventResult, noTemplateTransformer },
 147                 { staxEventSource, staxEventResult, staxEventTemplateTransformer },
 148                 { staxEventSource, saxResult, domTemplateTransformer },
 149                 { staxEventSource, streamResult, domTemplateTransformer },
 150                 { domSource, staxEventResult, domTemplateTransformer },
 151                 { saxSource, staxEventResult, domTemplateTransformer },
 152                 { streamSource, staxEventResult, domTemplateTransformer },
 153                 { staxEventSource, streamResult, saxTemplateTransformer },
 154                 { domSource, staxEventResult, saxTemplateTransformer },
 155                 { saxSource, staxEventResult, saxTemplateTransformer },
 156                 { streamSource, staxEventResult, saxTemplateTransformer },
 157                 { staxEventSource, streamResult, streamTemplateTransformer },
 158                 { domSource, staxEventResult, streamTemplateTransformer },
 159                 { saxSource, staxEventResult, streamTemplateTransformer },
 160                 { streamSource, staxEventResult, streamTemplateTransformer } };
 161     }
 162 
 163     /*
 164      * run Transformer.transform(Source, Result)
 165      */
 166     @Test(dataProvider = "input-provider")
 167     public void testTransform(Supplier<Source> src, Supplier<Result> res, Transformer transformer) throws Throwable {
 168         try {
 169             transformer.transform(src.get(), res.get());
 170         } catch (WrapperException e) {
 171             throw e.getCause();
 172         }
 173     }
 174 
 175     private InputStream xmlInputStream() {
 176         return new ByteArrayInputStream(xml);
 177     }
 178 
 179     private InputStream templateInputStream() {
 180         return new ByteArrayInputStream(template);
 181     }
 182 
 183     private OutputStream transOutputStream() {
 184         return new ByteArrayOutputStream(xml.length);
 185     }
 186 
 187     private XMLStreamReader getXMLStreamReader() {
 188         try {
 189             return ifac.createXMLStreamReader(xmlInputStream());
 190         } catch (XMLStreamException e) {
 191             throw new WrapperException(e);
 192         }
 193     }
 194 
 195     private XMLStreamWriter getXMLStreamWriter() {
 196         try {
 197             return ofac.createXMLStreamWriter(transOutputStream());
 198         } catch (XMLStreamException e) {
 199             throw new WrapperException(e);
 200         }
 201     }
 202 
 203     private StAXSource getStAXEventSource() {
 204         try {
 205             return new StAXSource(ifac.createXMLEventReader(xmlInputStream()));
 206         } catch (XMLStreamException e) {
 207             throw new WrapperException(e);
 208         }
 209     }
 210 
 211     private XMLEventWriter getXMLEventWriter() {
 212         try {
 213             return ofac.createXMLEventWriter(transOutputStream());
 214         } catch (XMLStreamException e) {
 215             throw new WrapperException(e);
 216         }
 217     }
 218 
 219     private SAXResult getHandlerSAXResult() {
 220         SAXResult res = new SAXResult();
 221         MyContentHandler myContentHandler = new MyContentHandler(transOutputStream());
 222         res.setHandler(myContentHandler);
 223         return res;
 224     }
 225 
 226     private Source getDomTemplate() throws SAXException, IOException {
 227         return new DOMSource(db.parse(templateInputStream()));
 228     }
 229 
 230     private Source getSAXTemplate() {
 231         return new SAXSource(new InputSource(templateInputStream()));
 232     }
 233 
 234     private Source getStreamTemplate() {
 235         return new StreamSource(templateInputStream());
 236     }
 237 
 238     private Source getStAXStreamTemplate() throws XMLStreamException {
 239         return new StAXSource(ifac.createXMLStreamReader(templateInputStream()));
 240     }
 241 
 242     private Source getStAXEventTemplate() throws XMLStreamException {
 243         return new StAXSource(ifac.createXMLEventReader(templateInputStream()));
 244     }
 245 
 246     private Transformer createTransformer(Source templateSource) throws TransformerConfigurationException {
 247         Transformer transformer = (templateSource == null) ? tfac.newTransformer() : tfac.newTransformer(templateSource);
 248         transformer.setOutputProperty("indent", "yes");
 249         return transformer;
 250 
 251     }
 252 
 253     private static class MyContentHandler implements ContentHandler {
 254         private BufferedWriter bWriter;
 255 
 256         public MyContentHandler(OutputStream os) {
 257             bWriter = new BufferedWriter(new OutputStreamWriter(os));
 258         }
 259 
 260         public void setDocumentLocator(Locator locator) {
 261         }
 262 
 263         public void startDocument() throws SAXException {
 264             String str = "startDocument";
 265             try {
 266                 bWriter.write(str, 0, str.length());
 267                 bWriter.newLine();
 268             } catch (IOException e) {
 269                 System.out.println("bWriter error");
 270             }
 271         }
 272 
 273         public void endDocument() throws SAXException {
 274             String str = "endDocument";
 275             try {
 276                 bWriter.write(str, 0, str.length());
 277                 bWriter.newLine();
 278                 bWriter.flush();
 279                 bWriter.close();
 280             } catch (IOException e) {
 281                 System.out.println("bWriter error");
 282             }
 283         }
 284 
 285         public void startPrefixMapping(String prefix, String uri) throws SAXException {
 286             String str = "startPrefixMapping: " + prefix + ", " + uri;
 287             try {
 288                 bWriter.write(str, 0, str.length());
 289                 bWriter.newLine();
 290             } catch (IOException e) {
 291                 System.out.println("bWriter error");
 292             }
 293         }
 294 
 295         public void endPrefixMapping(String prefix) throws SAXException {
 296             String str = "endPrefixMapping: " + prefix;
 297             try {
 298                 bWriter.write(str, 0, str.length());
 299                 bWriter.newLine();
 300             } catch (IOException e) {
 301                 System.out.println("bWriter error");
 302             }
 303         }
 304 
 305         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
 306             StringBuilder str = new StringBuilder("startElement: ").append(namespaceURI).append(", ").append(namespaceURI).append(", ").append(qName).append(" : ");
 307             int n = atts.getLength();
 308             for (int i = 0; i < n; i++) {
 309                 str.append(", ").append(atts.getQName(i)).append(" : ").append(atts.getValue(i));
 310             }
 311 
 312             try {
 313                 bWriter.write(str.toString(), 0, str.length());
 314                 bWriter.newLine();
 315             } catch (IOException e) {
 316                 System.out.println("bWriter error");
 317             }
 318         }
 319 
 320         public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
 321             String str = "endElement: " + namespaceURI + ", " + namespaceURI + ", " + qName;
 322             try {
 323                 bWriter.write(str, 0, str.length());
 324                 bWriter.newLine();
 325             } catch (IOException e) {
 326                 System.out.println("bWriter error");
 327             }
 328 
 329         }
 330 
 331         public void characters(char ch[], int start, int length) throws SAXException {
 332             String str = new String(ch, start, length);
 333             try {
 334                 bWriter.write(str, 0, str.length());
 335                 bWriter.newLine();
 336             } catch (IOException e) {
 337                 System.out.println("bWriter error");
 338             }
 339         }
 340 
 341         public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
 342             String str = "ignorableWhitespace";
 343             try {
 344                 bWriter.write(str, 0, str.length());
 345                 bWriter.newLine();
 346             } catch (IOException e) {
 347                 System.out.println("bWriter error");
 348             }
 349         }
 350 
 351         public void processingInstruction(String target, String data) throws SAXException {
 352             String str = "processingInstruction: " + target + ", " + target;
 353             try {
 354                 bWriter.write(str, 0, str.length());
 355                 bWriter.newLine();
 356             } catch (IOException e) {
 357                 System.out.println("bWriter error");
 358             }
 359         }
 360 
 361         public void skippedEntity(String name) throws SAXException {
 362             String str = "skippedEntity: " + name;
 363             try {
 364                 bWriter.write(str, 0, str.length());
 365                 bWriter.newLine();
 366             } catch (IOException e) {
 367                 System.out.println("bWriter error");
 368             }
 369         }
 370     }
 371 
 372     private static class WrapperException extends RuntimeException {
 373         public WrapperException(Throwable cause) {
 374             super(cause);
 375         }
 376     }
 377 
 378     private XMLInputFactory ifac;
 379     private XMLOutputFactory ofac;
 380     private TransformerFactory tfac;
 381     private DocumentBuilder db;
 382     private byte[] xml;
 383     private byte[] template;
 384     private Document xmlDoc;
 385 
 386 }