/projects/tomcat-7.0.2/test/org/apache/el/TestMethodExpressionImpl.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 329 lines · 252 code · 44 blank · 33 comment · 0 complexity · 607a9f7ddccdeb688d7bd6970a8e9c85 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.el;
  18. import javax.el.ELContext;
  19. import javax.el.ExpressionFactory;
  20. import javax.el.MethodExpression;
  21. import javax.el.ValueExpression;
  22. import org.apache.jasper.el.ELContextImpl;
  23. import junit.framework.TestCase;
  24. public class TestMethodExpressionImpl extends TestCase {
  25. private ExpressionFactory factory;
  26. ELContext context;
  27. @Override
  28. public void setUp() {
  29. factory = ExpressionFactory.newInstance();
  30. context = new ELContextImpl();
  31. TesterBeanA beanA = new TesterBeanA();
  32. beanA.setName("A");
  33. context.getVariableMapper().setVariable("beanA",
  34. factory.createValueExpression(beanA, TesterBeanA.class));
  35. TesterBeanAA beanAA = new TesterBeanAA();
  36. beanAA.setName("AA");
  37. context.getVariableMapper().setVariable("beanAA",
  38. factory.createValueExpression(beanAA, TesterBeanAA.class));
  39. TesterBeanAAA beanAAA = new TesterBeanAAA();
  40. beanAAA.setName("AAA");
  41. context.getVariableMapper().setVariable("beanAAA",
  42. factory.createValueExpression(beanAAA, TesterBeanAAA.class));
  43. TesterBeanB beanB = new TesterBeanB();
  44. beanB.setName("B");
  45. context.getVariableMapper().setVariable("beanB",
  46. factory.createValueExpression(beanB, TesterBeanB.class));
  47. TesterBeanBB beanBB = new TesterBeanBB();
  48. beanBB.setName("BB");
  49. context.getVariableMapper().setVariable("beanBB",
  50. factory.createValueExpression(beanBB, TesterBeanBB.class));
  51. TesterBeanBBB beanBBB = new TesterBeanBBB();
  52. beanBBB.setName("BBB");
  53. context.getVariableMapper().setVariable("beanBBB",
  54. factory.createValueExpression(beanBBB, TesterBeanBBB.class));
  55. TesterBeanC beanC = new TesterBeanC();
  56. context.getVariableMapper().setVariable("beanC",
  57. factory.createValueExpression(beanC, TesterBeanC.class));
  58. }
  59. public void testIsParametersProvided() {
  60. TesterBeanB beanB = new TesterBeanB();
  61. beanB.setName("Tomcat");
  62. ValueExpression var =
  63. factory.createValueExpression(beanB, TesterBeanB.class);
  64. context.getVariableMapper().setVariable("beanB", var);
  65. MethodExpression me1 = factory.createMethodExpression(
  66. context, "${beanB.getName}", String.class, new Class<?>[] {});
  67. MethodExpression me2 = factory.createMethodExpression(
  68. context, "${beanB.sayHello('JUnit')}", String.class,
  69. new Class<?>[] { String.class });
  70. assertFalse(me1.isParmetersProvided());
  71. assertTrue(me2.isParmetersProvided());
  72. }
  73. public void testInvoke() {
  74. TesterBeanB beanB = new TesterBeanB();
  75. beanB.setName("B");
  76. context.getVariableMapper().setVariable("beanB",
  77. factory.createValueExpression(beanB, TesterBeanB.class));
  78. MethodExpression me1 = factory.createMethodExpression(
  79. context, "${beanB.getName}", String.class, new Class<?>[] {});
  80. MethodExpression me2 = factory.createMethodExpression(
  81. context, "${beanB.sayHello('JUnit')}", String.class,
  82. new Class<?>[] { String.class });
  83. MethodExpression me3 = factory.createMethodExpression(
  84. context, "${beanB.sayHello}", String.class,
  85. new Class<?>[] { String.class });
  86. assertEquals("B", me1.invoke(context, null));
  87. assertEquals("Hello JUnit from B", me2.invoke(context, null));
  88. assertEquals("Hello JUnit from B",
  89. me2.invoke(context, new Object[] { "JUnit2" }));
  90. assertEquals("Hello JUnit2 from B",
  91. me3.invoke(context, new Object[] { "JUnit2" }));
  92. assertEquals("Hello JUnit from B",
  93. me2.invoke(context, new Object[] { null }));
  94. assertEquals("Hello null from B",
  95. me3.invoke(context, new Object[] { null }));
  96. }
  97. public void testInvokeWithSuper() {
  98. MethodExpression me = factory.createMethodExpression(context,
  99. "${beanA.setBean(beanBB)}", null ,
  100. new Class<?>[] { TesterBeanB.class });
  101. me.invoke(context, null);
  102. ValueExpression ve = factory.createValueExpression(context,
  103. "${beanA.bean.name}", String.class);
  104. Object r = ve.getValue(context);
  105. assertEquals("BB", r);
  106. }
  107. public void testInvokeWithSuperABNoReturnTypeNoParamTypes() {
  108. MethodExpression me2 = factory.createMethodExpression(context,
  109. "${beanC.sayHello(beanA,beanB)}", null , null);
  110. Object r2 = me2.invoke(context, null);
  111. assertEquals("AB: Hello A from B", r2.toString());
  112. }
  113. public void testInvokeWithSuperABReturnTypeNoParamTypes() {
  114. MethodExpression me3 = factory.createMethodExpression(context,
  115. "${beanC.sayHello(beanA,beanB)}", String.class , null);
  116. Object r3 = me3.invoke(context, null);
  117. assertEquals("AB: Hello A from B", r3.toString());
  118. }
  119. public void testInvokeWithSuperABNoReturnTypeParamTypes() {
  120. MethodExpression me4 = factory.createMethodExpression(context,
  121. "${beanC.sayHello(beanA,beanB)}", null ,
  122. new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
  123. Object r4 = me4.invoke(context, null);
  124. assertEquals("AB: Hello A from B", r4.toString());
  125. }
  126. public void testInvokeWithSuperABReturnTypeParamTypes() {
  127. MethodExpression me5 = factory.createMethodExpression(context,
  128. "${beanC.sayHello(beanA,beanB)}", String.class ,
  129. new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
  130. Object r5 = me5.invoke(context, null);
  131. assertEquals("AB: Hello A from B", r5.toString());
  132. }
  133. public void testInvokeWithSuperABB() {
  134. MethodExpression me6 = factory.createMethodExpression(context,
  135. "${beanC.sayHello(beanA,beanBB)}", null , null);
  136. Object r6 = me6.invoke(context, null);
  137. assertEquals("ABB: Hello A from BB", r6.toString());
  138. }
  139. public void testInvokeWithSuperABBB() {
  140. MethodExpression me7 = factory.createMethodExpression(context,
  141. "${beanC.sayHello(beanA,beanBBB)}", null , null);
  142. Object r7 = me7.invoke(context, null);
  143. assertEquals("ABB: Hello A from BBB", r7.toString());
  144. }
  145. public void testInvokeWithSuperAAB() {
  146. MethodExpression me8 = factory.createMethodExpression(context,
  147. "${beanC.sayHello(beanAA,beanB)}", null , null);
  148. Object r8 = me8.invoke(context, null);
  149. assertEquals("AAB: Hello AA from B", r8.toString());
  150. }
  151. public void testInvokeWithSuperAABB() {
  152. MethodExpression me9 = factory.createMethodExpression(context,
  153. "${beanC.sayHello(beanAA,beanBB)}", null , null);
  154. Exception e = null;
  155. try {
  156. me9.invoke(context, null);
  157. } catch (Exception e1) {
  158. e = e1;
  159. }
  160. // Expected to fail
  161. assertNotNull(e);
  162. }
  163. public void testInvokeWithSuperAABBB() {
  164. // The Java compiler reports this as ambiguous. Using the parameter that
  165. // matches exactly seems reasonable to limit the scope of the method
  166. // search so the EL will find a match.
  167. MethodExpression me10 = factory.createMethodExpression(context,
  168. "${beanC.sayHello(beanAA,beanBBB)}", null , null);
  169. Object r10 = me10.invoke(context, null);
  170. assertEquals("AAB: Hello AA from BBB", r10.toString());
  171. }
  172. public void testInvokeWithSuperAAAB() {
  173. MethodExpression me11 = factory.createMethodExpression(context,
  174. "${beanC.sayHello(beanAAA,beanB)}", null , null);
  175. Object r11 = me11.invoke(context, null);
  176. assertEquals("AAB: Hello AAA from B", r11.toString());
  177. }
  178. public void testInvokeWithSuperAAABB() {
  179. // The Java compiler reports this as ambiguous. Using the parameter that
  180. // matches exactly seems reasonable to limit the scope of the method
  181. // search so the EL will find a match.
  182. MethodExpression me12 = factory.createMethodExpression(context,
  183. "${beanC.sayHello(beanAAA,beanBB)}", null , null);
  184. Object r12 = me12.invoke(context, null);
  185. assertEquals("ABB: Hello AAA from BB", r12.toString());
  186. }
  187. public void testInvokeWithSuperAAABBB() {
  188. MethodExpression me13 = factory.createMethodExpression(context,
  189. "${beanC.sayHello(beanAAA,beanBBB)}", null , null);
  190. Exception e = null;
  191. try {
  192. me13.invoke(context, null);
  193. } catch (Exception e1) {
  194. e = e1;
  195. }
  196. // Expected to fail
  197. assertNotNull(e);
  198. }
  199. public void testInvokeWithVarArgsAB() throws Exception {
  200. MethodExpression me1 = factory.createMethodExpression(context,
  201. "${beanC.sayHello(beanA,beanB,beanB)}", null , null);
  202. Exception e = null;
  203. try {
  204. me1.invoke(context, null);
  205. } catch (Exception e1) {
  206. e = e1;
  207. }
  208. // Expected to fail
  209. assertNotNull(e);
  210. }
  211. public void testInvokeWithVarArgsABB() throws Exception {
  212. MethodExpression me2 = factory.createMethodExpression(context,
  213. "${beanC.sayHello(beanA,beanBB,beanBB)}", null , null);
  214. Object r2 = me2.invoke(context, null);
  215. assertEquals("ABB[]: Hello A from BB, BB", r2.toString());
  216. }
  217. public void testInvokeWithVarArgsABBB() throws Exception {
  218. MethodExpression me3 = factory.createMethodExpression(context,
  219. "${beanC.sayHello(beanA,beanBBB,beanBBB)}", null , null);
  220. Object r3 = me3.invoke(context, null);
  221. assertEquals("ABB[]: Hello A from BBB, BBB", r3.toString());
  222. }
  223. public void testInvokeWithVarArgsAAB() throws Exception {
  224. MethodExpression me4 = factory.createMethodExpression(context,
  225. "${beanC.sayHello(beanAA,beanB,beanB)}", null , null);
  226. Exception e = null;
  227. try {
  228. me4.invoke(context, null);
  229. } catch (Exception e1) {
  230. e = e1;
  231. }
  232. // Expected to fail
  233. assertNotNull(e);
  234. }
  235. public void testInvokeWithVarArgsAABB() throws Exception {
  236. MethodExpression me5 = factory.createMethodExpression(context,
  237. "${beanC.sayHello(beanAA,beanBB,beanBB)}", null , null);
  238. Object r5 = me5.invoke(context, null);
  239. assertEquals("ABB[]: Hello AA from BB, BB", r5.toString());
  240. }
  241. public void testInvokeWithVarArgsAABBB() throws Exception {
  242. MethodExpression me6 = factory.createMethodExpression(context,
  243. "${beanC.sayHello(beanAA,beanBBB,beanBBB)}", null , null);
  244. Object r6 = me6.invoke(context, null);
  245. assertEquals("ABB[]: Hello AA from BBB, BBB", r6.toString());
  246. }
  247. public void testInvokeWithVarArgsAAAB() throws Exception {
  248. MethodExpression me7 = factory.createMethodExpression(context,
  249. "${beanC.sayHello(beanAAA,beanB,beanB)}", null , null);
  250. Exception e = null;
  251. try {
  252. me7.invoke(context, null);
  253. } catch (Exception e1) {
  254. e = e1;
  255. }
  256. // Expected to fail
  257. assertNotNull(e);
  258. }
  259. public void testInvokeWithVarArgsAAABB() throws Exception {
  260. MethodExpression me8 = factory.createMethodExpression(context,
  261. "${beanC.sayHello(beanAAA,beanBB,beanBB)}", null , null);
  262. Object r8 = me8.invoke(context, null);
  263. assertEquals("ABB[]: Hello AAA from BB, BB", r8.toString());
  264. }
  265. public void testInvokeWithVarArgsAAABBB() throws Exception {
  266. MethodExpression me9 = factory.createMethodExpression(context,
  267. "${beanC.sayHello(beanAAA,beanBBB,beanBBB)}", null , null);
  268. Object r9 = me9.invoke(context, null);
  269. assertEquals("ABB[]: Hello AAA from BBB, BBB", r9.toString());
  270. }
  271. /*
  272. * This is also tested implicitly in numerous places elsewhere in this
  273. * class.
  274. */
  275. public void testBug49655() throws Exception {
  276. // This is the call the failed
  277. MethodExpression me = factory.createMethodExpression(context,
  278. "#{beanA.setName('New value')}", null, null);
  279. // The rest is to check it worked correctly
  280. me.invoke(context, null);
  281. ValueExpression ve = factory.createValueExpression(context,
  282. "#{beanA.name}", java.lang.String.class);
  283. assertEquals("New value", ve.getValue(context));
  284. }
  285. }