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 transform; 25 26 import java.io.File; 27 import java.io.StringWriter; 28 29 import javax.xml.transform.Source; 30 import javax.xml.transform.Transformer; 31 import javax.xml.transform.TransformerFactory; 32 import javax.xml.transform.dom.DOMSource; 33 import javax.xml.transform.sax.SAXSource; 34 import javax.xml.transform.stream.StreamResult; 35 import javax.xml.transform.stream.StreamSource; 36 37 import org.testng.Assert; 38 import org.testng.annotations.Listeners; 39 import org.testng.annotations.Test; 40 41 /* 42 * @summary Test Sources. 43 */ 44 @Listeners({jaxp.library.FilePolicy.class}) 45 public class SourceTest { 46 47 @Test 48 public final void testDOMSource() { 49 String xml = getClass().getResource("SourceTest.xml").getFile(); 50 xml = "file://" + xml; 51 File xsl = new File(getClass().getResource("SourceTest.xsl").getFile()); 52 try { 53 TransformerFactory tFactory = TransformerFactory.newInstance(); 54 Transformer transformer = tFactory.newTransformer(); 55 StreamSource source = new StreamSource(xsl); 56 transformer = tFactory.newTransformer(source); 57 // the xml result 58 StringWriter xmlResultString = new StringWriter(); 59 StreamResult xmlResultStream = new StreamResult(xmlResultString); 60 61 Source xmlSource = new DOMSource(); 62 xmlSource.setSystemId(xml); 63 64 transformer.transform(xmlSource, xmlResultStream); 65 System.out.println(xmlResultString.toString()); 66 String temp = xmlResultString.toString(); 67 int pos = temp.lastIndexOf("count"); 68 if (temp.substring(pos + 8, pos + 9).equals("1")) { 69 Assert.fail("count=1"); 70 } else if (temp.substring(pos + 8, pos + 9).equals("2")) { 71 // expected success 72 System.out.println("count=2"); 73 } 74 } catch (Exception e) { 75 // unexpected failure 76 e.printStackTrace(); 77 Assert.fail(e.toString()); 78 } 79 } 80 81 @Test 82 public final void testSAXSource() { 83 String xml = getClass().getResource("SourceTest.xml").getFile(); 84 File xsl = new File(getClass().getResource("SourceTest.xsl").getFile()); 85 try { 86 TransformerFactory tFactory = TransformerFactory.newInstance(); 87 Transformer transformer = tFactory.newTransformer(); 88 StreamSource source = new StreamSource(xsl); 89 transformer = tFactory.newTransformer(source); 90 // the xml result 91 StringWriter xmlResultString = new StringWriter(); 92 StreamResult xmlResultStream = new StreamResult(xmlResultString); 93 94 Source xmlSource = new SAXSource(); 95 xmlSource.setSystemId(xml); 96 97 transformer.transform(xmlSource, xmlResultStream); 98 System.out.println(xmlResultString.toString()); 99 String temp = xmlResultString.toString(); 100 int pos = temp.lastIndexOf("count"); 101 if (temp.substring(pos + 8, pos + 9).equals("1")) { 102 Assert.fail("count=1"); 103 } else if (temp.substring(pos + 8, pos + 9).equals("2")) { 104 // expected success 105 System.out.println("count=2"); 106 } 107 } catch (Exception e) { 108 // unexpected failure 109 e.printStackTrace(); 110 Assert.fail(e.toString()); 111 } 112 } 113 114 @Test 115 public final void testStreamSource() { 116 String xml = getClass().getResource("SourceTest.xml").getFile(); 117 File xsl = new File(getClass().getResource("SourceTest.xsl").getFile()); 118 try { 119 TransformerFactory tFactory = TransformerFactory.newInstance(); 120 Transformer transformer = tFactory.newTransformer(); 121 StreamSource source = new StreamSource(xsl); 122 transformer = tFactory.newTransformer(source); 123 // the xml result 124 StringWriter xmlResultString = new StringWriter(); 125 StreamResult xmlResultStream = new StreamResult(xmlResultString); 126 127 Source xmlSource = new StreamSource(); 128 xmlSource.setSystemId(xml); 129 130 transformer.transform(xmlSource, xmlResultStream); 131 System.out.println(xmlResultString.toString()); 132 String temp = xmlResultString.toString(); 133 int pos = temp.lastIndexOf("count"); 134 if (temp.substring(pos + 8, pos + 9).equals("1")) { 135 Assert.fail("count=1"); 136 } else if (temp.substring(pos + 8, pos + 9).equals("2")) { 137 // expected success 138 System.out.println("count=2"); 139 } 140 } catch (Exception e) { 141 // unexpected failure 142 e.printStackTrace(); 143 Assert.fail(e.toString()); 144 } 145 } 146 }