1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.lang.reflect.Method;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.velocity.runtime.RuntimeSingleton;
30  
31  /**
32   *  Simple introspector test case for primitive problem found in 1.3
33   *
34   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
35   * @version $Id: Introspector3TestCase.java 463298 2006-10-12 16:10:32Z henning $
36   */
37  public class Introspector3TestCase extends BaseTestCase
38  {
39      /**
40        * Creates a new instance.
41        */
42      public Introspector3TestCase(String name)
43      {
44          super(name);
45      }
46  
47      public static Test suite()
48      {
49          return new TestSuite(Introspector3TestCase.class);
50      }
51  
52      public void testSimple()
53          throws Exception
54      {
55          Method method;
56          String result;
57  
58          MethodProvider mp = new MethodProvider();
59  
60          /*
61           * string integer
62           */
63  
64          Object[] listIntInt = { new ArrayList(), new Integer(1), new Integer(2) };
65          Object[] listLongList = { new ArrayList(), new Long(1), new ArrayList() };
66          Object[] intInt = {  new Integer(1), new Integer(2) };
67          Object[] longInt = {  new Long(1), new Integer(2) };
68          Object[] longLong = {  new Long(1), new Long(2) };
69  
70          method = RuntimeSingleton.getIntrospector().getMethod(
71              MethodProvider.class, "lii", listIntInt);
72          result = (String) method.invoke(mp, listIntInt);
73  
74          assertTrue(result.equals("lii"));
75  
76          method = RuntimeSingleton.getIntrospector().getMethod(
77              MethodProvider.class, "ii", intInt);
78          result = (String) method.invoke(mp, intInt);
79  
80          assertTrue(result.equals("ii"));
81  
82          method = RuntimeSingleton.getIntrospector().getMethod(
83              MethodProvider.class, "ll", longInt);
84          result = (String) method.invoke(mp, longInt);
85  
86          assertTrue(result.equals("ll"));
87  
88          /*
89           * test overloading with primitives
90           */
91  
92          method = RuntimeSingleton.getIntrospector().getMethod(
93              MethodProvider.class, "ll", longLong);
94          result = (String) method.invoke(mp, longLong);
95  
96          assertTrue(result.equals("ll"));
97  
98          method = RuntimeSingleton.getIntrospector().getMethod(
99              MethodProvider.class, "lll", listLongList);
100         result = (String) method.invoke(mp, listLongList);
101 
102         assertTrue(result.equals("lll"));
103 
104         /*
105          *  test invocation with nulls
106          */
107 
108         Object [] oa = {null, new Integer(0)};
109         method = RuntimeSingleton.getIntrospector().getMethod(
110             MethodProvider.class, "lll", oa );
111         result = (String) method.invoke(mp, oa);
112 
113         assertTrue(result.equals("Listl"));
114 
115     }
116 
117     public static class MethodProvider
118     {
119         public String ii(int p, int d)
120         {
121             return "ii";
122         }
123 
124         public String lii(List s, int p, int d)
125         {
126             return "lii";
127         }
128 
129         public String lll(List s, long p, List d)
130         {
131             return "lll";
132         }
133 
134 
135         public String lll(List s, long p, int d)
136         {
137             return "lli";
138         }
139 
140         public String lll(List s, long p)
141         {
142             return "Listl";
143         }
144 
145         public String ll(long p, long d)
146         {
147             return "ll";
148         }
149 
150     }
151 }