View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.routing.filters.xml;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.module.xml.filters.IsXmlFilter;
12  import org.mule.module.xml.util.XMLTestUtils;
13  import org.mule.tck.junit4.AbstractMuleTestCase;
14  import org.mule.util.IOUtils;
15  
16  import java.io.InputStream;
17  import java.util.List;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.mockito.Mockito.mock;
26  
27  public class IsXmlFilterTestCase extends AbstractMuleTestCase
28  {
29      private IsXmlFilter filter;
30      private MuleContext muleContext;
31  
32      @Before
33      public void setUp()
34      {
35          filter = new IsXmlFilter();
36          muleContext = mock(MuleContext.class);
37      }
38  
39      @Test
40      public void testFilterFalse() throws Exception
41      {
42          assertFalse(filter.accept(new DefaultMuleMessage("This is definitely not XML.", muleContext)));
43      }
44  
45      @Test
46      public void testFilterFalse2() throws Exception
47      {
48          assertFalse(filter.accept(new DefaultMuleMessage(
49              "<line>This is almost XML</line><line>This is almost XML</line>", muleContext)));
50      }
51  
52      @Test
53      public void testFilterTrue() throws Exception
54      {
55          assertTrue(filter.accept(new DefaultMuleMessage("<msg attrib=\"att1\">This is some nice XML!</msg>", muleContext)));
56      }
57  
58      @Test
59      public void testFilterBytes() throws Exception
60      {
61          byte[] bytes = "<msg attrib=\"att1\">This is some nice XML!</msg>".getBytes();
62          assertTrue(filter.accept(new DefaultMuleMessage(bytes, muleContext)));
63      }
64  
65      @Test
66      public void testFilterNull() throws Exception
67      {
68          assertFalse(filter.accept(new DefaultMuleMessage(null, muleContext)));
69      }
70  
71      @Test
72      public void testFilterLargeXml() throws Exception
73      {
74          InputStream is = IOUtils.getResourceAsStream("cdcatalog.xml", getClass());
75          assertNotNull("Test resource not found.", is);
76          final String xml = IOUtils.toString(is);
77          assertTrue(filter.accept(new DefaultMuleMessage(xml, muleContext)));
78      }
79  
80      @Test
81      public void testFilterLargeXmlCompliantHtml() throws Exception
82      {
83          InputStream is = IOUtils.getResourceAsStream("cdcatalog.html", getClass());
84          assertNotNull("Test resource not found.", is);
85          final String html = IOUtils.toString(is);
86          assertTrue(filter.accept(new DefaultMuleMessage(html, muleContext)));
87      }
88  
89      @Test
90      public void testFilterXmlMessageVariants() throws Exception
91      {
92          List<?> list = XMLTestUtils.getXmlMessageVariants("cdcatalog.xml");
93          for (Object message : list)
94          {
95              assertTrue(filter.accept(new DefaultMuleMessage(message, muleContext)));
96          }
97      }
98  }