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