    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;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.io.StringWriter;
    13
    14  import javax.xml.transform.Result;
    15  import javax.xml.transform.Source;
    16  import javax.xml.transform.Transformer;
    17  import javax.xml.transform.TransformerFactory;
    18
    19  import org.dom4j.io.DocumentSource;
    20  import org.dom4j.io.OutputFormat;
    21  import org.dom4j.io.XMLResult;
    22  import org.dom4j.io.XMLWriter;
    23
    24  /***
    25   * Test harness for the XMLResult which acts as a JAXP Result
    26   *
    27   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    28   * @version $Revision: 1.3 $
    29   */
    30  public class XMLResultTest extends AbstractTestCase {
    31      protected static final boolean VERBOSE = false;
    32
    33      public static void main(String[] args) {
    34          TestRunner.run(XMLResultTest.class);
    35      }
    36
    37      // Test case(s)
    38      // -------------------------------------------------------------------------
    39      public void testWriter() throws Exception {
    40          // load a default transformer
    41          TransformerFactory factory = TransformerFactory.newInstance();
    42          Transformer transformer = factory.newTransformer();
    43
    44          // use dom4j document as the source
    45          Source source = new DocumentSource(document);
    46
    47          // use pretty print format and a buffer for the result
    48          OutputFormat format = OutputFormat.createCompactFormat();
    49          StringWriter buffer = new StringWriter();
    50          Result result = new XMLResult(buffer, format);
    51
    52          // now lets transform
    53          transformer.transform(source, result);
    54
    55          String text = buffer.toString();
    56
    57          if (VERBOSE) {
    58              log("Using JAXP and XMLResult the document is:- ");
    59              log(text);
    60          }
    61
    62          StringWriter out = new StringWriter();
    63
    64          XMLWriter writer = new XMLWriter(out, format);
    65          writer.write(document);
    66
    67          String text2 = out.toString();
    68
    69          if (VERBOSE) {
    70              log("Using XMLWriter the text is:-");
    71              log(text2);
    72          }
    73
    74          assertEquals("The text output should be identical", text2, text);
    75      }
    76  }
    77
    78  /*
    79   * Redistribution and use of this software and associated documentation
    80   * ("Software"), with or without modification, are permitted provided that the
    81   * following conditions are met:
    82   *
    83   * 1. Redistributions of source code must retain copyright statements and
    84   * notices. Redistributions must also contain a copy of this document.
    85   *
    86   * 2. Redistributions in binary form must reproduce the above copyright notice,
    87   * this list of conditions and the following disclaimer in the documentation
    88   * and/or other materials provided with the distribution.
    89   *
    90   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    91   * from this Software without prior written permission of MetaStuff, Ltd. For
    92   * written permission, please contact dom4j-info@metastuff.com.
    93   *
    94   * 4. Products derived from this Software may not be called "DOM4J" nor may
    95   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    96   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    97   *
    98   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    99   *
    100  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    101  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    102  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    103  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    104  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    105  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    106  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    107  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    108  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    109  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    110  * POSSIBILITY OF SUCH DAMAGE.
    111  *
    112  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    113  */