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