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