View Javadoc

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