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