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