View Javadoc

1   /*
2    * $Id: IsXmlFilterTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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.api.MuleContext;
15  import org.mule.module.xml.filters.IsXmlFilter;
16  import org.mule.module.xml.util.XMLTestUtils;
17  import org.mule.tck.junit4.AbstractMuleTestCase;
18  import org.mule.util.IOUtils;
19  
20  import java.io.InputStream;
21  import java.util.List;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertFalse;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  import static org.mockito.Mockito.mock;
30  
31  public class IsXmlFilterTestCase extends AbstractMuleTestCase
32  {
33      private IsXmlFilter filter;
34      private MuleContext muleContext;
35  
36      @Before
37      public void setUp()
38      {
39          filter = new IsXmlFilter();
40          muleContext = mock(MuleContext.class);
41      }
42  
43      @Test
44      public void testFilterFalse() throws Exception
45      {
46          assertFalse(filter.accept(new DefaultMuleMessage("This is definitely not XML.", muleContext)));
47      }
48  
49      @Test
50      public void testFilterFalse2() throws Exception
51      {
52          assertFalse(filter.accept(new DefaultMuleMessage(
53              "<line>This is almost XML</line><line>This is almost XML</line>", muleContext)));
54      }
55  
56      @Test
57      public void testFilterTrue() throws Exception
58      {
59          assertTrue(filter.accept(new DefaultMuleMessage("<msg attrib=\"att1\">This is some nice XML!</msg>", muleContext)));
60      }
61  
62      @Test
63      public void testFilterBytes() throws Exception
64      {
65          byte[] bytes = "<msg attrib=\"att1\">This is some nice XML!</msg>".getBytes();
66          assertTrue(filter.accept(new DefaultMuleMessage(bytes, muleContext)));
67      }
68  
69      @Test
70      public void testFilterNull() throws Exception
71      {
72          assertFalse(filter.accept(new DefaultMuleMessage(null, muleContext)));
73      }
74  
75      @Test
76      public void testFilterLargeXml() throws Exception
77      {
78          InputStream is = IOUtils.getResourceAsStream("cdcatalog.xml", getClass());
79          assertNotNull("Test resource not found.", is);
80          final String xml = IOUtils.toString(is);
81          assertTrue(filter.accept(new DefaultMuleMessage(xml, muleContext)));
82      }
83  
84      @Test
85      public void testFilterLargeXmlCompliantHtml() throws Exception
86      {
87          InputStream is = IOUtils.getResourceAsStream("cdcatalog.html", getClass());
88          assertNotNull("Test resource not found.", is);
89          final String html = IOUtils.toString(is);
90          assertTrue(filter.accept(new DefaultMuleMessage(html, muleContext)));
91      }
92  
93      @Test
94      public void testFilterXmlMessageVariants() throws Exception
95      {
96          List<?> list = XMLTestUtils.getXmlMessageVariants("cdcatalog.xml");
97          for (Object message : list)
98          {
99              assertTrue(filter.accept(new DefaultMuleMessage(message, muleContext)));
100         }
101     }
102 }