View Javadoc

1   /*
2    * $Id: W3CDomXPathExpressionWithNamespaceTestCase.java 22414 2011-07-14 13:24:46Z 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.module.xml.functional;
12  
13  import java.io.ByteArrayInputStream;
14  import java.text.MessageFormat;
15  import java.util.Properties;
16  
17  import javax.xml.parsers.DocumentBuilder;
18  import javax.xml.parsers.DocumentBuilderFactory;
19  
20  import org.w3c.dom.Document;
21  
22  public class W3CDomXPathExpressionWithNamespaceTestCase extends AbstractXmlPropertyExtractorTestCase
23  {
24  
25      public static final String MESSAGE = "<foo:endpoint xmlns:foo=\"http://foo.com\">{0}</foo:endpoint>";
26  
27      public W3CDomXPathExpressionWithNamespaceTestCase(ConfigVariant variant, String configResources)
28      {
29          super(variant, configResources, true);
30      }
31  
32      @Override
33      protected Properties getStartUpProperties()
34      {
35          Properties p = new Properties();
36          p.setProperty("selector.expression", "/foo:endpoint");
37          p.setProperty("selector.evaluator", "xpath");
38  
39          return p;
40      }
41  
42      @Override
43      protected Object getMatchMessage() throws Exception
44      {
45          return documentFor("matchingEndpoint1");
46      }
47  
48      @Override
49      protected Object getErrorMessage() throws Exception
50      {
51          return documentFor("missingEndpoint");
52      }
53  
54      protected Document documentFor(String name) throws Exception
55      {
56          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
57          factory.setNamespaceAware(true);
58          DocumentBuilder builder = factory.newDocumentBuilder();
59  
60          String s = MessageFormat.format(MESSAGE, name);
61          Document doc = builder.parse(new ByteArrayInputStream(s.getBytes()));
62          return doc;
63      }
64  
65  }