View Javadoc

1   /*
2    * $Id: XPathAnnotatedEntryPointResolverTestCase.java 18855 2010-08-06 15:32:53Z 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  package org.mule.module.xml.config;
11  
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.config.MuleProperties;
14  import org.mule.api.expression.RequiredValueException;
15  import org.mule.api.model.InvocationResult;
16  import org.mule.api.transport.PropertyScope;
17  import org.mule.impl.model.resolvers.AnnotatedEntryPointResolver;
18  import org.mule.tck.AbstractMuleTestCase;
19  
20  import java.util.Map;
21  
22  import org.w3c.dom.Document;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.Node;
25  import org.w3c.dom.NodeList;
26  
27  public class XPathAnnotatedEntryPointResolverTestCase extends AbstractMuleTestCase
28  {
29      public static final String TEST_PAYLOAD = "<foo><bar>4</bar><bar>8</bar></foo>";
30  
31      public void testAnnotatedMethod() throws Exception
32      {
33          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
34          AnnotatedComponent component = new AnnotatedComponent();
35          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
36          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
37          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff", PropertyScope.INVOCATION);
38          InvocationResult result = resolver.invoke(component, context);
39          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
40          assertTrue(result.getResult() instanceof Map);
41          Map<?, ?> map = (Map<?, ?>)result.getResult();
42          assertEquals(3, map.size());
43          assertTrue(map.get("foo") instanceof Element);
44          assertTrue((Boolean)map.get("isBarValue"));
45          assertEquals("4", map.get("bar"));
46      }
47  
48      public void testAnnotatedMethod2() throws Exception
49      {
50          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
51          AnnotatedComponent component = new AnnotatedComponent();
52          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
53          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
54          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff2", PropertyScope.INVOCATION);
55          InvocationResult result = resolver.invoke(component, context);
56          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
57          assertTrue(result.getResult() instanceof Map);
58          Map<?, ?> map = (Map<?, ?>)result.getResult();
59          assertEquals(3, map.size());
60          assertTrue(map.get("foo") instanceof Document);
61          assertTrue((Boolean)map.get("isBarValue"));
62          assertEquals(new Double(8), map.get("bar"));
63      }
64  
65      public void testAnnotatedMethod3() throws Exception
66      {
67          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
68          AnnotatedComponent component = new AnnotatedComponent();
69          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
70          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
71          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff3", PropertyScope.INVOCATION);
72          InvocationResult result = resolver.invoke(component, context);
73          assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
74          assertTrue(result.getResult() instanceof Map);
75          Map<?, ?> map = (Map<?, ?>)result.getResult();
76          assertEquals(2, map.size());
77          assertTrue(map.get("foo") instanceof Node);
78          assertTrue(map.get("bar") instanceof NodeList);
79          assertEquals(2, ((NodeList)map.get("bar")).getLength());
80      }
81  
82      public void testAnnotatedMethodRequiredMissing() throws Exception
83      {
84          AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
85          AnnotatedComponent component = new AnnotatedComponent();
86          MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
87          //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
88          context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff4", PropertyScope.INVOCATION);
89          try
90          {
91              resolver.invoke(component, context);
92              fail("The xpath expression returned null, nbut a value was required");
93          }
94          catch (RequiredValueException e)
95          {
96              //expected
97          }
98      }
99  
100     public void testAnnotatedMethodMissingNotRequired() throws Exception
101     {
102         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
103         AnnotatedComponent component = new AnnotatedComponent();
104         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
105         //Since AnnotatedComponent2 has two annotated methods we need to set the method to call
106         context.getMessage().setProperty(MuleProperties.MULE_METHOD_PROPERTY, "doStuff5", PropertyScope.INVOCATION);
107         InvocationResult result = resolver.invoke(component, context);
108         assertEquals(result.getState(), InvocationResult.State.SUCCESSFUL);
109         assertTrue(result.getResult() instanceof Map);
110         Map<?, ?> map = (Map<?, ?>)result.getResult();
111         assertEquals(1, map.size());
112         assertNull(map.get("foo"));
113     }
114 
115     public void testIllegalAnnotatedMethod() throws Exception
116     {
117         AnnotatedEntryPointResolver resolver = new AnnotatedEntryPointResolver();
118         IllegalAnnotatedComponent component = new IllegalAnnotatedComponent();
119         MuleEventContext context = getTestEventContext(TEST_PAYLOAD);
120         try
121         {
122             resolver.invoke(component, context);
123             fail("Annotated parameter has an illegal return type argument");
124         }
125         catch (IllegalArgumentException e)
126         {
127             //expected
128         }
129     }
130 }