1   /*
2    * $Id: DomXmlTransformerEncodingTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transformers.xml;
12  
13  import org.mule.impl.endpoint.MuleEndpoint;
14  import org.mule.umo.endpoint.UMOEndpoint;
15  import org.mule.umo.transformer.UMOTransformer;
16  import org.mule.util.IOUtils;
17  
18  import org.dom4j.DocumentHelper;
19  import org.dom4j.io.DOMReader;
20  import org.dom4j.io.DOMWriter;
21  import org.w3c.dom.Document;
22  
23  public class DomXmlTransformerEncodingTestCase extends AbstractXmlTransformerTestCase
24  {
25      private Document srcData; // Parsed XML doc
26      private String resultData; // String as US-ASCII
27  
28      // @Override
29      protected void doSetUp() throws Exception
30      {
31          org.dom4j.Document dom4jDoc = DocumentHelper.parseText(IOUtils.toString(IOUtils.getResourceAsStream(
32              "cdcatalog-utf-8.xml", getClass()), "UTF-8"));
33          srcData = new DOMWriter().write(dom4jDoc);
34          resultData = IOUtils.toString(IOUtils.getResourceAsStream("cdcatalog-us-ascii.xml", getClass()),
35              "US-ASCII");
36      }
37  
38      public UMOTransformer getTransformer() throws Exception
39      {
40          UMOTransformer trans = new DomDocumentToXml();
41          trans.setReturnClass(String.class);
42  
43          UMOEndpoint endpoint = new MuleEndpoint();
44          endpoint.setEncoding("US-ASCII");
45          trans.setEndpoint(endpoint);
46          return trans;
47      }
48  
49      public UMOTransformer getRoundTripTransformer() throws Exception
50      {
51          return new XmlToDomDocument(); // encoding is not interesting
52      }
53  
54      public Object getTestData()
55      {
56          return srcData;
57      }
58  
59      public Object getResultData()
60      {
61          return resultData;
62      }
63  
64      // @Override
65      public boolean compareResults(Object expected, Object result)
66      {
67          // This is only used during roundtrip test, so it will always be Document
68          // instances
69          if (expected instanceof Document)
70          {
71              expected = new DOMReader().read((Document)expected).asXML();
72              result = new DOMReader().read((Document)result).asXML();
73          }
74  
75          return super.compareResults(expected, result);
76      }
77  
78  }