View Javadoc

1   /*
2    * $Id: IsXmlFilterTestCase.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.routing.filters.xml;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.module.xml.filters.IsXmlFilter;
15  import org.mule.module.xml.util.XMLTestUtils;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.util.IOUtils;
18  
19  import java.io.InputStream;
20  import java.util.List;
21  
22  public class IsXmlFilterTestCase extends AbstractMuleTestCase
23  {
24      private IsXmlFilter filter;
25  
26      @Override
27      protected void doSetUp() throws Exception
28      {
29          filter = new IsXmlFilter();
30      }
31  
32      public void testFilterFalse() throws Exception
33      {
34          assertFalse(filter.accept(new DefaultMuleMessage("This is definitely not XML.", muleContext)));
35      }
36  
37      public void testFilterFalse2() throws Exception
38      {
39          assertFalse(filter.accept(new DefaultMuleMessage(
40              "<line>This is almost XML</line><line>This is almost XML</line>", muleContext)));
41      }
42  
43      public void testFilterTrue() throws Exception
44      {
45          assertTrue(filter.accept(new DefaultMuleMessage("<msg attrib=\"att1\">This is some nice XML!</msg>", muleContext)));
46      }
47  
48      public void testFilterBytes() throws Exception
49      {
50          byte[] bytes = "<msg attrib=\"att1\">This is some nice XML!</msg>".getBytes();
51          assertTrue(filter.accept(new DefaultMuleMessage(bytes, muleContext)));
52      }
53  
54      public void testFilterNull() throws Exception
55      {
56          assertFalse(filter.accept(new DefaultMuleMessage(null, muleContext)));
57      }
58  
59      public void testFilterLargeXml() throws Exception
60      {
61          InputStream is = IOUtils.getResourceAsStream("cdcatalog.xml", getClass());
62          assertNotNull("Test resource not found.", is);
63          final String xml = IOUtils.toString(is);
64          assertTrue(filter.accept(new DefaultMuleMessage(xml, muleContext)));
65      }
66  
67      public void testFilterLargeXmlCompliantHtml() throws Exception
68      {
69          InputStream is = IOUtils.getResourceAsStream("cdcatalog.html", getClass());
70          assertNotNull("Test resource not found.", is);
71          final String html = IOUtils.toString(is);
72          assertTrue(filter.accept(new DefaultMuleMessage(html, muleContext)));
73      }
74  
75      public void testFilterXmlMessageVariants() throws Exception
76      {
77          List<?> list = XMLTestUtils.getXmlMessageVariants("cdcatalog.xml");
78          for (Object message : list)
79          {
80              assertTrue(filter.accept(new DefaultMuleMessage(message, muleContext)));
81          }
82      }
83  }