View Javadoc

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