Skip to content

Commit

Permalink
Add new tests for MethodReference
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 20, 2021
1 parent 251c720 commit 8688ba1
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 5 deletions.
27 changes: 24 additions & 3 deletions internal/docs/el/ELJavadocAssertions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!DOCTYPE javadoc SYSTEM "https://raw.githubusercontent.com/eclipse-ee4j/jakartaee-tck/master/internal/docs/dtd/javadoc_assertions.dtd">
<!--
Copyright (c) 2018, 2020 Oracle and/or its affiliates and others.
Copyright (c) 2018, 2021 Oracle and/or its affiliates and others.
All rights reserved.
This program and the accompanying materials are made available under the
Expand All @@ -20,8 +20,8 @@
-->

<javadoc>
<next-available-id>411</next-available-id>
<previous-id>385</previous-id>
<next-available-id>414</next-available-id>
<previous-id>410</previous-id>
<technology>EL</technology>
<id>EL</id>
<name>Expression Language</name>
Expand Down Expand Up @@ -5028,6 +5028,27 @@
</parameters>
</method>
</assertion>
<assertion required="true" impl-spec="false" status="active" testable="true">
<id>EL:JAVADOC:411</id>
<description>Returns the name of the method</description>
<package>jakarta.el</package>
<class-interface>MethodInfo</class-interface>
<method name="getName" return-type="java.lang.String" />
</assertion>
<assertion required="true" impl-spec="false" status="active" testable="true">
<id>EL:JAVADOC:412</id>
<description>Returns the return type of the method</description>
<package>jakarta.el</package>
<class-interface>MethodInfo</class-interface>
<method name="getReturnType" return-type="java.lang.Class" />
</assertion>
<assertion required="true" impl-spec="false" status="active" testable="true">
<id>EL:JAVADOC:413</id>
<description>Returns the parameter types of the method</description>
<package>jakarta.el</package>
<class-interface>MethodInfo</class-interface>
<method name="getParamTypes" return-type="java.lang.Class[]" />
</assertion>
</assertions>
</javadoc>

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2020 Oracle and/or its affiliates and others.
* Copyright (c) 2009, 2021 Oracle and/or its affiliates and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -26,11 +26,13 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.annotation.Annotation;

import jakarta.el.ELContext;
import jakarta.el.Expression;
import jakarta.el.MethodExpression;
import jakarta.el.MethodInfo;
import jakarta.el.MethodReference;
import jakarta.el.PropertyNotWritableException;
import jakarta.el.ValueExpression;

Expand Down Expand Up @@ -87,6 +89,85 @@ public static boolean testMethodInfo(MethodInfo minfo, String expectedName,
return pass;
}

public static boolean testMethodReference(MethodReference mref,
Object expectedBase, MethodInfo expectedMethodInfo,
Class<?>[] expectedAnnotationTypes,
Object[] expectedParamValues, StringBuffer buf) {

boolean pass = true;

Object base = mref.getBase();
MethodInfo minfo = mref.getMethodInfo();
Annotation[] annotations = mref.getAnnotations();
Object[] parameterValues = mref.getEvaluatedParameters();

if (base == null) {
buf.append("Did not get expected base object." + NLINE);
buf.append("Expected base = " + expectedBase + NLINE);
buf.append("Computed name = null" + NLINE);
pass = false;
} else if (!base.equals(expectedBase)) {
buf.append("Did not get expected base object." + NLINE);
buf.append("Expected base = " + expectedBase + NLINE);
buf.append("Computed base = " + base + NLINE);
pass = false;
}

if (minfo == null) {
buf.append("Did not get expected MethodInfo object." + NLINE);
buf.append("Expected MethodInfo = " + expectedMethodInfo + NLINE);
buf.append("Computed MethodInfo = null" + NLINE);
pass = false;
} else if (!minfo.equals(expectedMethodInfo)) {
buf.append("Did not get expected base object." + NLINE);
buf.append("Expected MethodInfo = " + expectedMethodInfo + NLINE);
buf.append("Computed MethodInfo = " + minfo + NLINE);
pass = false;
}

if (annotations == null) {
buf.append("Did not get expected annotation array." + NLINE);
buf.append("Computed array was null" + NLINE);
pass = false;
} else if (annotations.length != expectedAnnotationTypes.length) {
buf.append("Did not get expected number of annotations." + NLINE);
buf.append("Expected annotation array length = " + expectedAnnotationTypes.length + NLINE);
buf.append("Computed annotation array length = " + annotations.length + NLINE);
pass = false;
} else {
for (int i = 0; i < annotations.length; i++) {
if (!annotations[i].annotationType().equals(expectedAnnotationTypes[i])) {
buf.append("Did not get expected annotation type for array index = " + i + NLINE);
buf.append("Expected type = " + expectedAnnotationTypes[i] + NLINE);
buf.append("Computed type = " + annotations[i].getClass() + NLINE);
pass = false;
}
}
}

if (parameterValues == null) {
buf.append("Did not get expected parameter value array." + NLINE);
buf.append("Computed array was null" + NLINE);
pass = false;
} else if (parameterValues.length != expectedParamValues.length) {
buf.append("Did not get expected number of parameter values." + NLINE);
buf.append("Expected annotation array length = " + expectedParamValues.length + NLINE);
buf.append("Computed annotation array length = " + parameterValues.length + NLINE);
pass = false;
} else {
for (int i = 0; i < parameterValues.length; i++) {
if (!parameterValues[i].equals(expectedParamValues[i])) {
buf.append("Did not get expected parameter value for array index = " + i + NLINE);
buf.append("Expected type = " + expectedParamValues[i] + NLINE);
buf.append("Computed type = " + parameterValues[i] + NLINE);
pass = false;
}
}
}

return pass;
}

public static boolean testValueExpression(ValueExpression vexp,
ELContext context, String exprStr, Class expectedType,
Object expectedValue, boolean expectedReadOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void cleanup() throws Fault {
/**
* @testName: methodInfoTest
*
* @assertion_ids: EL:JAVADOC:87; EL:JAVADOC:88; EL:JAVADOC:89
* @assertion_ids: EL:JAVADOC:411;EL:JAVADOC:412;EL:JAVADOC:413
* @test_Strategy: Validate the behavior of MethodInfo MethodInfo class
* methods: MethodInfo.getName() MethodInfo.getReturnType()
* MethodInfo.getParamTypes()
Expand Down
131 changes: 131 additions & 0 deletions src/com/sun/ts/tests/el/api/jakarta_el/methodreference/ELClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.ts.tests.el.api.jakarta_el.methodreference;

import java.util.Properties;

import com.sun.javatest.Status;
import com.sun.ts.lib.harness.ServiceEETest;
import com.sun.ts.lib.util.TestUtil;
import com.sun.ts.tests.common.el.api.expression.ExpressionTest;
import com.sun.ts.tests.el.common.elcontext.VarMapperELContext;
import com.sun.ts.tests.el.common.util.ELTestUtil;
import com.sun.ts.tests.el.common.util.MethodsBean;

import jakarta.el.ELContext;
import jakarta.el.ExpressionFactory;
import jakarta.el.MethodExpression;
import jakarta.el.MethodInfo;
import jakarta.el.MethodNotFoundException;
import jakarta.el.MethodReference;
import jakarta.el.PropertyNotFoundException;
import jakarta.el.ValueExpression;

public class ELClient extends ServiceEETest {

private static final String NL = System.getProperty("line.seperator", "\n");

private Properties testProps;

public static void main(String[] args) {
ELClient theTests = new ELClient();
Status s = theTests.run(args, System.out, System.err);
s.exit();
}

public void setup(String[] args, Properties p) throws Fault {
TestUtil.logTrace("Setup method called");
this.testProps = p;
}

public void cleanup() throws Fault {
}

/**
* @testName: methodReferenceTest
*
* @assertion_ids: EL:JAVADOC:87; EL:JAVADOC:88; EL:JAVADOC:89
* @test_Strategy: Validate the behavior of MethodReference class methods:
* MethodReference.getBase() MethodReference.getMethodInfo()
* MethodReference.getAnnotations()
* MethodReference.getEvaluatedParameters
*/
public void methodReferenceTest() throws Fault {

StringBuffer buf = new StringBuffer();

boolean pass1, pass2, pass3, pass4;

try {
ExpressionFactory expFactory = ExpressionFactory.newInstance();
ELContext context = (new VarMapperELContext(testProps)).getELContext();

MethodsBean bean = new MethodsBean();
ValueExpression ve = expFactory.createValueExpression(bean, MethodsBean.class);
context.getVariableMapper().setVariable("bean", ve);

// case 1: non-null return value
MethodExpression mexp1 = expFactory.createMethodExpression(
context, "#{bean.targetF('aaa',1234)}", String.class, null);
MethodInfo minfo1 = mexp1.getMethodInfo(context);
MethodReference mref1 = mexp1.getMethodReference(context);
pass1 = ExpressionTest.testMethodReference(mref1, bean, minfo1,
new Class<?>[] { Deprecated.class },
new Object[] { "aaa", Long.valueOf(1234) },
buf);

// case 2: NPE
try {
mexp1.getMethodReference(null);
pass2 = false;
buf.append("Did not get expected NullPointerException for null context." + NL);
} catch (NullPointerException npe) {
pass2 = true;
}

// case 3: PNFE
MethodExpression mexp3 = expFactory.createMethodExpression(
context, "#{noSuchBean.method()}", String.class, null);
try {
mexp3.getMethodReference(context);
pass3 = false;
buf.append("Did not get expected PropertyNotFoundException return for unknown bean." + NL);
} catch (PropertyNotFoundException pnfe) {
pass3 = true;
}

// case 4: MNFE
MethodExpression mexp4 = expFactory.createMethodExpression(
context, "#{bean.noSuchMethod()}", String.class, null);
try {
mexp4.getMethodReference(context);
pass4 = false;
buf.append("Did not get expected MethodNotFoundException return for unknown bean." + NL);
} catch (MethodNotFoundException mnfe) {
pass4 = true;
}

} catch (Exception ex) {
TestUtil.logErr("Test getMethodReference threw an Exception!" + TestUtil.NEW_LINE +
"Received: " + ex.toString() + TestUtil.NEW_LINE);
ex.printStackTrace();
throw new Fault(ex);
}

if (!(pass1 && pass2 && pass3 && pass4))
throw new Fault(ELTestUtil.FAIL + NL + buf.toString());
}
}
32 changes: 32 additions & 0 deletions src/com/sun/ts/tests/el/api/jakarta_el/methodreference/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Copyright (c) 2021 Contributors to the Eclipse Foundation.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<project name="methodreference" basedir="." default="usage" >

<import file="../../../../../../../../../bin/xml/ts.import.xml"/>

<property name="app.name" value="methodreference" />

<target name="package">
<ts.vehicles name="methodreference"
classes="com/sun/ts/tests/el/common/**/*.class,
com/sun/ts/tests/common/el/api/**/*.class"
singleear="true" />
</target>

</project>

0 comments on commit 8688ba1

Please sign in to comment.