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 
  24 package stream;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileNotFoundException;
  29 import java.io.InputStream;
  30 import java.util.Iterator;
  31 
  32 import javax.xml.namespace.NamespaceContext;
  33 import javax.xml.stream.FactoryConfigurationError;
  34 import javax.xml.stream.XMLInputFactory;
  35 import javax.xml.stream.XMLStreamConstants;
  36 import javax.xml.stream.XMLStreamException;
  37 import javax.xml.stream.XMLStreamReader;
  38 import javax.xml.stream.util.StreamReaderDelegate;
  39 
  40 import org.testng.Assert;
  41 import org.testng.annotations.Listeners;
  42 import org.testng.annotations.Test;
  43 
  44 /*
  45  * @test
  46  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  47  * @run testng/othervm -DrunSecMngr=true stream.StreamReaderDelegateTest
  48  * @run testng/othervm stream.StreamReaderDelegateTest
  49  * @summary Test StreamReaderDelegate.
  50  */
  51 @Listeners({jaxp.library.FilePolicy.class})
  52 public class StreamReaderDelegateTest {
  53 
  54     /**
  55      * Tested xml file looks as below: <?xml version="1.0" standalone="no" ?>
  56      * <ns1:foo attr1="defaultAttr1" ns1:attr1="ns1Attr1" ns2:attr1="ns2Attr1"
  57      * attr2="defaultAttr2" attr3="defaultAttr3" xmlns:ns1="http://ns1.java.com"
  58      * xmlns:ns2="http://ns2.java.com"> <!--description--> content text
  59      * <![CDATA[<greeting>Hello</greeting>]]> other content </ns1:foo>
  60      **/
  61     @Test
  62     public void testAttribute() {
  63         StreamReaderDelegate delegate = null;
  64         try {
  65             System.out.println("===in testAttribute()===");
  66             XMLInputFactory ifac = XMLInputFactory.newFactory();
  67             XMLStreamReader reader = ifac.createXMLStreamReader(new FileInputStream(new File(getClass().getResource("testfile1.xml").getFile())));
  68             delegate = new StreamReaderDelegate(reader);
  69 
  70             Assert.assertTrue(delegate.standaloneSet());
  71             Assert.assertFalse(delegate.isStandalone());
  72             while (delegate.hasNext()) {
  73                 delegate.next();
  74                 if (delegate.getEventType() == XMLStreamConstants.START_ELEMENT || delegate.getEventType() == XMLStreamConstants.ATTRIBUTE) {
  75                     if (delegate.getLocalName().equals("foo")) {
  76                         Assert.assertTrue(delegate.getAttributeCount() == 5);
  77                         Assert.assertTrue(delegate.getAttributeType(1) == "CDATA");
  78 
  79                         Assert.assertTrue(delegate.getAttributeValue(0).equals("defaultAttr1"));
  80                         Assert.assertTrue(delegate.getAttributeValue(delegate.getAttributeCount() - 2).equals("defaultAttr2"));
  81                         Assert.assertTrue(delegate.getAttributeValue(delegate.getAttributeCount() - 1).equals("defaultAttr3"));
  82 
  83                         Assert.assertTrue(delegate.getAttributeValue("http://ns1.java.com", "attr1").equals("ns1Attr1"));
  84                         Assert.assertTrue(delegate.getAttributeValue("http://ns2.java.com", "attr1").equals("ns2Attr1"));
  85 
  86                         Assert.assertTrue(delegate.getAttributeValue(null, "attr2").equals("defaultAttr2"));
  87                         Assert.assertTrue(delegate.getAttributeValue(null, "attr3").equals("defaultAttr3"));
  88 
  89                         Assert.assertTrue(delegate.getAttributeNamespace(0) == null);
  90                         Assert.assertTrue(delegate.getAttributeNamespace(1).equals("http://ns1.java.com"));
  91                         Assert.assertTrue(delegate.getAttributePrefix(1).equals("ns1"));
  92                         Assert.assertTrue(delegate.getAttributeName(1).toString()
  93                                 .equals("{" + delegate.getAttributeNamespace(1) + "}" + delegate.getAttributeLocalName(1)));
  94                         Assert.assertTrue(delegate.getAttributeLocalName(1).equals("attr1"));
  95 
  96                         // negative test. Should return null for out of
  97                         // attribute array index
  98                         Assert.assertTrue(delegate.getAttributeNamespace(delegate.getAttributeCount()) == null);
  99                         Assert.assertTrue(delegate.getAttributePrefix(delegate.getAttributeCount()) == null);
 100                         Assert.assertTrue(delegate.getAttributeName(delegate.getAttributeCount()) == null);
 101                         Assert.assertTrue(delegate.getAttributeLocalName(delegate.getAttributeCount()) == null);
 102                         Assert.assertTrue(delegate.getAttributeType(delegate.getAttributeCount()) == null);
 103                     }
 104                 } else {
 105                     try {
 106                         delegate.getAttributeCount();
 107                     } catch (IllegalStateException e) {
 108                         System.out.println("expected exception for incorrect event type");
 109                     }
 110                 }
 111 
 112             }
 113         } catch (FileNotFoundException e) {
 114             e.printStackTrace();
 115             Assert.fail("FileNotFoundException in testAttribute()");
 116         } catch (XMLStreamException e) {
 117             e.printStackTrace();
 118             System.out.println(delegate.getLocation());
 119             Assert.fail("XMLStreamException in testAttribute()");
 120         } catch (FactoryConfigurationError e) {
 121             e.printStackTrace();
 122             Assert.fail("FactoryConfigurationError in testAttribute()");
 123         } finally {
 124             try {
 125                 delegate.close();
 126             } catch (XMLStreamException e) {
 127                 e.printStackTrace();
 128                 Assert.fail("XMLStreamException in testAttribute()");
 129             }
 130         }
 131     }
 132 
 133     /**
 134      * Tested xml file looks as below: <?xml version="1.0" encoding="UTF-8"?>
 135      * <ns1:foo xmlns:ns="http://ns1.java.com" xmlns:ns1="http://ns1.java.com"
 136      * xmlns:ns2="http://ns2.java.com" > <!--description-->content text
 137      * <![CDATA[<greeting>Hello</greeting>]]> other content </ns1:foo>
 138      **/
 139     @Test
 140     public void testNamespace() {
 141         StreamReaderDelegate delegate = null;
 142         try {
 143             System.out.println("===in testNamespace()===");
 144             XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(
 145                     new FileInputStream(new File(getClass().getResource("testfile2.xml").getFile())));
 146             delegate = new StreamReaderDelegate();
 147             delegate.setParent(reader);
 148             while (delegate.hasNext()) {
 149                 delegate.next();
 150                 if (delegate.getEventType() == XMLStreamConstants.START_ELEMENT || delegate.getEventType() == XMLStreamConstants.ATTRIBUTE) {
 151 
 152                     if (delegate.getName().getLocalPart().equals("foo")) {
 153                         Assert.assertTrue(("{" + delegate.getNamespaceURI(delegate.getPrefix()) + "}" + delegate.getLocalName()).equals(delegate.getName()
 154                                 .toString()));
 155                         System.out.println(delegate.getLocation());
 156 
 157                         Assert.assertTrue(delegate.getNamespaceCount() == 3);
 158                         Assert.assertTrue(delegate.getNamespaceURI().equals("http://ns1.java.com"));
 159                         Assert.assertTrue(delegate.getNamespaceURI(2).equals("http://ns2.java.com"));
 160                         Assert.assertTrue(delegate.getNamespaceURI("ns").equals("http://ns1.java.com"));
 161 
 162                         Assert.assertTrue(delegate.getNamespacePrefix(1).equals("ns1"));
 163 
 164                         NamespaceContext nsCtx = delegate.getNamespaceContext();
 165                         nsCtx.getNamespaceURI("ns");
 166                         Iterator prefixes = nsCtx.getPrefixes("http://ns1.java.com");
 167                         boolean hasns = false;
 168                         boolean hasns1 = false;
 169                         while (prefixes.hasNext()) {
 170                             String prefix = (String) prefixes.next();
 171                             if (prefix.equals("ns")) {
 172                                 hasns = true;
 173                             } else if (prefix.equals("ns1")) {
 174                                 hasns1 = true;
 175                             }
 176                         }
 177                         Assert.assertTrue(hasns && hasns1);
 178                     }
 179                 }
 180             }
 181         } catch (FileNotFoundException e) {
 182             e.printStackTrace();
 183             Assert.fail("FileNotFoundException in testNamespace()");
 184         } catch (XMLStreamException e) {
 185             e.printStackTrace();
 186             System.out.println(delegate.getLocation());
 187             Assert.fail("XMLStreamException in testNamespace()");
 188         } catch (FactoryConfigurationError e) {
 189             e.printStackTrace();
 190             Assert.fail("FactoryConfigurationError in testNamespace()");
 191         } finally {
 192             try {
 193                 delegate.close();
 194             } catch (XMLStreamException e) {
 195                 e.printStackTrace();
 196                 Assert.fail("XMLStreamException in testNamespace()");
 197             }
 198         }
 199     }
 200 
 201     /**
 202      * <?xml version="1.0" encoding="utf-8" ?> <ns1:foo
 203      * xmlns:ns1="http://ns1.java.com" xmlns:ns2="http://ns2.java.com">
 204      * <!--description--> content text <![CDATA[<greeting>Hello</greeting>]]>
 205      * other content </ns1:foo>
 206      **/
 207     @Test
 208     public void testText() {
 209         String property = "javax.xml.stream.isCoalescing";
 210         System.out.println("===in testText()====");
 211         StreamReaderDelegate delegate = null;
 212         try {
 213             XMLInputFactory ifac = XMLInputFactory.newFactory();
 214             ifac.setProperty(property, Boolean.TRUE);
 215             XMLStreamReader reader = ifac.createXMLStreamReader(new FileInputStream(new File(getClass().getResource("testfile3.xml").getFile())), "iso8859-1");
 216             delegate = new StreamReaderDelegate();
 217             delegate.setParent(reader);
 218 
 219             Assert.assertTrue(delegate.getParent().equals(reader));
 220             Assert.assertTrue(delegate.getProperty(property).equals(Boolean.TRUE));
 221             Assert.assertTrue(delegate.getCharacterEncodingScheme().equalsIgnoreCase("utf-8"));
 222             Assert.assertTrue(delegate.getEncoding().equalsIgnoreCase("iso8859-1"));
 223             Assert.assertTrue(delegate.getVersion().equals("1.0"));
 224             while (delegate.hasNext()) {
 225                 delegate.next();
 226                 if (delegate.getEventType() == XMLStreamConstants.CHARACTERS) {
 227                     char[] target1 = new char[delegate.getTextLength()];
 228                     delegate.getTextCharacters(delegate.getTextStart(), target1, 0, target1.length);
 229                     char[] target2 = delegate.getTextCharacters();
 230 
 231                     Assert.assertTrue(delegate.getText().trim().equals(new String(target1).trim()));
 232                     Assert.assertTrue(delegate.getText().trim().equals(new String(target2).trim()));
 233                 }
 234             }
 235 
 236         } catch (FileNotFoundException e) {
 237             e.printStackTrace();
 238             Assert.fail("FileNotFoundException in testText()");
 239         } catch (XMLStreamException e) {
 240             e.printStackTrace();
 241             System.out.println(delegate.getLocation());
 242             Assert.fail("XMLStreamException in testText()");
 243         } catch (FactoryConfigurationError e) {
 244             e.printStackTrace();
 245             Assert.fail("FactoryConfigurationError in testText()");
 246         } finally {
 247             try {
 248                 delegate.close();
 249             } catch (XMLStreamException e) {
 250                 e.printStackTrace();
 251                 Assert.fail("XMLStreamException in testText()");
 252             }
 253         }
 254     }
 255 
 256     @Test
 257     public void testWhiteSpace() {
 258         System.out.println("===in testWhiteSpace()===");
 259         StreamReaderDelegate delegate = null;
 260         try {
 261             XMLInputFactory ifac = XMLInputFactory.newFactory();
 262             ifac.setProperty("javax.xml.stream.isCoalescing", Boolean.TRUE);
 263             XMLStreamReader reader = ifac.createXMLStreamReader(new FileInputStream(new File(getClass().getResource("testfile4.xml").getFile())));
 264 
 265             delegate = new StreamReaderDelegate();
 266             delegate.setParent(reader);
 267             while (delegate.hasNext()) {
 268                 int i = delegate.next();
 269                 switch (i) {
 270                     case XMLStreamConstants.CHARACTERS: {
 271                         Assert.assertTrue(delegate.isCharacters());
 272                         Assert.assertTrue(delegate.hasText());
 273                         Assert.assertTrue(delegate.isWhiteSpace());
 274                         break;
 275                     }
 276                     case XMLStreamConstants.START_ELEMENT: {
 277                         Assert.assertTrue(delegate.isStartElement());
 278                         Assert.assertTrue(delegate.isAttributeSpecified(0));
 279                         Assert.assertTrue(delegate.hasName());
 280                         delegate.require(XMLStreamConstants.START_ELEMENT, delegate.getNamespaceURI(), delegate.getLocalName());
 281                         break;
 282                     }
 283                     case XMLStreamConstants.END_ELEMENT: {
 284                         Assert.assertTrue(delegate.isEndElement());
 285                         Assert.assertTrue(delegate.hasName());
 286                         delegate.require(XMLStreamConstants.END_ELEMENT, delegate.getNamespaceURI(), delegate.getLocalName());
 287                         break;
 288                     }
 289                 }
 290             }
 291         } catch (FileNotFoundException e) {
 292             e.printStackTrace();
 293             Assert.fail("FileNotFoundException in testWhiteSpace()");
 294         } catch (XMLStreamException e) {
 295             e.printStackTrace();
 296             System.out.println(delegate.getLocation());
 297             Assert.fail("XMLStreamException in testWhiteSpace()");
 298         } catch (FactoryConfigurationError e) {
 299             e.printStackTrace();
 300             Assert.fail("FactoryConfigurationError in testWhiteSpace()");
 301         } finally {
 302             try {
 303                 delegate.close();
 304             } catch (XMLStreamException e) {
 305                 e.printStackTrace();
 306                 Assert.fail("XMLStreamException in testWhitespace()");
 307             }
 308         }
 309 
 310     }
 311 
 312     @Test
 313     public void testElementText() {
 314         System.out.println("===in testElementText()===");
 315         StreamReaderDelegate delegate = null;
 316         try {
 317             XMLInputFactory ifac = XMLInputFactory.newFactory();
 318             XMLStreamReader reader = ifac.createXMLStreamReader(new FileInputStream(new File(getClass().getResource("toys.xml").getFile())));
 319 
 320             delegate = new StreamReaderDelegate();
 321             delegate.setParent(reader);
 322             while (delegate.hasNext()) {
 323                 if (delegate.getEventType() == XMLStreamConstants.START_ELEMENT) {
 324                     if (delegate.getLocalName().equals("name") || delegate.getLocalName().equals("price")) {
 325                         System.out.println(delegate.getElementText());
 326                     }
 327                     delegate.nextTag();
 328                 } else {
 329                     delegate.next();
 330                 }
 331             }
 332         } catch (FileNotFoundException e) {
 333             e.printStackTrace();
 334             Assert.fail("FileNotFoundException in testElementText()");
 335         } catch (XMLStreamException e) {
 336             e.printStackTrace();
 337             System.out.println(delegate.getLocation());
 338             Assert.fail("XMLStreamException in testElementText()");
 339         } catch (FactoryConfigurationError e) {
 340             e.printStackTrace();
 341             Assert.fail("FactoryConfigurationError in testElementText()");
 342         } finally {
 343             try {
 344                 delegate.close();
 345             } catch (XMLStreamException e) {
 346                 e.printStackTrace();
 347                 Assert.fail("XMLStreamException in testElementText()");
 348             }
 349         }
 350     }
 351 
 352     @Test
 353     public void testPITargetAndData() {
 354         System.out.println("===in testPITargetAndData()===");
 355         StreamReaderDelegate delegate = null;
 356         try {
 357             XMLInputFactory xif = XMLInputFactory.newInstance();
 358             String PITarget = "soffice";
 359             String PIData = "WebservicesArchitecture";
 360             String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<?" + PITarget + " " + PIData + "?>" + "<foo></foo>";
 361             InputStream is = new java.io.ByteArrayInputStream(xml.getBytes());
 362             XMLStreamReader sr = xif.createXMLStreamReader(is);
 363             delegate = new StreamReaderDelegate(sr);
 364             while (delegate.hasNext()) {
 365                 int eventType = delegate.next();
 366                 if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION) {
 367                     String target = delegate.getPITarget();
 368                     String data = delegate.getPIData();
 369                     Assert.assertTrue(target.equals(PITarget));
 370                     Assert.assertTrue(data.equals(PIData));
 371                 }
 372             }
 373         } catch (Exception ex) {
 374             ex.printStackTrace();
 375             Assert.fail("Exception in testPITargetAndData()");
 376         } finally {
 377             try {
 378                 delegate.close();
 379             } catch (XMLStreamException e) {
 380                 e.printStackTrace();
 381                 Assert.fail("XMLStreamException in testPITargetAndData()");
 382             }
 383         }
 384     }
 385 }
 386