    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.io;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.io.File;
    13  import java.io.FileReader;
    14  import java.io.StringWriter;
    15
    16  import javax.xml.stream.XMLInputFactory;
    17
    18  import org.dom4j.AbstractTestCase;
    19  import org.dom4j.Document;
    20
    21  /***
    22   * Tests STAX->DOM4J functionality.
    23   *
    24   * @author <a href="mailto:maartenc@sourceforge.net">Maarten Coene </a>
    25   * @author Christian Niles
    26   */
    27  public class StaxTest extends AbstractTestCase {
    28      public static void main(String[] args) {
    29          TestRunner.run(StaxTest.class);
    30      }
    31
    32      // Test case(s)
    33      // -------------------------------------------------------------------------
    34
    35      /***
    36       * Tests that the encoding specified in the XML declaration is exposed in
    37       * the Document read via StAX, and also that it gets output when writing.
    38       */
    39      public void testEncoding() {
    40          /*
    41           * only execute if a reference implementation is available
    42           */
    43          try {
    44              XMLInputFactory.newInstance();
    45          } catch (javax.xml.stream.FactoryConfigurationError e) {
    46              // no implementation found, stop the test.
    47              return;
    48          }
    49
    50          try {
    51              File file = getFile("/xml/russArticle.xml");
    52              STAXEventReader xmlReader = new STAXEventReader();
    53              Document doc = xmlReader.readDocument(new FileReader(file));
    54
    55              assertEquals("russArticle.xml encoding wasn't correct", "koi8-r",
    56                      doc.getXMLEncoding());
    57
    58              StringWriter writer = new StringWriter();
    59              STAXEventWriter xmlWriter = new STAXEventWriter(writer);
    60              xmlWriter.writeDocument(doc);
    61
    62              String output = writer.toString();
    63              String xmlDecl = output.substring(0, output.indexOf("?>") + 2);
    64              String expected = "<?xml version=\'1.0\' encoding=\'koi8-r\'?>";
    65              assertEquals("Unexpected xml declaration", expected, xmlDecl);
    66              System.out.println(output);
    67          } catch (Exception e) {
    68              e.printStackTrace();
    69              fail(e.getMessage());
    70          }
    71      }
    72  }
    73
    74  /*
    75   * Redistribution and use of this software and associated documentation
    76   * ("Software"), with or without modification, are permitted provided that the
    77   * following conditions are met:
    78   *
    79   * 1. Redistributions of source code must retain copyright statements and
    80   * notices. Redistributions must also contain a copy of this document.
    81   *
    82   * 2. Redistributions in binary form must reproduce the above copyright notice,
    83   * this list of conditions and the following disclaimer in the documentation
    84   * and/or other materials provided with the distribution.
    85   *
    86   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    87   * from this Software without prior written permission of MetaStuff, Ltd. For
    88   * written permission, please contact dom4j-info@metastuff.com.
    89   *
    90   * 4. Products derived from this Software may not be called "DOM4J" nor may
    91   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    92   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    93   *
    94   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    95   *
    96   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    97   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    98   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    99   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    100  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    101  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    102  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    103  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    104  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    105  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    106  * POSSIBILITY OF SUCH DAMAGE.
    107  *
    108  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    109  */