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 package stream.CoalesceTest; 24 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.InputStream; 28 29 import javax.xml.stream.XMLInputFactory; 30 import javax.xml.stream.XMLStreamConstants; 31 import javax.xml.stream.XMLStreamException; 32 import javax.xml.stream.XMLStreamReader; 33 34 import org.testng.Assert; 35 import org.testng.annotations.Test; 36 37 /* 38 * @summary Test Coalesce property works. 39 */ 40 public class CoalesceTest { 41 42 String countryElementContent = "START India CS}}}}}} India END"; 43 String descriptionElementContent = "a&b"; 44 String fooElementContent = "&< cdatastart<><>>><>><<<<cdataend entitystart insert entityend"; 45 46 @Test 47 public void testCoalesceProperty() { 48 try { 49 XMLInputFactory xifactory = XMLInputFactory.newInstance(); 50 xifactory.setProperty(XMLInputFactory.IS_COALESCING, new Boolean(true)); 51 InputStream xml = this.getClass().getResourceAsStream("coalesce.xml"); 52 XMLStreamReader streamReader = xifactory.createXMLStreamReader(xml); 53 while (streamReader.hasNext()) { 54 int eventType = streamReader.next(); 55 if (eventType == XMLStreamConstants.START_ELEMENT && streamReader.getLocalName().equals("country")) { 56 eventType = streamReader.next(); 57 if (eventType == XMLStreamConstants.CHARACTERS) { 58 String text = streamReader.getText(); 59 if (!text.equals(countryElementContent)) { 60 System.out.println("String dont match"); 61 System.out.println("text = " + text); 62 System.out.println("countryElementContent = " + countryElementContent); 63 } 64 // assertTrue(text.equals(countryElementContent)); 65 } 66 } 67 if (eventType == XMLStreamConstants.START_ELEMENT && streamReader.getLocalName().equals("description")) { 68 eventType = streamReader.next(); 69 if (eventType == XMLStreamConstants.CHARACTERS) { 70 String text = streamReader.getText(); 71 if (!text.equals(descriptionElementContent)) { 72 System.out.println("String dont match"); 73 System.out.println("text = " + text); 74 System.out.println("descriptionElementContent = " + descriptionElementContent); 75 } 76 Assert.assertTrue(text.equals(descriptionElementContent)); 77 } 78 } 79 if (eventType == XMLStreamConstants.START_ELEMENT && streamReader.getLocalName().equals("foo")) { 80 eventType = streamReader.next(); 81 if (eventType == XMLStreamConstants.CHARACTERS) { 82 String text = streamReader.getText(); 83 if (!text.equals(fooElementContent)) { 84 System.out.println("String dont match"); 85 System.out.println("text = " + text); 86 System.out.println("fooElementContent = " + fooElementContent); 87 } 88 89 Assert.assertTrue(text.equals(fooElementContent)); 90 } 91 } 92 93 } 94 } catch (XMLStreamException ex) { 95 96 if (ex.getNestedException() != null) { 97 ex.getNestedException().printStackTrace(); 98 } 99 // ex.printStackTrace() ; 100 } catch (Exception ex) { 101 ex.printStackTrace(); 102 } 103 104 } 105 106 }