1   /*
2    * $Id: OGNLFilterTestCase.java 11195 2008-03-06 04:13:01Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.routing.filters;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.ConfigurationException;
16  import org.mule.module.client.MuleClient;
17  import org.mule.module.ognl.filters.OGNLFilter;
18  import org.mule.routing.outbound.FilteringOutboundRouter;
19  import org.mule.tck.FunctionalTestCase;
20  
21  public class OGNLFilterTestCase extends FunctionalTestCase
22  {
23      public static final String DEFAULT_INPUT_QUEUE = "vm://in";
24      public static final String DEFUALT_OUTPUT_QUEUE = "vm://out";
25      public static final String FIRST_MESSAGE = "foo";
26      public static final String SECOND_MESSAGE = "foobar";
27      public static final String THIRD_MESSAGE = "INPUT MESSAGE";
28      public static final long TIMEOUT = 2000;
29      public static final String OGNL_EXSPRESSION = " equals(\"foo\") || content.endsWith(\"bar\") ";
30      public static final String SERVICE_NAME = "OGNLServiceWrapper1";
31  
32      protected String getConfigResources()
33      {
34          return "ognl-functional-test.xml";
35      }
36  
37      private OGNLFilter filter;
38  
39      // @Override
40      protected void doSetUp() throws Exception
41      {
42          filter = new OGNLFilter();
43      }
44  
45      // @Override
46      protected void doTearDown() throws Exception
47      {
48          filter = null;
49      }
50  
51      public void testNewFilter()
52      {
53          assertFalse(filter.accept(null));
54      }
55  
56      public void testNoExpressionEmptyMessage()
57      {
58          MuleMessage message = new DefaultMuleMessage(null);
59          assertFalse(filter.accept(message));
60      }
61  
62      public void testNoExpressionValidMessage()
63      {
64          MuleMessage message = new DefaultMuleMessage("foo");
65          assertFalse(filter.accept(message));
66      }
67  
68      public void testNamespaceHandler()
69      {
70          String expression =
71                  ((OGNLFilter) ((FilteringOutboundRouter) muleContext.getRegistry().
72                          lookupService(SERVICE_NAME).getOutboundRouter().getRouters().get(0)).
73                          getFilter()).getExpression();
74  
75          assertEquals(expression, OGNL_EXSPRESSION);
76      }
77  
78      public void testFunctionalTest() throws Exception
79      {
80          MuleClient client = new MuleClient();
81          try
82          {
83              client.dispatch(DEFAULT_INPUT_QUEUE, FIRST_MESSAGE, null);
84              MuleMessage message = client.request(DEFUALT_OUTPUT_QUEUE, TIMEOUT);
85              assertNotNull(message);
86              assertNotNull(message.getPayload());
87              assertNull(message.getExceptionPayload());
88              assertEquals(FIRST_MESSAGE, message.getPayload());
89  
90              Dummy payload = new Dummy();
91              payload.setContent(SECOND_MESSAGE);
92              client.dispatch(DEFAULT_INPUT_QUEUE, new DefaultMuleMessage(payload));
93              message = client.request(DEFUALT_OUTPUT_QUEUE, TIMEOUT);
94              assertNotNull(message);
95              assertNotNull(message.getPayload());
96              assertNull(message.getExceptionPayload());
97              assertEquals(SECOND_MESSAGE, ((Dummy) message.getPayload()).getContent());
98  
99              client.dispatch(DEFAULT_INPUT_QUEUE, THIRD_MESSAGE, null);
100             message = client.request(DEFUALT_OUTPUT_QUEUE, TIMEOUT);
101             assertNull(message);
102         }
103         finally
104         {
105             client.dispose();
106         }
107 
108     }
109 
110 
111     public void testInvalidObjectExpression()
112     {
113         try
114         {
115             filter.setExpression("foo:bar");
116             fail("should have failed with ConfigurationException");
117         }
118         catch (ConfigurationException configex)
119         {
120             // expected
121         }
122 
123         // make sure the filter is still unconfigured
124         assertNull(filter.getExpression());
125     }
126 
127     // a simple POJO for testing object expressions
128     private static class Dummy
129     {
130         private int id;
131         private String content;
132 
133         public Dummy()
134         {
135             super();
136         }
137 
138         /**
139          * @return Returns the content.
140          */
141         public String getContent()
142         {
143             return content;
144         }
145 
146         /**
147          * @param content The content to set.
148          */
149         public void setContent(String content)
150         {
151             this.content = content;
152         }
153 
154         /**
155          * @return Returns the id.
156          */
157         public int getId()
158         {
159             return id;
160         }
161 
162         /**
163          * @param id The id to set.
164          */
165         public void setId(int id)
166         {
167             this.id = id;
168         }
169     }
170 
171 }