View Javadoc

1   /*
2    * $Id: StringExpressionEvaluatorTestCase.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  package org.mule.expression;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MuleMessage;
14  import org.mule.tck.junit4.AbstractMuleContextTestCase;
15  import org.mule.tck.testmodels.fruit.Apple;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  public class StringExpressionEvaluatorTestCase extends AbstractMuleContextTestCase
26  {
27      private Map props;
28  
29          @Override
30          public void doSetUp()
31          {
32              props = new HashMap(3);
33              props.put("foo", "moo");
34              props.put("bar", "mar");
35              props.put("baz", "maz");
36          }
37  
38      @Test
39      public void testString() throws Exception
40      {
41          MuleMessage message = new DefaultMuleMessage(new Apple(), props, muleContext);
42          StringExpressionEvaluator extractor = new StringExpressionEvaluator();
43          extractor.setMuleContext(muleContext);
44          Object o = extractor.evaluate("payload is #[function:shortPayloadClass] and has foo=#[header:foo] header", message);
45          assertNotNull(o);
46          assertEquals("payload is Apple and has foo=moo header", o.toString());
47  
48          o = extractor.evaluate("literal string", message);
49          assertNotNull(o);
50          assertEquals("literal string", o.toString());
51      }
52  
53  
54      @Test
55      public void testStringUsingManager() throws Exception
56      {
57          MuleMessage message = new DefaultMuleMessage(new Apple(), props, muleContext);
58          Object o = muleContext.getExpressionManager().evaluate("#[string:payload is #[function:shortPayloadClass] and has foo=#[header:foo] header]", message);
59          assertNotNull(o);
60          assertEquals("payload is Apple and has foo=moo header", o.toString());
61  
62          o = muleContext.getExpressionManager().evaluate("#[string:literal string]", message);
63          assertNotNull(o);
64          assertEquals("literal string", o.toString());
65      }
66  }