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 transform;
  25 
  26 import org.testng.annotations.Test;
  27 import org.testng.Assert;
  28 
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.FileNotFoundException;
  32 import java.io.IOException;
  33 
  34 import javax.xml.parsers.DocumentBuilder;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerConfigurationException;
  39 import javax.xml.transform.TransformerException;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.dom.DOMResult;
  42 import javax.xml.transform.dom.DOMSource;
  43 
  44 import org.w3c.dom.Document;
  45 import org.w3c.dom.Node;
  46 import org.xml.sax.SAXException;
  47 
  48 /*
  49  * @summary Test DOMResult.
  50  */
  51 public class DOMResultTest {
  52 
  53     @Test
  54     public void testDOMResult1() {
  55         try {
  56             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  57             DocumentBuilder db = dbf.newDocumentBuilder();
  58             String xml = this.getClass().getResource("toys.xml").getFile();
  59             Document doc = db.parse(new FileInputStream(new File(xml)));
  60             TransformerFactory tff = TransformerFactory.newInstance();
  61             Transformer tf = tff.newTransformer();
  62             // get <toys> element node
  63             Node toys = doc.getChildNodes().item(1);
  64             // supposed to insert new node at index=4
  65             int index = 4;
  66             String systemId = "customSysId";
  67             DOMResult result = new DOMResult(toys, systemId);
  68             result.setNextSibling(result.getNode().getChildNodes().item(index));
  69             int length = result.getNode().getChildNodes().getLength();
  70             // copy the first <toy> element node and insert it to position
  71             // marked by index
  72             Node source = doc.getLastChild().getChildNodes().item(1);
  73             tf.transform(new DOMSource(source), result);
  74 
  75             // document length verification
  76             if (result.getNode().getChildNodes().getLength() != length + 1) {
  77                 Assert.fail("incorrect nodes length");
  78             }
  79             // element content verification
  80             Node newnode = result.getNode().getChildNodes().item(index);
  81             System.out.println(newnode.getTextContent());
  82             if (!source.getTextContent().equals(newnode.getTextContent())) {
  83                 Assert.fail("target node content is not matched with source");
  84             }
  85             // element systemid verification
  86             if (!result.getSystemId().equals(systemId)) {
  87                 Assert.fail("systemId is not matched");
  88             }
  89         } catch (ParserConfigurationException e) {
  90             e.printStackTrace();
  91         } catch (FileNotFoundException e) {
  92             e.printStackTrace();
  93         } catch (SAXException e) {
  94             e.printStackTrace();
  95             Assert.fail(e.getMessage());
  96         } catch (IOException e) {
  97             e.printStackTrace();
  98             Assert.fail(e.getMessage());
  99         } catch (TransformerConfigurationException e) {
 100             e.printStackTrace();
 101             Assert.fail(e.getMessage());
 102         } catch (TransformerException e) {
 103             e.printStackTrace();
 104             Assert.fail(e.getMessage());
 105         }
 106     }
 107 
 108     @Test
 109     public void testDOMResult2() {
 110         try {
 111             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 112             DocumentBuilder db = dbf.newDocumentBuilder();
 113             String xml = this.getClass().getResource("toys.xml").getFile();
 114             Document doc = db.parse(new FileInputStream(new File(xml)));
 115             TransformerFactory tff = TransformerFactory.newInstance();
 116             Transformer tf = tff.newTransformer();
 117             // get <toys> element node
 118             Node toys = doc.getChildNodes().item(1);
 119             // supposed to insert new node at index=4
 120             int index = 4;
 121             String systemId = "customSysId";
 122             DOMResult result = new DOMResult(toys, toys.getChildNodes().item(index), systemId);
 123             int length = result.getNode().getChildNodes().getLength();
 124             // copy the first <toy> element node and insert it to position
 125             // marked by index
 126             Node source = doc.getLastChild().getChildNodes().item(1);
 127             tf.transform(new DOMSource(source), result);
 128 
 129             // document length verification
 130             if (result.getNode().getChildNodes().getLength() != length + 1) {
 131                 Assert.fail("incorrect nodes length");
 132             }
 133             // element content verification
 134             Node newnode = result.getNode().getChildNodes().item(index);
 135             System.out.println(newnode.getTextContent());
 136             if (!source.getTextContent().equals(newnode.getTextContent())) {
 137                 Assert.fail("target node content is not matched with source");
 138             }
 139             // element systemid verification
 140             if (!result.getSystemId().equals(systemId)) {
 141                 Assert.fail("systemId is not matched");
 142             }
 143         } catch (ParserConfigurationException e) {
 144             e.printStackTrace();
 145         } catch (FileNotFoundException e) {
 146             e.printStackTrace();
 147         } catch (SAXException e) {
 148             e.printStackTrace();
 149             Assert.fail(e.getMessage());
 150         } catch (IOException e) {
 151             e.printStackTrace();
 152             Assert.fail(e.getMessage());
 153         } catch (TransformerConfigurationException e) {
 154             e.printStackTrace();
 155             Assert.fail(e.getMessage());
 156         } catch (TransformerException e) {
 157             e.printStackTrace();
 158             Assert.fail(e.getMessage());
 159         }
 160     }
 161 
 162     @Test
 163     public void testDOMResult3() {
 164         try {
 165             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 166             DocumentBuilder db = dbf.newDocumentBuilder();
 167             String xml = this.getClass().getResource("toys.xml").getFile();
 168             Document doc = db.parse(new FileInputStream(new File(xml)));
 169             TransformerFactory tff = TransformerFactory.newInstance();
 170             Transformer tf = tff.newTransformer();
 171             // get <toys> element node
 172             Node toys = doc.getChildNodes().item(1);
 173             // supposed to insert new node at index=4
 174             int index = 4;
 175             DOMResult result = new DOMResult(toys, toys.getChildNodes().item(index));
 176             int length = result.getNode().getChildNodes().getLength();
 177             // copy the first <toy> element node and insert it to position
 178             // marked by index
 179             Node source = doc.getLastChild().getChildNodes().item(1);
 180             tf.transform(new DOMSource(source), result);
 181 
 182             // document length verification
 183             if (result.getNode().getChildNodes().getLength() != length + 1) {
 184                 Assert.fail("incorrect nodes length");
 185             }
 186             // element content verification
 187             Node newnode = result.getNode().getChildNodes().item(index);
 188             System.out.println(newnode.getTextContent());
 189             if (!source.getTextContent().equals(newnode.getTextContent())) {
 190                 Assert.fail("target node content is not matched with source");
 191             }
 192         } catch (ParserConfigurationException e) {
 193             e.printStackTrace();
 194         } catch (FileNotFoundException e) {
 195             e.printStackTrace();
 196         } catch (SAXException e) {
 197             e.printStackTrace();
 198             Assert.fail(e.getMessage());
 199         } catch (IOException e) {
 200             e.printStackTrace();
 201             Assert.fail(e.getMessage());
 202         } catch (TransformerConfigurationException e) {
 203             e.printStackTrace();
 204             Assert.fail(e.getMessage());
 205         } catch (TransformerException e) {
 206             e.printStackTrace();
 207             Assert.fail(e.getMessage());
 208         }
 209     }
 210 
 211 }