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