View Javadoc

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