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