View Javadoc

1   /*
2    * $Id: VariableEnricherEvaluatorTestCase.java 20418 2010-12-01 13:25:51Z dfeist $
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.expression;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.expression.ExpressionManager;
16  import org.mule.api.transport.PropertyScope;
17  import org.mule.tck.AbstractMuleTestCase;
18  
19  public class VariableEnricherEvaluatorTestCase extends AbstractMuleTestCase
20  {
21      public void testEnrichEvaluate() throws Exception
22      {
23          VariableExpressionEvaluator eval = new VariableExpressionEvaluator();
24          VariableExpressionEnricher enricher = new VariableExpressionEnricher();
25  
26          MuleMessage message = new DefaultMuleMessage("test", muleContext);
27  
28          enricher.enrich("foo", message, "fooValue");
29  
30          // Value required + found
31          Object result = eval.evaluate("foo", message);
32          assertNotNull(result);
33          assertEquals("fooValue", result);
34  
35          // Value required + not found (throws exception)
36          try
37          {
38              eval.evaluate("fool", message);
39              fail("required value");
40          }
41          catch (Exception e)
42          {
43              // Expected
44          }
45  
46          // Variable is stored as an invocation property
47          result = message.getProperty("foo", PropertyScope.INVOCATION);
48          assertNotNull(result);
49          assertEquals("fooValue", result);
50      }
51  
52      public void testEnrichEvaluateWithManager() throws Exception
53      {
54          ExpressionManager expressionManager = muleContext.getExpressionManager();
55          MuleMessage message = new DefaultMuleMessage("test", muleContext);
56  
57          expressionManager.enrich("#[variable:foo]", message, "fooValue");
58  
59          // Value required + found
60          Object result = expressionManager.evaluate("#[variable:foo]", message);
61          assertNotNull(result);
62          assertEquals("fooValue", result);
63  
64          // Value required + not found (throws exception)
65          try
66          {
67              expressionManager.evaluate("#[variable:fool]", message);
68              fail("required value");
69          }
70          catch (Exception e)
71          {
72              // Expected
73          }
74  
75          // Variable is stored as an invocation property
76          result = message.getProperty("foo", PropertyScope.INVOCATION);
77          assertNotNull(result);
78          assertEquals("fooValue", result);
79  
80      }
81  
82  }