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