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
24package transform;
25
26import static jaxp.library.JAXPTestUtilities.USER_DIR;
27
28import java.io.File;
29import java.io.FileInputStream;
30import java.io.FileOutputStream;
31import java.io.IOException;
32import java.net.URL;
33
34import javax.xml.parsers.SAXParser;
35import javax.xml.parsers.SAXParserFactory;
36import javax.xml.transform.Transformer;
37import javax.xml.transform.TransformerFactory;
38import javax.xml.transform.stream.StreamResult;
39import javax.xml.transform.stream.StreamSource;
40
41import org.testng.Assert;
42import org.testng.annotations.Listeners;
43import org.testng.annotations.Test;
44import org.xml.sax.InputSource;
45import org.xml.sax.helpers.DefaultHandler;
46
47/*
48 * @test
49 * @bug 4693341
50 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
51 * @run testng/othervm -DrunSecMngr=true transform.Bug4693341Test
52 * @run testng/othervm transform.Bug4693341Test
53 * @summary Test transform with external dtd.
54 */
55@Listeners({jaxp.library.FilePolicy.class})
56public class Bug4693341Test {
57    // save dtd file to current working directory to avoid writing into source repository
58    public void copyDTDtoWorkDir() throws IOException {
59        try (FileInputStream dtdres = new FileInputStream(getClass().getResource("Bug4693341.dtd").getPath());
60             FileOutputStream dtdwork = new FileOutputStream(USER_DIR + "Bug4693341.dtd");) {
61            int n;
62            byte[] buffer = new byte[1024];
63            while((n = dtdres.read(buffer)) > -1) {
64                dtdwork.write(buffer, 0, n);
65            }
66        }
67    }
68
69    @Test
70    public void test() {
71        try {
72            Transformer transformer = TransformerFactory.newInstance().newTransformer();
73
74            copyDTDtoWorkDir();
75
76            File outf = new File(USER_DIR + "Bug4693341.out");
77            StreamResult result = new StreamResult(new FileOutputStream(outf));
78
79            String in = getClass().getResource("Bug4693341.xml").getPath();
80            File file = new File(in);
81            StreamSource source = new StreamSource(new FileInputStream(file), ("file://" + in));
82
83            transformer.transform(source, result);
84
85            //URL inputsource = new URL("file", "", golden);
86            URL output = new URL("file", "", outf.getPath());
87
88            // error happens when trying to parse output
89            String systemId = output.toExternalForm();
90            System.out.println("systemId: " + systemId);
91            InputSource is = new InputSource(systemId);
92            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
93            parser.parse(is, new DefaultHandler());
94        } catch (Exception ex) {
95            Assert.fail(ex.getMessage());
96        }
97    }
98}
99