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.api.annotations.transformer;
8   
9   import org.mule.config.transformer.AnnotatedTransformerProxy;
10  import org.mule.tck.junit4.AbstractMuleContextTestCase;
11  
12  import java.lang.reflect.Method;
13  
14  import org.junit.Test;
15  
16  import static org.junit.Assert.fail;
17  
18  public class BadAnnotatedTransformerTestCase extends AbstractMuleContextTestCase
19  {
20      @Test
21      public void testVoidTransformer() throws Exception
22      {
23          Method m = getClass().getDeclaredMethod("voidTransformer", StringBuffer.class);
24          try
25          {
26              new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
27              fail("Cannot register invalid transformer method");
28          }
29          catch (IllegalArgumentException e)
30          {
31              //Expected
32          }
33      }
34  
35      @Test
36      public void testNoParamsTransformer() throws Exception
37      {
38          Method m = getClass().getDeclaredMethod("noParamsTransformer", new Class[]{});
39          try
40          {
41              new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
42              fail("Cannot register invalid transformer method");
43          }
44          catch (IllegalArgumentException e)
45          {
46              //Expected
47          }
48      }
49  
50      @Test
51      public void testPrivateTransformer() throws Exception
52      {
53          Method m = getClass().getDeclaredMethod("privateTransformer", StringBuffer.class);
54          try
55          {
56              new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
57              fail("Cannot register invalid transformer method");
58          }
59          catch (IllegalArgumentException e)
60          {
61              //Expected
62          }
63      }
64  
65      @Test
66      public void testProtectedTransformer() throws Exception
67      {
68          Method m = getClass().getDeclaredMethod("protectedTransformer", StringBuffer.class);
69          try
70          {
71              new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
72              fail("Cannot register invalid transformer method");
73          }
74          catch (IllegalArgumentException e)
75          {
76              //Expected
77          }
78      }
79  
80      @Test
81      public void testPackageTransformer() throws Exception
82      {
83          Method m = getClass().getDeclaredMethod("packageTransformer", StringBuffer.class);
84          try
85          {
86              new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
87              fail("Cannot register invalid transformer method");
88          }
89          catch (IllegalArgumentException e)
90          {
91              //Expected
92          }
93      }
94  
95      @Test
96      public void testPublicTransformerObjectReturn() throws Exception
97      {
98          Method m = getClass().getDeclaredMethod("publicTransformerObjectReturn", StringBuffer.class);
99          try
100         {
101             new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
102             fail("Cannot register invalid transformer method");
103         }
104         catch (IllegalArgumentException e)
105         {
106             //Expected
107         }
108     }
109 
110     @Test
111     public void testPublicTransformerObjectParam() throws Exception
112     {
113         Method m = getClass().getDeclaredMethod("publicTransformerObjectParam", Object.class);
114         try
115         {
116             new AnnotatedTransformerProxy(5, getClass(), m, m.getParameterTypes(), null, null);
117             fail("Cannot register invalid transformer method");
118         }
119         catch (IllegalArgumentException e)
120         {
121             //Expected
122         }
123     }
124 
125     @Test
126     public void testGoodTransformerWithObjectSource() throws Exception
127     {
128         Method m = getClass().getDeclaredMethod("goodTransformer", StringBuffer.class);
129         Class c[] = new Class[]{String.class, Object.class};
130         try
131         {
132             new AnnotatedTransformerProxy(5, getClass(), m, c, null, null);
133             fail("Cannot register invalid transformer method");
134         }
135         catch (IllegalArgumentException e)
136         {
137             //Expected
138         }
139     }
140 
141     @Test
142     public void testBadTransformerRegistration() throws Exception
143     {
144         try
145         {
146             muleContext.getRegistry().registerObject("badTransformer", new BadAnnotatedTransformer());
147             fail("Cannot register invalid transformer method");            
148         }
149         catch (IllegalArgumentException e)
150         {
151             //expected
152         }
153 
154     }
155 
156     public void voidTransformer(StringBuffer in)
157     {
158         
159     }
160 
161     public String noParamsTransformer()
162     {
163         return "";
164     }
165 
166     private String privateTransformer(StringBuffer foo)
167     {
168         return foo.toString();
169     }
170 
171     protected String protectedTransformer(StringBuffer foo)
172     {
173         return foo.toString();
174     }
175 
176     String packageTransformer(StringBuffer foo)
177     {
178         return foo.toString();
179     }
180 
181     public Object publicTransformerObjectReturn(StringBuffer foo)
182     {
183         return foo;
184     }
185 
186     public String publicTransformerObjectParam(Object foo)
187     {
188         return foo.toString();
189     }
190 
191     public String goodTransformer(StringBuffer foo)
192     {
193         return foo.toString();
194     }
195 
196 }