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.ExpressionEvaluator;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  
14  import java.util.Map;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  
21  /**
22   * Tests a custom expression evaluator using direct registration with the manager.
23   */
24  public class CustomExpressionEvaluatorTestCase extends AbstractMuleContextTestCase
25  {
26  
27      @Test
28      public void testCustomExpressionEvaluator()
29      {
30          muleContext.getExpressionManager().registerEvaluator(new FooExpressionEvaluator());
31  
32          Object result = muleContext.getExpressionManager().evaluate("#[foo:abc]",
33              new DefaultMuleMessage("test", (Map) null, muleContext));
34          assertNotNull(result);
35          assertEquals("Wrong evaluation result", "testabc", result);
36      }
37  
38      public static class FooExpressionEvaluator implements ExpressionEvaluator
39      {
40  
41          public Object evaluate(String expression, MuleMessage message)
42          {
43              return message.getPayload() + expression;
44          }
45  
46          public void setName(String name)
47          {
48              throw new UnsupportedOperationException("setName");
49          }
50  
51          public String getName()
52          {
53              return "foo";
54          }
55      }
56  
57  }