View Javadoc

1   /*
2    * $Id: AbstractXmlTransformerTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.module.xml.util.XMLUtils;
14  import org.mule.transformer.AbstractTransformerTestCase;
15  
16  import javax.xml.transform.TransformerFactoryConfigurationError;
17  
18  import org.custommonkey.xmlunit.XMLUnit;
19  import org.w3c.dom.Document;
20  
21  /**
22   * Use this superclass if you intend to compare Xml contents.
23   */
24  public abstract class AbstractXmlTransformerTestCase extends AbstractTransformerTestCase
25  {
26  
27      protected AbstractXmlTransformerTestCase()
28      {
29          super();
30          XMLUnit.setIgnoreWhitespace(true);
31          XMLUnit.setXSLTVersion("2.0");
32          try
33          {
34              XMLUnit.getTransformerFactory();
35          }
36          catch (TransformerFactoryConfigurationError e)
37          {
38              XMLUnit.setTransformerFactory(XMLUtils.TRANSFORMER_FACTORY_JDK5);
39          }
40      }
41  
42      @Override
43      public boolean compareResults(Object expected, Object result)
44      {
45          if (expected instanceof Document && result instanceof Document)
46          {
47              return XMLUnit.compareXML((Document)expected, (Document)result).similar();
48          }
49          else if (expected instanceof String && result instanceof String)
50          {
51              try
52              {
53                  String expectedString = this.normalizeString((String)expected);
54                  String resultString = this.normalizeString((String)result);
55                  return XMLUnit.compareXML(expectedString, resultString).similar();
56              }
57              catch (Exception ex)
58              {
59                  return false;
60              }
61          }
62  
63          // all other comparisons are passed up
64          return super.compareResults(expected, result);
65      }
66  
67  }