EntityTest.java revision 997:540334ae53fe
1/*
2 * Copyright (c) 2014, 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 stream.EntitiesTest;
25
26import java.io.IOException;
27import java.io.InputStreamReader;
28import java.io.LineNumberReader;
29import java.io.Reader;
30import java.io.StringReader;
31import java.net.URL;
32
33import javax.xml.stream.XMLInputFactory;
34import javax.xml.stream.XMLStreamReader;
35import javax.xml.stream.events.XMLEvent;
36
37import org.testng.Assert;
38import org.testng.annotations.AfterMethod;
39import org.testng.annotations.BeforeMethod;
40import org.testng.annotations.Listeners;
41import org.testng.annotations.Test;
42
43/*
44 * @test
45 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
46 * @run testng/othervm -DrunSecMngr=true stream.EntitiesTest.EntityTest
47 * @run testng/othervm stream.EntitiesTest.EntityTest
48 * @summary Test StAX parses entity.
49 */
50@Listeners({jaxp.library.FilePolicy.class})
51public class EntityTest {
52
53    XMLInputFactory factory = null;
54    String output = "";
55
56    @BeforeMethod
57    public void setUp() {
58        try {
59            factory = XMLInputFactory.newInstance();
60        } catch (Exception ex) {
61            Assert.fail("Could not create XMLInputFactory");
62        }
63    }
64
65    @AfterMethod
66    public void tearDown() {
67        factory = null;
68    }
69
70    @Test
71    public void testProperties() {
72        Assert.assertTrue(factory.isPropertySupported("javax.xml.stream.isReplacingEntityReferences"));
73    }
74
75    @Test
76    public void testCharacterReferences() {
77        try {
78            URL fileName = EntityTest.class.getResource("testCharRef.xml");
79            URL outputFileName = EntityTest.class.getResource("testCharRef.xml.output");
80            XMLStreamReader xmlr = factory.createXMLStreamReader(new InputStreamReader(fileName.openStream()));
81            int eventType = 0;
82            while (xmlr.hasNext()) {
83                eventType = xmlr.next();
84                handleEvent(xmlr, eventType);
85            }
86            System.out.println("Output:");
87            System.out.println(output);
88            Assert.assertTrue(compareOutput(new InputStreamReader(outputFileName.openStream()), new StringReader(output)));
89        } catch (Exception ex) {
90            ex.printStackTrace();
91            Assert.fail(ex.getMessage());
92        }
93    }
94
95    private void handleEvent(XMLStreamReader xmlr, int eventType) {
96        switch (eventType) {
97            case XMLEvent.START_ELEMENT:
98                handleStartElement(xmlr);
99                break;
100            case XMLEvent.END_ELEMENT:
101                handleEndElement(xmlr);
102                break;
103            case XMLEvent.CHARACTERS:
104                handleCharacters(xmlr);
105                break;
106            case XMLEvent.COMMENT:
107                handleComment(xmlr);
108                break;
109            case XMLEvent.ENTITY_REFERENCE:
110                break;
111            case XMLEvent.ATTRIBUTE:
112                break;
113            case XMLEvent.DTD:
114                break;
115            case XMLEvent.CDATA:
116                break;
117            default:
118                break;
119        }
120    }
121
122    private void handleStartElement(XMLStreamReader xmlr) {
123        output += "<";
124        output += xmlr.getLocalName();
125        if (xmlr.hasText())
126            output += xmlr.getText();
127        printAttributes(xmlr);
128        output += ">";
129    }
130
131    private void handleEndElement(XMLStreamReader xmlr) {
132        output += "</";
133        output += xmlr.getLocalName();
134        output += ">";
135    }
136
137    private void handleComment(XMLStreamReader xmlr) {
138        if (xmlr.hasText())
139            output += xmlr.getText();
140    }
141
142    private void handleCharacters(XMLStreamReader xmlr) {
143        if (xmlr.hasText())
144            output += xmlr.getText();
145    }
146
147    private void printAttributes(XMLStreamReader xmlr) {
148        if (xmlr.getAttributeCount() > 0) {
149            int count = xmlr.getAttributeCount();
150            for (int i = 0; i < count; i++) {
151                output += xmlr.getAttributeName(i);
152                output += "=";
153                output += xmlr.getAttributeValue(i);
154                /*
155                 * String name = xmlr.getAttributeName(i) ; String value =
156                 * xmlr.getAttributeValue(i) ;
157                 * System.out.println(name+"="+value);
158                 */
159            }
160        }
161    }
162
163    protected boolean compareOutput(Reader expected, Reader actual) throws IOException {
164        LineNumberReader expectedOutput = new LineNumberReader(expected);
165        LineNumberReader actualOutput = new LineNumberReader(actual);
166
167        while (expectedOutput.ready() && actualOutput.ready()) {
168            String expectedLine = expectedOutput.readLine();
169            String actualLine = actualOutput.readLine();
170            if (!expectedLine.equals(actualLine)) {
171                System.out.println("Entityreference expansion failed, line no: " + expectedOutput.getLineNumber());
172                System.out.println("Expected: " + expectedLine);
173                System.out.println("Actual  : " + actualLine);
174                return false;
175            }
176        }
177        expectedOutput.close();
178        actualOutput.close();
179        return true;
180    }
181}
182