1/*
2 * Copyright (c) 2003, 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 */
23package javax.xml.transform.ptests;
24
25import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
26import static org.testng.Assert.assertEquals;
27import static org.testng.Assert.assertNotNull;
28import static org.testng.Assert.assertTrue;
29
30import java.io.File;
31import java.io.FileInputStream;
32import java.util.Properties;
33
34import javax.xml.parsers.DocumentBuilder;
35import javax.xml.parsers.DocumentBuilderFactory;
36import javax.xml.transform.ErrorListener;
37import javax.xml.transform.Transformer;
38import javax.xml.transform.TransformerConfigurationException;
39import javax.xml.transform.TransformerException;
40import javax.xml.transform.TransformerFactory;
41import javax.xml.transform.dom.DOMSource;
42import javax.xml.transform.sax.SAXSource;
43import javax.xml.transform.stream.StreamSource;
44
45import org.testng.annotations.Listeners;
46import org.testng.annotations.Test;
47import org.w3c.dom.Document;
48import org.xml.sax.InputSource;
49
50/**
51 * Basic test cases for Transformer API
52 */
53/*
54 * @test
55 * @library /javax/xml/jaxp/libs
56 * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.TransformerTest
57 * @run testng/othervm javax.xml.transform.ptests.TransformerTest
58 */
59@Listeners({jaxp.library.FilePolicy.class})
60public class TransformerTest {
61    /**
62     * XSLT file serves every test method.
63     */
64    private final static String TEST_XSL = XML_DIR + "cities.xsl";
65
66    /**
67     * This tests if newTransformer(StreamSource) method returns Transformer.
68     * @throws TransformerConfigurationException If for some reason the
69     *         TransformerHandler can not be created.
70     */
71    @Test
72    public void transformer01() throws TransformerConfigurationException {
73        TransformerFactory tfactory = TransformerFactory.newInstance();
74        StreamSource streamSource = new StreamSource(
75                                    new File(TEST_XSL));
76        Transformer transformer = tfactory.newTransformer(streamSource);
77        assertNotNull(transformer);
78    }
79
80    /**
81     * This tests if newTransformer(SAXSource) method returns Transformer.
82     * @throws Exception If any errors occur.
83     */
84    @Test
85    public void transformer02() throws Exception {
86        try (FileInputStream fis = new FileInputStream(TEST_XSL)) {
87            TransformerFactory tfactory = TransformerFactory.newInstance();
88            SAXSource saxSource = new SAXSource(new InputSource(fis));
89            Transformer transformer = tfactory.newTransformer(saxSource);
90            assertNotNull(transformer);
91        }
92    }
93
94    /**
95     * This tests if newTransformer(DOMSource) method returns Transformer.
96     *
97     * @throws Exception If any errors occur.
98     */
99    @Test
100    public void transformer03() throws Exception {
101        TransformerFactory tfactory = TransformerFactory.newInstance();
102
103        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
104        dbf.setNamespaceAware(true);
105        DocumentBuilder db = dbf.newDocumentBuilder();
106        Document document = db.parse(new File(TEST_XSL));
107        DOMSource domSource = new DOMSource(document);
108
109        Transformer transformer = tfactory.newTransformer(domSource);
110        assertNotNull(transformer);
111    }
112
113    /**
114     * This tests set/get ErrorListener methods of Transformer.
115     *
116     * @throws Exception If any errors occur.
117     */
118    @Test
119    public void transformer04() throws Exception {
120        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
121        dbf.setNamespaceAware(true);
122        DocumentBuilder db = dbf.newDocumentBuilder();
123        Document document = db.parse(new File(TEST_XSL));
124        DOMSource domSource = new DOMSource(document);
125
126        Transformer transformer = TransformerFactory.newInstance()
127                .newTransformer(domSource);
128        transformer.setErrorListener(new MyErrorListener());
129        assertNotNull(transformer.getErrorListener());
130        assertTrue(transformer.getErrorListener() instanceof MyErrorListener);
131    }
132
133    /**
134     * This tests getOutputProperties() method of Transformer.
135     *
136     * @throws Exception If any errors occur.
137     */
138    @Test
139    public void transformer05() throws Exception {
140        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
141        dbf.setNamespaceAware(true);
142        DocumentBuilder db = dbf.newDocumentBuilder();
143        Document document = db.parse(new File(TEST_XSL));
144        DOMSource domSource = new DOMSource(document);
145
146        Transformer transformer = TransformerFactory.newInstance().
147                newTransformer(domSource);
148        Properties prop = transformer.getOutputProperties();
149
150        assertEquals(prop.getProperty("indent"), "yes");
151        assertEquals(prop.getProperty("method"), "xml");
152        assertEquals(prop.getProperty("encoding"), "UTF-8");
153        assertEquals(prop.getProperty("standalone"), "no");
154        assertEquals(prop.getProperty("version"), "1.0");
155        assertEquals(prop.getProperty("omit-xml-declaration"), "no");
156    }
157
158    /**
159     * This tests getOutputProperty() method of Transformer.
160     *
161     * @throws Exception If any errors occur.
162     */
163    @Test
164    public void transformer06() throws Exception {
165        TransformerFactory tfactory = TransformerFactory.newInstance();
166
167        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
168        dbf.setNamespaceAware(true);
169        DocumentBuilder db = dbf.newDocumentBuilder();
170        Document document = db.parse(new File(TEST_XSL));
171        DOMSource domSource = new DOMSource(document);
172
173        Transformer transformer = tfactory.newTransformer(domSource);
174        assertEquals(transformer.getOutputProperty("method"), "xml");
175    }
176}
177
178/**
179 * Simple ErrorListener print out all exception.
180 */
181class MyErrorListener implements ErrorListener {
182    /**
183     * Prints exception when notification of a recoverable error.
184     * @param e exception of a recoverable error.
185     */
186    @Override
187    public void error (TransformerException e) {
188        System.out.println(" In error" + e);
189    }
190
191    /**
192     * Prints exception when notification of a warning.
193     * @param e exception of a warning.
194     */
195    @Override
196    public void warning (TransformerException e) {
197        System.out.println(" In warning");
198    }
199
200    /**
201     * Prints exception when notification of a fatal error.
202     * @param e exception of a fatal error.
203     */
204    @Override
205    public void fatalError (TransformerException e) throws
206                TransformerException {
207        System.out.println(" In fatal");
208    }
209}
210