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