View Javadoc

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