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