View Javadoc

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