View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformers.xml;
8   
9   import org.mule.module.xml.util.XMLUtils;
10  import org.mule.transformer.AbstractTransformerTestCase;
11  
12  import javax.xml.transform.TransformerFactoryConfigurationError;
13  
14  import org.custommonkey.xmlunit.XMLUnit;
15  import org.w3c.dom.Document;
16  
17  /**
18   * Use this superclass if you intend to compare Xml contents.
19   */
20  public abstract class AbstractXmlTransformerTestCase extends AbstractTransformerTestCase
21  {
22  
23      protected AbstractXmlTransformerTestCase()
24      {
25          super();
26          XMLUnit.setIgnoreWhitespace(true);
27          XMLUnit.setXSLTVersion("2.0");
28          try
29          {
30              XMLUnit.getTransformerFactory();
31          }
32          catch (TransformerFactoryConfigurationError e)
33          {
34              XMLUnit.setTransformerFactory(XMLUtils.TRANSFORMER_FACTORY_JDK5);
35          }
36      }
37  
38      @Override
39      public boolean compareResults(Object expected, Object result)
40      {
41          if (expected instanceof Document && result instanceof Document)
42          {
43              return XMLUnit.compareXML((Document)expected, (Document)result).similar();
44          }
45          else if (expected instanceof String && result instanceof String)
46          {
47              try
48              {
49                  String expectedString = this.normalizeString((String)expected);
50                  String resultString = this.normalizeString((String)result);
51                  return XMLUnit.compareXML(expectedString, resultString).similar();
52              }
53              catch (Exception ex)
54              {
55                  return false;
56              }
57          }
58  
59          // all other comparisons are passed up
60          return super.compareResults(expected, result);
61      }
62  
63  }