Skip to content

Commit

Permalink
Fix incorrect lambda coercion tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Jan 11, 2024
1 parent ae3aab9 commit 21d8e90
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions el/src/main/java/com/sun/ts/tests/el/spec/coercion/ELClientIT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2021 Oracle and/or its affiliates and others.
* Copyright (c) 2009, 2024 Oracle and/or its affiliates and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -1814,28 +1814,38 @@ public void elCoerceLambdaExpressionToFunctionalInterfaceTest() throws Exception
* both compareTo(Object) and compareTo(Long)/compareTo(String) methods will be found as potential matches. The
* method matching rules (see section 1.2.1.2 of the specification) require that overload resolution has a higher
* precedence than coercion resolution so it is always the compareTo(Object) method that will be used for the
* followingtests.
* following tests resulting in a ClassCastException (which is wrapped in an ELException).
*/

// Coercible lambda expression with wrong type
ELProcessor elp3 = new ELProcessor();
elp3.defineFunction("", "", "com.sun.ts.tests.el.spec.coercion.ELClientIT", "testPredicateLong");
result = elp3.eval("testPredicateLong(x -> x.compareTo('data') == 0)");
pass[3] = ExprEval.compareClass(result, String.class) && ExprEval.compareValue(result, "BLOCK");
try {
result = elp3.eval("testPredicateLong(x -> x.compareTo('data') == 0)");
} catch (ELException e) {
// String 'data' cannot be cast to Long
pass[3] = true;
}

// Coercible lambda expression where filter does not match and parameter needs to be coerced
ELProcessor elp4 = new ELProcessor();
elp4.defineFunction("", "", "com.sun.ts.tests.el.spec.coercion.ELClientIT", "testPredicateString");
result = elp4.eval("testPredicateString(x -> x.compareTo(1234) == 0)");
pass[4] = ExprEval.compareClass(result, String.class) && ExprEval.compareValue(result, "BLOCK");
try {
result = elp4.eval("testPredicateString(x -> x.compareTo(1234) == 0)");
} catch (ELException e) {
// Long 1234 cannot be cast to String. It is coercible but that should not used here.
pass[4] = true;
}

// Coercible lambda expression with coercible type but coercion rules mean this test fails
ELProcessor elp5 = new ELProcessor();
elp5.defineFunction("", "", "com.sun.ts.tests.el.spec.coercion.ELClientIT", "testPredicateLong");
result = elp5.eval("testPredicateLong(x -> x.compareTo('1234') == 0)");
pass[5] = ExprEval.compareClass(result, String.class) && ExprEval.compareValue(result, "BLOCK");


try {
result = elp5.eval("testPredicateLong(x -> x.compareTo('1234') == 0)");
} catch (ELException e) {
// String '1234' cannot be cast to String. It is coercible but that should not used here.
pass[5] = true;
}
} catch (Exception e) {
logger.log(Logger.Level.ERROR, "Testing coercion of lambda expressions to functional interfaces " +
"threw an Exception!" + TestUtil.NEW_LINE + "Received: " + e.toString() + TestUtil.NEW_LINE);
Expand Down

0 comments on commit 21d8e90

Please sign in to comment.