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.module.xml;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertNotNull;
17  
18  /**
19   * @see EE-1734
20   */
21  public class TemplateParserTestCase extends AbstractMuleContextTestCase
22  {
23      private static final String TEST_MULE_STRING_EXPRESSION_XML = 
24          "<xml><t2><tag1 attr1='blahattr1'>BLAH1</tag1><tag1 attr1='blahattr2'>BLAH2</tag1></t2></xml>";
25  
26      private MuleMessage message;
27      
28      @Override
29      protected void doSetUp() throws Exception
30      {
31          super.doSetUp();
32          
33          message = new DefaultMuleMessage(TEST_MULE_STRING_EXPRESSION_XML, muleContext);
34      }
35  
36      @Test
37      public void testXPathExpression() throws Exception
38      {
39          String result = (String) muleContext.getExpressionManager().evaluate(
40              "#[xpath:/xml/t2/tag1[@attr1='blahattr1']]", message);
41          assertNotNull(result);
42          assertEquals("BLAH1", result);
43      }
44  
45      @Test
46      public void testStringExpression() throws Exception
47      {
48          String result = (String) muleContext.getExpressionManager().evaluate(
49              "#[string:#[xpath:/xml/t2/tag1[@attr1='blahattr1']]]", message);
50          assertNotNull(result);
51          assertEquals("BLAH1", result);
52      }
53  
54      @Test
55      public void testXPathExpressionWithAsterisk() throws Exception
56      {
57          String result = (String) muleContext.getExpressionManager().evaluate(
58              "#[xpath:/xml/*/tag1[@attr1='blahattr1']]", message);
59          assertNotNull(result);
60          assertEquals("BLAH1", result);
61      }
62  
63      @Test
64      public void testStringExpressionWithAsterisk() throws Exception
65      {
66          String result = (String) muleContext.getExpressionManager().evaluate(
67              "#[string:#[xpath:/xml/*/tag1[@attr1='blahattr1']]]", message);
68          assertNotNull(result);
69          assertEquals("BLAH1", result);
70      }
71  
72      @Test
73      public void testStringExpressionDoParse() throws Exception
74      {
75          String result = muleContext.getExpressionManager().parse(
76              "#[xpath:/xml/*/tag1[@attr1='blahattr1']]", message);
77          assertNotNull(result);
78          assertEquals("BLAH1", result);
79      }
80  
81      @Test
82      public void testStringExpressionDoParseEmbedded() throws Exception
83      {
84          String result = muleContext.getExpressionManager().parse(
85              "#[xpath:/xml/*/tag1[@attr1='blahattr1']] foo #[xpath:/xml/*/tag1[@attr1='blahattr2']]", message);
86          assertNotNull(result);
87          assertEquals("BLAH1 foo BLAH2", result);
88      }
89  }