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.config.spring.parsers.processors;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  import org.mule.config.spring.parsers.PreProcessor;
11  import org.mule.util.ArrayUtils;
12  
13  import java.util.StringTokenizer;
14  
15  import javax.xml.parsers.DocumentBuilder;
16  import javax.xml.parsers.DocumentBuilderFactory;
17  import javax.xml.parsers.ParserConfigurationException;
18  
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  public abstract class AbstractPreProcessorTestCase extends AbstractMuleTestCase
26  {
27  
28      protected void assertBad(String[][] constraint, String attributes, String text) throws ParserConfigurationException
29      {
30          try
31          {
32              assertOk(constraint, attributes);
33              fail("Expected failure with " + attributes + " and " + ArrayUtils.toString(constraint));
34          }
35          catch (Exception e)
36          {
37              assertTrue(e.getMessage(), e.getMessage().indexOf(text) > -1);
38          }
39      }
40  
41      protected void assertOk(String[][] constraint, String attributes) throws ParserConfigurationException
42      {
43          createCheck(constraint).preProcess(null, createElement(attributes));
44      }
45  
46      protected abstract PreProcessor createCheck(String[][] constraint);
47  
48      protected Element createElement(String attributes) throws ParserConfigurationException
49      {
50          DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
51          Document document = builder.newDocument();
52          Element element = document.createElement("element");
53          StringTokenizer tokens = new StringTokenizer(attributes);
54          while (tokens.hasMoreTokens())
55          {
56              element.setAttribute(tokens.nextToken(), "value");
57          }
58          return element;
59      }
60  
61  }