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