This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,264 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import java.io.File;
import java.util.Date;
import javax.el.ELException;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
import org.apache.el.lang.ELSupport;
import org.apache.jasper.el.ELContextImpl;
/**
* Tests the EL engine directly. Similar tests may be found in
* {@link org.apache.jasper.compiler.TestAttributeParser} and
* {@link TestELInJsp}.
*/
public class TestELEvaluation {
/**
* Test use of spaces in ternary expressions. This was primarily an EL
* parser bug.
*/
@Test
public void testBug42565() {
Assert.assertEquals("false", evaluateExpression("${false?true:false}"));
Assert.assertEquals("false", evaluateExpression("${false?true: false}"));
Assert.assertEquals("false", evaluateExpression("${false?true :false}"));
Assert.assertEquals("false", evaluateExpression("${false?true : false}"));
Assert.assertEquals("false", evaluateExpression("${false? true:false}"));
Assert.assertEquals("false", evaluateExpression("${false? true: false}"));
Assert.assertEquals("false", evaluateExpression("${false? true :false}"));
Assert.assertEquals("false", evaluateExpression("${false? true : false}"));
Assert.assertEquals("false", evaluateExpression("${false ?true:false}"));
Assert.assertEquals("false", evaluateExpression("${false ?true: false}"));
Assert.assertEquals("false", evaluateExpression("${false ?true :false}"));
Assert.assertEquals("false", evaluateExpression("${false ?true : false}"));
Assert.assertEquals("false", evaluateExpression("${false ? true:false}"));
Assert.assertEquals("false", evaluateExpression("${false ? true: false}"));
Assert.assertEquals("false", evaluateExpression("${false ? true :false}"));
Assert.assertEquals("false", evaluateExpression("${false ? true : false}"));
}
/**
* Test use nested ternary expressions. This was primarily an EL parser bug.
*/
@Test
public void testBug44994() {
Assert.assertEquals("none", evaluateExpression(
"${0 lt 0 ? 1 lt 0 ? 'many': 'one': 'none'}"));
Assert.assertEquals("one", evaluateExpression(
"${0 lt 1 ? 1 lt 1 ? 'many': 'one': 'none'}"));
Assert.assertEquals("many", evaluateExpression(
"${0 lt 2 ? 1 lt 2 ? 'many': 'one': 'none'}"));
}
@Test
public void testParserBug45511() {
// Test cases provided by OP
Assert.assertEquals("true", evaluateExpression("${empty ('')}"));
Assert.assertEquals("true", evaluateExpression("${empty('')}"));
Assert.assertEquals("false", evaluateExpression("${(true) and (false)}"));
Assert.assertEquals("false", evaluateExpression("${(true)and(false)}"));
}
@Test
public void testBug48112() {
// bug 48112
Assert.assertEquals("{world}", evaluateExpression("${fn:trim('{world}')}"));
}
@Test
public void testParserLiteralExpression() {
// Inspired by work on bug 45451, comments from kkolinko on the dev
// list and looking at the spec to find some edge cases
// '\' is only an escape character inside a StringLiteral
Assert.assertEquals("\\\\", evaluateExpression("\\\\"));
/*
* LiteralExpressions can only contain ${ or #{ if escaped with \
* \ is not an escape character in any other circumstances including \\
*/
Assert.assertEquals("\\", evaluateExpression("\\"));
Assert.assertEquals("$", evaluateExpression("$"));
Assert.assertEquals("#", evaluateExpression("#"));
Assert.assertEquals("\\$", evaluateExpression("\\$"));
Assert.assertEquals("\\#", evaluateExpression("\\#"));
Assert.assertEquals("\\\\$", evaluateExpression("\\\\$"));
Assert.assertEquals("\\\\#", evaluateExpression("\\\\#"));
Assert.assertEquals("${", evaluateExpression("\\${"));
Assert.assertEquals("#{", evaluateExpression("\\#{"));
Assert.assertEquals("\\${", evaluateExpression("\\\\${"));
Assert.assertEquals("\\#{", evaluateExpression("\\\\#{"));
// '\' is only an escape for '${' and '#{'.
Assert.assertEquals("\\$", evaluateExpression("\\$"));
Assert.assertEquals("${", evaluateExpression("\\${"));
Assert.assertEquals("\\$a", evaluateExpression("\\$a"));
Assert.assertEquals("\\a", evaluateExpression("\\a"));
Assert.assertEquals("\\\\", evaluateExpression("\\\\"));
}
@Test
public void testParserStringLiteral() {
// Inspired by work on bug 45451, comments from kkolinko on the dev
// list and looking at the spec to find some edge cases
// The only characters that can be escaped inside a String literal
// are \ " and '. # and $ are not escaped inside a String literal.
Assert.assertEquals("\\", evaluateExpression("${'\\\\'}"));
Assert.assertEquals("\\", evaluateExpression("${\"\\\\\"}"));
Assert.assertEquals("\\\"'$#", evaluateExpression("${'\\\\\\\"\\'$#'}"));
Assert.assertEquals("\\\"'$#", evaluateExpression("${\"\\\\\\\"\\'$#\"}"));
// Trying to quote # or $ should throw an error
Exception e = null;
try {
evaluateExpression("${'\\$'}");
} catch (ELException el) {
e = el;
}
Assert.assertNotNull(e);
Assert.assertEquals("\\$", evaluateExpression("${'\\\\$'}"));
Assert.assertEquals("\\\\$", evaluateExpression("${'\\\\\\\\$'}"));
// Can use ''' inside '"' when quoting with '"' and vice versa without
// escaping
Assert.assertEquals("\\\"", evaluateExpression("${'\\\\\"'}"));
Assert.assertEquals("\"\\", evaluateExpression("${'\"\\\\'}"));
Assert.assertEquals("\\'", evaluateExpression("${'\\\\\\''}"));
Assert.assertEquals("'\\", evaluateExpression("${'\\'\\\\'}"));
Assert.assertEquals("\\'", evaluateExpression("${\"\\\\'\"}"));
Assert.assertEquals("'\\", evaluateExpression("${\"'\\\\\"}"));
Assert.assertEquals("\\\"", evaluateExpression("${\"\\\\\\\"\"}"));
Assert.assertEquals("\"\\", evaluateExpression("${\"\\\"\\\\\"}"));
}
@Test
public void testMultipleEscaping() throws Exception {
Assert.assertEquals("''", evaluateExpression("${\"\'\'\"}"));
}
private void compareBoth(String msg, int expected, Object o1, Object o2){
int i1 = ELSupport.compare(null, o1, o2);
int i2 = ELSupport.compare(null, o2, o1);
Assert.assertEquals(msg,expected, i1);
Assert.assertEquals(msg,expected, -i2);
}
@Test
public void testElSupportCompare(){
compareBoth("Nulls should compare equal", 0, null, null);
compareBoth("Null should compare equal to \"\"", 0, "", null);
compareBoth("Null should be less than File()",-1, null, new File(""));
compareBoth("Null should be less than Date()",-1, null, new Date());
compareBoth("Date(0) should be less than Date(1)",-1, new Date(0), new Date(1));
try {
compareBoth("Should not compare",0, new Date(), new File(""));
Assert.fail("Expecting ClassCastException");
} catch (ClassCastException expected) {
// Expected
}
Assert.assertTrue(null == null);
}
/**
* Test mixing ${...} and #{...} in the same expression.
*/
@Test
public void testMixedTypes() {
// Mixing types should throw an error
Exception e = null;
try {
evaluateExpression("${1+1}#{1+1}");
} catch (ELException el) {
e = el;
}
Assert.assertNotNull(e);
}
@Test
public void testEscape01() {
Assert.assertEquals("$${", evaluateExpression("$\\${"));
}
@Test
public void testBug49081a() {
Assert.assertEquals("$2", evaluateExpression("$${1+1}"));
}
@Test
public void testBug49081b() {
Assert.assertEquals("#2", evaluateExpression("##{1+1}"));
}
@Test
public void testBug49081c() {
Assert.assertEquals("#2", evaluateExpression("#${1+1}"));
}
@Test
public void testBug49081d() {
Assert.assertEquals("$2", evaluateExpression("$#{1+1}"));
}
@Test
public void testBug60431a() {
Assert.assertEquals("OK", evaluateExpression("${fn:concat('O','K')}"));
}
@Test
public void testBug60431b() {
Assert.assertEquals("OK", evaluateExpression("${fn:concat(fn:toArray('O','K'))}"));
}
@Test
public void testBug60431c() {
Assert.assertEquals("", evaluateExpression("${fn:concat()}"));
}
@Test
public void testBug60431d() {
Assert.assertEquals("OK", evaluateExpression("${fn:concat2('OK')}"));
}
@Test
public void testBug60431e() {
Assert.assertEquals("RUOK", evaluateExpression("${fn:concat2('RU', fn:toArray('O','K'))}"));
}
// ************************************************************************
private String evaluateExpression(String expression) {
ExpressionFactoryImpl exprFactory = new ExpressionFactoryImpl();
ELContextImpl ctx = new ELContextImpl(exprFactory);
ctx.setFunctionMapper(new TesterFunctions.FMapper());
ValueExpression ve = exprFactory.createValueExpression(ctx, expression,
String.class);
return (String) ve.getValue(ctx);
}
}

View File

@@ -0,0 +1,528 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import java.io.File;
import java.math.RoundingMode;
import java.util.Collections;
import javax.servlet.DispatcherType;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.jasper.servlet.JasperInitializer;
import org.apache.tomcat.util.buf.ByteChunk;
/**
* Tests EL with an without JSP attributes using a test web application. Similar
* tests may be found in {@link TestELEvaluation} and
* {@link org.apache.jasper.compiler.TestAttributeParser}.
*/
public class TestELInJsp extends TomcatBaseTest {
@Test
public void testBug36923() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug36923.jsp");
String result = res.toString();
assertEcho(result, "00-${hello world}");
}
@Test
public void testBug42565() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug42565.jsp");
String result = res.toString();
assertEcho(result, "00-false");
assertEcho(result, "01-false");
assertEcho(result, "02-false");
assertEcho(result, "03-false");
assertEcho(result, "04-false");
assertEcho(result, "05-false");
assertEcho(result, "06-false");
assertEcho(result, "07-false");
assertEcho(result, "08-false");
assertEcho(result, "09-false");
assertEcho(result, "10-false");
assertEcho(result, "11-false");
assertEcho(result, "12-false");
assertEcho(result, "13-false");
assertEcho(result, "14-false");
assertEcho(result, "15-false");
}
@Test
public void testBug44994() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug44994.jsp");
String result = res.toString();
assertEcho(result, "00-none");
assertEcho(result, "01-one");
assertEcho(result, "02-many");
}
@Test
public void testBug45427() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug45nnn/bug45427.jsp");
String result = res.toString();
// Warning: JSP attribute escaping != Java String escaping
assertEcho(result, "00-hello world");
assertEcho(result, "01-hello 'world");
assertEcho(result, "02-hello \"world");
assertEcho(result, "03-hello \"world");
assertEcho(result, "04-hello world");
assertEcho(result, "05-hello 'world");
assertEcho(result, "06-hello 'world");
assertEcho(result, "07-hello \"world");
assertEcho(result, "08-hello world");
assertEcho(result, "09-hello 'world");
assertEcho(result, "10-hello \"world");
assertEcho(result, "11-hello \"world");
assertEcho(result, "12-hello world");
assertEcho(result, "13-hello 'world");
assertEcho(result, "14-hello 'world");
assertEcho(result, "15-hello \"world");
}
@Test
public void testBug45451() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug45nnn/bug45451a.jsp");
String result = res.toString();
// Warning: JSP attribute escaping != Java String escaping
assertEcho(result, "00-\\'hello world\\'");
assertEcho(result, "01-\\'hello world\\'");
assertEcho(result, "02-\\'hello world\\'");
assertEcho(result, "03-\\'hello world\\'");
res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451b.jsp");
result = res.toString();
// Warning: JSP attribute escaping != Java String escaping
// Warning: Attributes are always unescaped before passing to the EL
// processor
assertEcho(result, "00-2");
assertEcho(result, "01-${1+1}");
assertEcho(result, "02-\\${1+1}");
assertEcho(result, "03-\\\\${1+1}");
assertEcho(result, "04-$500");
// Inside an EL literal '\' is only used to escape '\', ''' and '"'
assertEcho(result, "05-\\$");
assertEcho(result, "06-\\${");
assertEcho(result, "10-2");
assertEcho(result, "11-${1+1}");
assertEcho(result, "12-\\2");
assertEcho(result, "13-\\${1+1}");
assertEcho(result, "14-\\\\2");
assertEcho(result, "15-$500");
assertEcho(result, "16-\\$");
assertEcho(result, "17-\\${");
assertEcho(result, "20-2");
assertEcho(result, "21-#{1+1}");
assertEcho(result, "22-\\2");
assertEcho(result, "23-\\#{1+1}");
assertEcho(result, "24-\\\\2");
assertEcho(result, "25-\\#");
assertEcho(result, "26-\\#{");
res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451c.jsp");
result = res.toString();
// Warning: JSP attribute escaping != Java String escaping
// TODO - Currently we allow a single unescaped \ in attribute values
// Review if this should cause a warning/error
assertEcho(result, "00-${1+1}");
assertEcho(result, "01-\\${1+1}");
assertEcho(result, "02-\\\\${1+1}");
assertEcho(result, "03-\\\\\\${1+1}");
assertEcho(result, "04-\\$500");
assertEcho(result, "10-${1+1}");
assertEcho(result, "11-\\${1+1}");
assertEcho(result, "12-\\${1+1}");
assertEcho(result, "13-\\\\${1+1}");
assertEcho(result, "14-\\\\${1+1}");
assertEcho(result, "15-\\$500");
assertEcho(result, "20-#{1+1}");
assertEcho(result, "21-\\#{1+1}");
assertEcho(result, "22-\\#{1+1}");
assertEcho(result, "23-\\\\#{1+1}");
assertEcho(result, "24-\\\\#{1+1}");
res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451d.jspx");
result = res.toString();
// Warning: JSP attribute escaping != Java String escaping
// \\ Is *not* an escape sequence in XML attributes
assertEcho(result, "00-2");
assertEcho(result, "01-${1+1}");
assertEcho(result, "02-\\${1+1}");
assertEcho(result, "03-\\\\${1+1}");
assertEcho(result, "04-$500");
assertEcho(result, "10-2");
assertEcho(result, "11-${1+1}");
assertEcho(result, "12-\\${1+1}");
assertEcho(result, "13-\\\\${1+1}");
assertEcho(result, "14-\\\\\\${1+1}");
assertEcho(result, "15-$500");
assertEcho(result, "20-2");
assertEcho(result, "21-#{1+1}");
assertEcho(result, "22-\\#{1+1}");
assertEcho(result, "23-\\\\#{1+1}");
assertEcho(result, "24-\\\\\\#{1+1}");
res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451e.jsp");
result = res.toString();
// Warning: JSP attribute escaping != Java String escaping
// Warning: Attributes are always unescaped before passing to the EL
// processor
assertEcho(result, "00-2");
assertEcho(result, "01-${1+1}");
assertEcho(result, "02-\\${1+1}");
assertEcho(result, "03-\\\\${1+1}");
assertEcho(result, "04-$500");
assertEcho(result, "10-2");
assertEcho(result, "11-${1+1}");
assertEcho(result, "12-\\2");
assertEcho(result, "13-\\${1+1}");
assertEcho(result, "14-\\\\2");
assertEcho(result, "15-$500");
assertEcho(result, "20-#{1+1}");
assertEcho(result, "21-\\#{1+1}");
assertEcho(result, "22-\\#{1+1}");
assertEcho(result, "23-\\\\#{1+1}");
assertEcho(result, "24-\\\\#{1+1}");
}
@Test
public void testBug45511() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug45nnn/bug45511.jsp");
String result = res.toString();
assertEcho(result, "00-true");
assertEcho(result, "01-false");
}
@Test
public void testBug46596() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug46596.jsp");
String result = res.toString();
assertEcho(result, "{OK}");
}
@Test
public void testBug47413() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug47413.jsp");
String result = res.toString();
assertEcho(result, "00-hello world");
assertEcho(result, "01-hello world");
assertEcho(result, "02-3.22");
assertEcho(result, "03-3.22");
assertEcho(result, "04-17");
assertEcho(result, "05-17");
assertEcho(result, "06-hello world");
assertEcho(result, "07-hello world");
assertEcho(result, "08-0.0");
assertEcho(result, "09-0.0");
assertEcho(result, "10-0");
assertEcho(result, "11-0");
}
@Test
public void testBug48112() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug48nnn/bug48112.jsp");
String result = res.toString();
assertEcho(result, "{OK}");
}
@Test
public void testBug49555() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49555.jsp");
String result = res.toString();
assertEcho(result, "00-" + TesterFunctions.Inner$Class.RETVAL);
}
@Test
public void testBug51544() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug51544.jsp");
String result = res.toString();
assertEcho(result, "Empty list: true");
}
@Test
public void testELMiscNoQuoteAttributeEL() throws Exception {
doTestELMisc(false);
}
@Test
public void testELMiscWithQuoteAttributeEL() throws Exception {
doTestELMisc(true);
}
private void doTestELMisc(boolean quoteAttributeEL) throws Exception {
Tomcat tomcat = getTomcatInstance();
// Create the context (don't use addWebapp as we want to modify the
// JSP Servlet settings).
File appDir = new File("test/webapp");
StandardContext ctxt = (StandardContext) tomcat.addContext(
null, "/test", appDir.getAbsolutePath());
ctxt.addServletContainerInitializer(new JasperInitializer(), null);
// Configure the defaults and then tweak the JSP servlet settings
// Note: Min value for maxLoadedJsps is 2
Tomcat.initWebappDefaults(ctxt);
Wrapper w = (Wrapper) ctxt.findChild("jsp");
String jspName;
if (quoteAttributeEL) {
jspName = "/test/el-misc-with-quote-attribute-el.jsp";
w.addInitParameter("quoteAttributeEL", "true");
} else {
jspName = "/test/el-misc-no-quote-attribute-el.jsp";
w.addInitParameter("quoteAttributeEL", "false");
}
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + jspName);
String result = res.toString();
assertEcho(result, "00-\\\\\\\"${'hello world'}");
assertEcho(result, "01-\\\\\\\"\\${'hello world'}");
assertEcho(result, "02-\\\"${'hello world'}");
assertEcho(result, "03-\\\"\\hello world");
assertEcho(result, "2az-04");
assertEcho(result, "05-a2z");
assertEcho(result, "06-az2");
assertEcho(result, "2az-07");
assertEcho(result, "08-a2z");
assertEcho(result, "09-az2");
assertEcho(result, "10-${'foo'}bar");
assertEcho(result, "11-\\\"}");
assertEcho(result, "12-foo\\bar\\baz");
assertEcho(result, "13-foo\\bar\\baz");
assertEcho(result, "14-foo\\bar\\baz");
assertEcho(result, "15-foo\\bar\\baz");
assertEcho(result, "16-foo\\bar\\baz");
assertEcho(result, "17-foo\\'bar'\\"baz"");
assertEcho(result, "18-3");
assertEcho(result, "19-4");
assertEcho(result, "20-4");
assertEcho(result, "21-[{value=11}, {value=12}, {value=13}, {value=14}]");
assertEcho(result, "22-[{value=11}, {value=12}, {value=13}, {value=14}]");
}
@Test
public void testScriptingExpression() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/script-expr.jsp");
String result = res.toString();
assertEcho(result, "00-hello world");
assertEcho(result, "01-hello \"world");
assertEcho(result, "02-hello \\\"world");
assertEcho(result, "03-hello ${world");
assertEcho(result, "04-hello \\${world");
assertEcho(result, "05-hello world");
assertEcho(result, "06-hello \"world");
assertEcho(result, "07-hello \\\"world");
assertEcho(result, "08-hello ${world");
assertEcho(result, "09-hello \\${world");
assertEcho(result, "10-hello <% world");
assertEcho(result, "11-hello %> world");
}
@Test
public void testELMethod() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/el-method.jsp");
String result = res.toString();
assertEcho(result, "00-Hello JUnit from Tomcat");
assertEcho(result, "01-Hello JUnit from Tomcat");
assertEcho(result, "02-Hello JUnit from Tomcat");
assertEcho(result, "03-Hello JUnit from Tomcat");
assertEcho(result, "04-Hello JUnit from Tomcat");
assertEcho(result, "05-Hello JUnit from Tomcat");
}
@Test
public void testBug56029() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug56029.jspx");
String result = res.toString();
Assert.assertTrue(result.contains("[1]:[1]"));
}
@Test
public void testBug56147() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug56147.jsp");
String result = res.toString();
assertEcho(result, "00-OK");
}
@Test
public void testBug56612() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug56612.jsp");
String result = res.toString();
Assert.assertTrue(result.contains("00-''"));
}
/*
* java.lang should be imported by default
*/
@Test
public void testBug57141() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug57141.jsp");
String result = res.toString();
assertEcho(result, "00-true");
assertEcho(result, "01-false");
assertEcho(result, "02-2147483647");
}
/*
* BZ https://bz.apache.org/bugzilla/show_bug.cgi?id=57142
* javax.servlet, javax.servlet.http and javax.servlet.jsp should be
* imported by default.
*/
@Test
public void testBug57142() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug57142.jsp");
String result = res.toString();
// javax.servlet
assertEcho(result, "00-" + DispatcherType.ASYNC);
// No obvious static fields for javax.servlet.http
// Could hack something with HttpUtils...
// No obvious static fields for javax.servlet.jsp
// Wild card (package) import
assertEcho(result, "01-" + RoundingMode.HALF_UP);
// Class import
assertEcho(result, "02-" + Collections.EMPTY_LIST.size());
}
/*
* BZ https://bz.apache.org/bugzilla/show_bug.cgi?id=57441
* Can't validate function names defined in lambdas (or via imports)
*/
@Test
public void testBug57441() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() +
"/test/bug5nnnn/bug57441.jsp");
String result = res.toString();
assertEcho(result, "00-11");
}
@Test
public void testBug60032() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug60032.jsp");
String result = res.toString();
assertEcho(result, "{OK}");
}
@Test
public void testBug60431() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug60431.jsp");
String result = res.toString();
assertEcho(result, "01-OK");
assertEcho(result, "02-OK");
assertEcho(result, "03-OK");
}
@Test
public void testBug61854a() throws Exception {
getTomcatInstanceTestWebapp(true, true);
ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug61854.jsp");
String result = res.toString();
assertEcho(result, "01-OK");
}
// Assertion for text contained with <p></p>, e.g. printed by tags:echo
private static void assertEcho(String result, String expected) {
Assert.assertTrue(result, result.indexOf("<p>" + expected + "</p>") > 0);
}
}

View File

@@ -0,0 +1,539 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.MethodNotFoundException;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.jasper.el.ELContextImpl;
public class TestMethodExpressionImpl {
private static final String BUG53792 = "TEST_PASS";
private ExpressionFactory factory;
private ELContext context;
private TesterBeanB beanB;
@Before
public void setUp() {
factory = ExpressionFactory.newInstance();
context = new ELContextImpl(factory);
TesterBeanA beanA = new TesterBeanA();
beanA.setName("A");
context.getVariableMapper().setVariable("beanA",
factory.createValueExpression(beanA, TesterBeanA.class));
TesterBeanAA beanAA = new TesterBeanAA();
beanAA.setName("AA");
context.getVariableMapper().setVariable("beanAA",
factory.createValueExpression(beanAA, TesterBeanAA.class));
TesterBeanAAA beanAAA = new TesterBeanAAA();
beanAAA.setName("AAA");
context.getVariableMapper().setVariable("beanAAA",
factory.createValueExpression(beanAAA, TesterBeanAAA.class));
beanB = new TesterBeanB();
beanB.setName("B");
context.getVariableMapper().setVariable("beanB",
factory.createValueExpression(beanB, TesterBeanB.class));
TesterBeanBB beanBB = new TesterBeanBB();
beanBB.setName("BB");
context.getVariableMapper().setVariable("beanBB",
factory.createValueExpression(beanBB, TesterBeanBB.class));
TesterBeanBBB beanBBB = new TesterBeanBBB();
beanBBB.setName("BBB");
context.getVariableMapper().setVariable("beanBBB",
factory.createValueExpression(beanBBB, TesterBeanBBB.class));
TesterBeanC beanC = new TesterBeanC();
context.getVariableMapper().setVariable("beanC",
factory.createValueExpression(beanC, TesterBeanC.class));
TesterBeanEnum beanEnum = new TesterBeanEnum();
context.getVariableMapper().setVariable("beanEnum",
factory.createValueExpression(beanEnum, TesterBeanEnum.class));
}
@Test
public void testIsParametersProvided() {
MethodExpression me1 = factory.createMethodExpression(
context, "${beanB.getName}", String.class, new Class<?>[] {});
MethodExpression me2 = factory.createMethodExpression(
context, "${beanB.sayHello('JUnit')}", String.class,
new Class<?>[] { String.class });
Assert.assertFalse(me1.isParametersProvided());
Assert.assertTrue(me2.isParametersProvided());
}
@Test
public void testInvoke() {
MethodExpression me1 = factory.createMethodExpression(
context, "${beanB.getName}", String.class, new Class<?>[] {});
MethodExpression me2 = factory.createMethodExpression(
context, "${beanB.sayHello('JUnit')}", String.class,
new Class<?>[] { String.class });
MethodExpression me3 = factory.createMethodExpression(
context, "${beanB.sayHello}", String.class,
new Class<?>[] { String.class });
Assert.assertEquals("B", me1.invoke(context, null));
Assert.assertEquals("Hello JUnit from B", me2.invoke(context, null));
Assert.assertEquals("Hello JUnit from B",
me2.invoke(context, new Object[] { "JUnit2" }));
Assert.assertEquals("Hello JUnit2 from B",
me3.invoke(context, new Object[] { "JUnit2" }));
Assert.assertEquals("Hello JUnit from B",
me2.invoke(context, new Object[] { null }));
Assert.assertEquals("Hello from B",
me3.invoke(context, new Object[] { null }));
}
@Test
public void testInvokeWithSuper() {
MethodExpression me = factory.createMethodExpression(context,
"${beanA.setBean(beanBB)}", null ,
new Class<?>[] { TesterBeanB.class });
me.invoke(context, null);
ValueExpression ve = factory.createValueExpression(context,
"${beanA.bean.name}", String.class);
Object r = ve.getValue(context);
Assert.assertEquals("BB", r);
}
@Test
public void testInvokeWithSuperABNoReturnTypeNoParamTypes() {
MethodExpression me2 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanB)}", null , null);
Object r2 = me2.invoke(context, null);
Assert.assertEquals("AB: Hello A from B", r2.toString());
}
@Test
public void testInvokeWithSuperABReturnTypeNoParamTypes() {
MethodExpression me3 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanB)}", String.class , null);
Object r3 = me3.invoke(context, null);
Assert.assertEquals("AB: Hello A from B", r3.toString());
}
@Test
public void testInvokeWithSuperABNoReturnTypeParamTypes() {
MethodExpression me4 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanB)}", null ,
new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
Object r4 = me4.invoke(context, null);
Assert.assertEquals("AB: Hello A from B", r4.toString());
}
@Test
public void testInvokeWithSuperABReturnTypeParamTypes() {
MethodExpression me5 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanB)}", String.class ,
new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
Object r5 = me5.invoke(context, null);
Assert.assertEquals("AB: Hello A from B", r5.toString());
}
@Test
public void testInvokeWithSuperABB() {
MethodExpression me6 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanBB)}", null , null);
Object r6 = me6.invoke(context, null);
Assert.assertEquals("ABB: Hello A from BB", r6.toString());
}
@Test
public void testInvokeWithSuperABBB() {
MethodExpression me7 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanBBB)}", null , null);
Object r7 = me7.invoke(context, null);
Assert.assertEquals("ABB: Hello A from BBB", r7.toString());
}
@Test
public void testInvokeWithSuperAAB() {
MethodExpression me8 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAA,beanB)}", null , null);
Object r8 = me8.invoke(context, null);
Assert.assertEquals("AAB: Hello AA from B", r8.toString());
}
@Test
public void testInvokeWithSuperAABB() {
MethodExpression me9 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAA,beanBB)}", null , null);
Exception e = null;
try {
me9.invoke(context, null);
} catch (Exception e1) {
e = e1;
}
// Expected to fail
Assert.assertNotNull(e);
}
@Test
public void testInvokeWithSuperAABBB() {
// The Java compiler reports this as ambiguous. Using the parameter that
// matches exactly seems reasonable to limit the scope of the method
// search so the EL will find a match.
MethodExpression me10 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAA,beanBBB)}", null , null);
Object r10 = me10.invoke(context, null);
Assert.assertEquals("AAB: Hello AA from BBB", r10.toString());
}
@Test
public void testInvokeWithSuperAAAB() {
MethodExpression me11 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAAA,beanB)}", null , null);
Object r11 = me11.invoke(context, null);
Assert.assertEquals("AAB: Hello AAA from B", r11.toString());
}
@Test
public void testInvokeWithSuperAAABB() {
// The Java compiler reports this as ambiguous. Using the parameter that
// matches exactly seems reasonable to limit the scope of the method
// search so the EL will find a match.
MethodExpression me12 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAAA,beanBB)}", null , null);
Object r12 = me12.invoke(context, null);
Assert.assertEquals("ABB: Hello AAA from BB", r12.toString());
}
@Test
public void testInvokeWithSuperAAABBB() {
MethodExpression me13 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAAA,beanBBB)}", null , null);
Exception e = null;
try {
me13.invoke(context, null);
} catch (Exception e1) {
e = e1;
}
// Expected to fail
Assert.assertNotNull(e);
}
@Test
public void testInvokeWithVarArgsAB() throws Exception {
MethodExpression me1 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanB,beanB)}", null , null);
Exception e = null;
try {
me1.invoke(context, null);
} catch (Exception e1) {
e = e1;
}
// Expected to fail
Assert.assertNotNull(e);
}
@Test
public void testInvokeWithVarArgsABB() throws Exception {
MethodExpression me2 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanBB,beanBB)}", null , null);
Object r2 = me2.invoke(context, null);
Assert.assertEquals("ABB[]: Hello A from BB, BB", r2.toString());
}
@Test
public void testInvokeWithVarArgsABBB() throws Exception {
MethodExpression me3 = factory.createMethodExpression(context,
"${beanC.sayHello(beanA,beanBBB,beanBBB)}", null , null);
Object r3 = me3.invoke(context, null);
Assert.assertEquals("ABB[]: Hello A from BBB, BBB", r3.toString());
}
@Test
public void testInvokeWithVarArgsAAB() throws Exception {
MethodExpression me4 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAA,beanB,beanB)}", null , null);
Exception e = null;
try {
me4.invoke(context, null);
} catch (Exception e1) {
e = e1;
}
// Expected to fail
Assert.assertNotNull(e);
}
@Test
public void testInvokeWithVarArgsAABB() throws Exception {
MethodExpression me5 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAA,beanBB,beanBB)}", null , null);
Object r5 = me5.invoke(context, null);
Assert.assertEquals("ABB[]: Hello AA from BB, BB", r5.toString());
}
@Test
public void testInvokeWithVarArgsAABBB() throws Exception {
MethodExpression me6 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAA,beanBBB,beanBBB)}", null , null);
Object r6 = me6.invoke(context, null);
Assert.assertEquals("ABB[]: Hello AA from BBB, BBB", r6.toString());
}
@Test
public void testInvokeWithVarArgsAAAB() throws Exception {
MethodExpression me7 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAAA,beanB,beanB)}", null , null);
Exception e = null;
try {
me7.invoke(context, null);
} catch (Exception e1) {
e = e1;
}
// Expected to fail
Assert.assertNotNull(e);
}
@Test
public void testInvokeWithVarArgsAAABB() throws Exception {
MethodExpression me8 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAAA,beanBB,beanBB)}", null , null);
Object r8 = me8.invoke(context, null);
Assert.assertEquals("ABB[]: Hello AAA from BB, BB", r8.toString());
}
@Test
public void testInvokeWithVarArgsAAABBB() throws Exception {
MethodExpression me9 = factory.createMethodExpression(context,
"${beanC.sayHello(beanAAA,beanBBB,beanBBB)}", null , null);
Object r9 = me9.invoke(context, null);
Assert.assertEquals("ABB[]: Hello AAA from BBB, BBB", r9.toString());
}
/*
* This is also tested implicitly in numerous places elsewhere in this
* class.
*/
@Test
public void testBug49655() throws Exception {
// This is the call the failed
MethodExpression me = factory.createMethodExpression(context,
"#{beanA.setName('New value')}", null, null);
// The rest is to check it worked correctly
me.invoke(context, null);
ValueExpression ve = factory.createValueExpression(context,
"#{beanA.name}", java.lang.String.class);
Assert.assertEquals("New value", ve.getValue(context));
}
@Test
public void testBugPrimitives() throws Exception {
MethodExpression me = factory.createMethodExpression(context,
"${beanA.setValLong(5)}", null, null);
me.invoke(context, null);
ValueExpression ve = factory.createValueExpression(context,
"#{beanA.valLong}", java.lang.String.class);
Assert.assertEquals("5", ve.getValue(context));
}
@Test
public void testBug50449a() throws Exception {
MethodExpression me1 = factory.createMethodExpression(context,
"${beanB.sayHello()}", null, null);
String actual = (String) me1.invoke(context, null);
Assert.assertEquals("Hello from B", actual);
}
@Test
public void testBug50449b() throws Exception {
MethodExpression me1 = factory.createMethodExpression(context,
"${beanB.sayHello('Tomcat')}", null, null);
String actual = (String) me1.invoke(context, null);
Assert.assertEquals("Hello Tomcat from B", actual);
}
@Test
public void testBug50790a() throws Exception {
ValueExpression ve = factory.createValueExpression(context,
"#{beanAA.name.contains(beanA.name)}", java.lang.Boolean.class);
Boolean actual = (Boolean) ve.getValue(context);
Assert.assertEquals(Boolean.TRUE, actual);
}
@Test
public void testBug50790b() throws Exception {
ValueExpression ve = factory.createValueExpression(context,
"#{beanA.name.contains(beanAA.name)}", java.lang.Boolean.class);
Boolean actual = (Boolean) ve.getValue(context);
Assert.assertEquals(Boolean.FALSE, actual);
}
@Test
public void testBug52445a() {
MethodExpression me = factory.createMethodExpression(context,
"${beanA.setBean(beanBB)}", null ,
new Class<?>[] { TesterBeanB.class });
me.invoke(context, null);
MethodExpression me1 = factory.createMethodExpression(context,
"${beanA.bean.sayHello()}", null, null);
String actual = (String) me1.invoke(context, null);
Assert.assertEquals("Hello from BB", actual);
}
@Test
public void testBug52970() {
MethodExpression me = factory.createMethodExpression(context,
"${beanEnum.submit('APPLE')}", null ,
new Class<?>[] { TesterBeanEnum.class });
me.invoke(context, null);
ValueExpression ve = factory.createValueExpression(context,
"#{beanEnum.lastSubmitted}", TesterEnum.class);
TesterEnum actual = (TesterEnum) ve.getValue(context);
Assert.assertEquals(TesterEnum.APPLE, actual);
}
@Test
public void testBug53792a() {
MethodExpression me = factory.createMethodExpression(context,
"${beanA.setBean(beanB)}", null ,
new Class<?>[] { TesterBeanB.class });
me.invoke(context, null);
me = factory.createMethodExpression(context,
"${beanB.setName('" + BUG53792 + "')}", null ,
new Class<?>[] { TesterBeanB.class });
me.invoke(context, null);
ValueExpression ve = factory.createValueExpression(context,
"#{beanA.getBean().name}", java.lang.String.class);
String actual = (String) ve.getValue(context);
Assert.assertEquals(BUG53792, actual);
}
@Test
public void testBug53792b() {
MethodExpression me = factory.createMethodExpression(context,
"${beanA.setBean(beanB)}", null ,
new Class<?>[] { TesterBeanB.class });
me.invoke(context, null);
me = factory.createMethodExpression(context,
"${beanB.setName('" + BUG53792 + "')}", null ,
new Class<?>[] { TesterBeanB.class });
me.invoke(context, null);
ValueExpression ve = factory.createValueExpression(context,
"#{beanA.getBean().name.length()}", java.lang.Integer.class);
Integer actual = (Integer) ve.getValue(context);
Assert.assertEquals(Integer.valueOf(BUG53792.length()), actual);
}
@Test
public void testBug53792c() {
MethodExpression me = factory.createMethodExpression(context,
"#{beanB.sayHello().length()}", null, new Class<?>[] {});
Integer result = (Integer) me.invoke(context, null);
Assert.assertEquals(beanB.sayHello().length(), result.intValue());
}
@Test
public void testBug53792d() {
MethodExpression me = factory.createMethodExpression(context,
"#{beanB.sayHello().length()}", null, new Class<?>[] {});
Integer result = (Integer) me.invoke(context, new Object[] { "foo" });
Assert.assertEquals(beanB.sayHello().length(), result.intValue());
}
@Test
public void testBug56797a() {
MethodExpression me = factory.createMethodExpression(context,
"${beanAA.echo1('Hello World!')}", null , null);
Object r = me.invoke(context, null);
Assert.assertEquals("AA1Hello World!", r.toString());
}
@Test
public void testBug56797b() {
MethodExpression me = factory.createMethodExpression(context,
"${beanAA.echo2('Hello World!')}", null , null);
Object r = me.invoke(context, null);
Assert.assertEquals("AA2Hello World!", r.toString());
}
@Test(expected=MethodNotFoundException.class)
public void testBug57855a() {
MethodExpression me = factory.createMethodExpression(context,
"${beanAA.echo2}", null , new Class[]{String.class});
me.invoke(context, new Object[0]);
}
@Test(expected=IllegalArgumentException.class)
public void testBug57855b() {
MethodExpression me = factory.createMethodExpression(context,
"${beanAA.echo2}", null , new Class[]{String.class});
me.invoke(context, null);
}
@Test
public void testBug57855c() {
MethodExpression me = factory.createMethodExpression(context,
"${beanB.echo}", null , new Class[]{String.class});
me.invoke(context, null);
}
@Test
public void testBug57855d() {
MethodExpression me = factory.createMethodExpression(context,
"${beanB.echo}", null , new Class[]{String.class});
Object r = me.invoke(context, new String[] { "aaa" });
Assert.assertEquals("aaa", r.toString());
}
@Test(expected=MethodNotFoundException.class)
public void testBug57855e() {
MethodExpression me = factory.createMethodExpression(context,
"${beanB.echo}", null , new Class[]{String.class});
Object r = me.invoke(context, new String[] { "aaa", "bbb" });
Assert.assertEquals("aaa, bbb", r.toString());
}
@Test(expected=IllegalArgumentException.class)
public void testBug60844() {
MethodExpression me2 = factory.createMethodExpression(context,
"${beanC.sayHello}", null , new Class[]{ TesterBeanA.class, TesterBeanB.class});
me2.invoke(context, new Object[] { new Object() });
}
}

View File

@@ -0,0 +1,267 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.el.ValueReference;
import org.junit.Assert;
import org.junit.Test;
import org.apache.jasper.el.ELContextImpl;
public class TestValueExpressionImpl {
@Test
public void testGetValueReference() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanB beanB = new TesterBeanB();
beanB.setName("Tomcat");
ValueExpression var =
factory.createValueExpression(beanB, TesterBeanB.class);
context.getVariableMapper().setVariable("beanB", var);
ValueExpression ve = factory.createValueExpression(
context, "${beanB.name}", String.class);
// First check the basics work
String result = (String) ve.getValue(context);
Assert.assertEquals("Tomcat", result);
// Now check the value reference
ValueReference vr = ve.getValueReference(context);
Assert.assertNotNull(vr);
Assert.assertEquals(beanB, vr.getBase());
Assert.assertEquals("name", vr.getProperty());
}
@Test
public void testGetValueReferenceVariable() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanB beanB = new TesterBeanB();
beanB.setName("Tomcat");
ValueExpression var =
factory.createValueExpression(beanB, TesterBeanB.class);
context.getVariableMapper().setVariable("beanB", var);
ValueExpression var2 = factory.createValueExpression(
context, "${beanB.name}", String.class);
context.getVariableMapper().setVariable("foo", var2);
ValueExpression ve = factory.createValueExpression(
context, "${foo}", ValueExpression.class);
// Now check the value reference
ValueReference vr = ve.getValueReference(context);
Assert.assertNotNull(vr);
Assert.assertEquals(beanB, vr.getBase());
Assert.assertEquals("name", vr.getProperty());
}
@Test
public void testBug49345() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanA beanA = new TesterBeanA();
TesterBeanB beanB = new TesterBeanB();
beanB.setName("Tomcat");
beanA.setBean(beanB);
ValueExpression var =
factory.createValueExpression(beanA, TesterBeanA.class);
context.getVariableMapper().setVariable("beanA", var);
ValueExpression ve = factory.createValueExpression(
context, "${beanA.bean.name}", String.class);
// First check the basics work
String result = (String) ve.getValue(context);
Assert.assertEquals("Tomcat", result);
// Now check the value reference
ValueReference vr = ve.getValueReference(context);
Assert.assertNotNull(vr);
Assert.assertEquals(beanB, vr.getBase());
Assert.assertEquals("name", vr.getProperty());
}
@Test
public void testBug50105() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterEnum testEnum = TesterEnum.APPLE;
ValueExpression var =
factory.createValueExpression(testEnum, TesterEnum.class);
context.getVariableMapper().setVariable("testEnum", var);
// When coercing an Enum to a String, name() should always be used.
ValueExpression ve1 = factory.createValueExpression(
context, "${testEnum}", String.class);
String result1 = (String) ve1.getValue(context);
Assert.assertEquals("APPLE", result1);
ValueExpression ve2 = factory.createValueExpression(
context, "foo${testEnum}bar", String.class);
String result2 = (String) ve2.getValue(context);
Assert.assertEquals("fooAPPLEbar", result2);
}
@Test
public void testBug51177ObjectMap() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
Object o1 = "String value";
Object o2 = Integer.valueOf(32);
Map<Object,Object> map = new HashMap<>();
map.put("key1", o1);
map.put("key2", o2);
ValueExpression var =
factory.createValueExpression(map, Map.class);
context.getVariableMapper().setVariable("map", var);
ValueExpression ve1 = factory.createValueExpression(
context, "${map.key1}", Object.class);
ve1.setValue(context, o2);
Assert.assertEquals(o2, ve1.getValue(context));
ValueExpression ve2 = factory.createValueExpression(
context, "${map.key2}", Object.class);
ve2.setValue(context, o1);
Assert.assertEquals(o1, ve2.getValue(context));
}
@Test
public void testBug51177ObjectList() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
Object o1 = "String value";
Object o2 = Integer.valueOf(32);
List<Object> list = new ArrayList<>();
list.add(0, o1);
list.add(1, o2);
ValueExpression var =
factory.createValueExpression(list, List.class);
context.getVariableMapper().setVariable("list", var);
ValueExpression ve1 = factory.createValueExpression(
context, "${list[0]}", Object.class);
ve1.setValue(context, o2);
Assert.assertEquals(o2, ve1.getValue(context));
ValueExpression ve2 = factory.createValueExpression(
context, "${list[1]}", Object.class);
ve2.setValue(context, o1);
Assert.assertEquals(o1, ve2.getValue(context));
}
/*
* Test returning an empty list as a bean property.
*/
@Test
public void testBug51544Bean() throws Exception {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanA beanA = new TesterBeanA();
beanA.setValList(Collections.emptyList());
ValueExpression var =
factory.createValueExpression(beanA, TesterBeanA.class);
context.getVariableMapper().setVariable("beanA", var);
ValueExpression ve = factory.createValueExpression(
context, "${beanA.valList.size()}", Integer.class);
Integer result = (Integer) ve.getValue(context);
Assert.assertEquals(Integer.valueOf(0), result);
}
/*
* Test using list directly as variable.
*/
@Test
public void testBug51544Direct() throws Exception {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
List<?> list = Collections.emptyList();
ValueExpression var =
factory.createValueExpression(list, List.class);
context.getVariableMapper().setVariable("list", var);
ValueExpression ve = factory.createValueExpression(
context, "${list.size()}", Integer.class);
Integer result = (Integer) ve.getValue(context);
Assert.assertEquals(Integer.valueOf(0), result);
}
@Test
public void testBug56522SetNullValue() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanB beanB = new TesterBeanB();
beanB.setName("Tomcat");
ValueExpression var =
factory.createValueExpression(beanB, TesterBeanB.class);
context.getVariableMapper().setVariable("beanB", var);
ValueExpression ve = factory.createValueExpression(
context, "${beanB.name}", String.class);
// First check the basics work
String result = (String) ve.getValue(context);
Assert.assertEquals("Tomcat", result);
// Now set the value to null
ve.setValue(context, null);
Assert.assertEquals("", beanB.getName());
}
}

View File

@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import java.util.List;
public class TesterBeanA {
private TesterBeanB bean;
private String name;
private long valLong;
private List<?> valList;
public TesterBeanB getBean() {
return bean;
}
public void setBean(TesterBeanB bean) {
this.bean = bean;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getValLong() {
return valLong;
}
public void setValLong(long valLong) {
this.valLong = valLong;
}
public List<?> getValList() {
return valList;
}
public void setValList(List<?> valList) {
this.valList = valList;
}
public CharSequence echo1(CharSequence cs) {
return "A1" + cs;
}
public CharSequence echo2(String s) {
return "A2" + s;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanAA extends TesterBeanA {
@Override
public String echo1(CharSequence cs) {
return "AA1" + cs.toString();
}
@Override
public String echo2(String s) {
return "AA2" + s;
}
}

View File

@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanAAA extends TesterBeanAA {
// No additional implementation - just need a class that extends AA for
// testing EL methods calls
}

View File

@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanB {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String sayHello() {
return "Hello from " + name;
}
public String sayHello(String to) {
return "Hello " + to + " from " + name;
}
public String echo(String...strings) {
if (strings == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(strings[i]);
}
return sb.toString();
}
}

View File

@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanBB extends TesterBeanB {
private String extra;
public String getExtra() {
return extra;
}
public void setExtra(String extra) {
this.extra = extra;
}
}

View File

@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanBBB extends TesterBeanBB {
// No additional implementation - just need a class that extends BB for
// testing EL methods calls
}

View File

@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanC {
public String sayHello(TesterBeanA a, TesterBeanB b) {
return "AB: Hello " + a.getName() + " from " + b.getName();
}
public String sayHello(TesterBeanAA a, TesterBeanB b) {
return "AAB: Hello " + a.getName() + " from " + b.getName();
}
public String sayHello(TesterBeanA a, TesterBeanBB b) {
return "ABB: Hello " + a.getName() + " from " + b.getName();
}
public String sayHello(TesterBeanA a, TesterBeanBB... b) {
StringBuilder result =
new StringBuilder("ABB[]: Hello " + a.getName() + " from ");
for (int i = 0; i < b.length; i++) {
if (i > 0) {
result.append(", ");
}
result.append(b[i].getName());
}
return result.toString();
}
}

View File

@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import java.util.ArrayList;
import java.util.List;
/**
* Class used to test the calling of overloaded methods.
*/
public class TesterBeanD {
private List<Object> things = new ArrayList<>();
public void addThing(String thing) {
things.add(thing);
}
public void addThing(Class<?> clazz) {
things.add(clazz);
}
public List<Object> getThings() {
return things;
}
}

View File

@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public class TesterBeanEnum {
private volatile TesterEnum lastSubmitted = null;
public void submit(TesterEnum testerEnum) {
this.lastSubmitted = testerEnum;
}
public TesterEnum getLastSubmitted() {
return lastSubmitted;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
public enum TesterEnum {
APPLE, ORANGE;
@Override
public String toString() {
return "This is a " + this.name();
}
}

View File

@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el;
import java.lang.reflect.Method;
import javax.el.FunctionMapper;
public class TesterFunctions {
public static String trim(String input) {
return input.trim();
}
public static String concat(String... inputs) {
if (inputs == null || inputs.length == 0) {
return null;
}
StringBuilder result = new StringBuilder();
for (String input : inputs) {
result.append(input);
}
return result.toString();
}
public static String concat2(String prefix, String... inputs) {
StringBuilder result = new StringBuilder(prefix);
for (String input : inputs) {
result.append(input);
}
return result.toString();
}
public static String[] toArray(String a, String b) {
return new String[] { a, b };
}
public static class Inner$Class {
public static final String RETVAL = "Return from bug49555";
public static String bug49555() {
return RETVAL;
}
}
public static class FMapper extends FunctionMapper {
@Override
public Method resolveFunction(String prefix, String localName) {
if ("trim".equals(localName)) {
Method m;
try {
m = TesterFunctions.class.getMethod("trim", String.class);
return m;
} catch (SecurityException e) {
// Ignore
} catch (NoSuchMethodException e) {
// Ignore
}
} else if ("concat".equals(localName)) {
Method m;
try {
m = TesterFunctions.class.getMethod("concat", String[].class);
return m;
} catch (SecurityException e) {
// Ignore
} catch (NoSuchMethodException e) {
// Ignore
}
} else if ("concat2".equals(localName)) {
Method m;
try {
m = TesterFunctions.class.getMethod("concat2", String.class, String[].class);
return m;
} catch (SecurityException e) {
// Ignore
} catch (NoSuchMethodException e) {
// Ignore
}
} else if ("toArray".equals(localName)) {
Method m;
try {
m = TesterFunctions.class.getMethod("toArray", String.class, String.class);
return m;
} catch (SecurityException e) {
// Ignore
} catch (NoSuchMethodException e) {
// Ignore
}
}
return null;
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestELArithmetic {
private static final String a = "1.1";
private static final BigInteger b =
new BigInteger("1000000000000000000000");
@Test
public void testAdd01() throws Exception {
Assert.assertEquals("1000000000000000000001.1",
String.valueOf(ELArithmetic.add(a, b)));
}
@Test
public void testAdd02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null + null");
Assert.assertEquals(Long.valueOf(0), result);
}
@Test
public void testSubtract01() throws Exception {
Assert.assertEquals("-999999999999999999998.9",
String.valueOf(ELArithmetic.subtract(a, b)));
}
@Test
public void testSubtract02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null - null");
Assert.assertEquals(Long.valueOf(0), result);
}
@Test
public void testMultiply01() throws Exception {
Assert.assertEquals("1100000000000000000000.0",
String.valueOf(ELArithmetic.multiply(a, b)));
}
@Test
public void testMultiply02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null * null");
Assert.assertEquals(Long.valueOf(0), result);
}
@Test
public void testDivide01() throws Exception {
Assert.assertEquals("0.0",
String.valueOf(ELArithmetic.divide(a, b)));
}
@Test
public void testDivide02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null / null");
Assert.assertEquals(Long.valueOf(0), result);
}
@Test
public void testMod01() throws Exception {
Assert.assertEquals("1.1",
String.valueOf(ELArithmetic.mod(a, b)));
}
@Test
public void testMod02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null % null");
Assert.assertEquals(Long.valueOf(0), result);
}
@Test
public void testUnaryMinus01() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("-null");
Assert.assertEquals(Long.valueOf(0), result);
}
@Test
public void testBug47371bigDecimal() throws Exception {
Assert.assertEquals(BigDecimal.valueOf(1),
ELArithmetic.add("", BigDecimal.valueOf(1)));
}
@Test
public void testBug47371double() throws Exception {
Assert.assertEquals(Double.valueOf(7), ELArithmetic.add("", Double.valueOf(7)));
}
@Test
public void testBug47371doubleString() throws Exception {
Assert.assertEquals(Double.valueOf(2), ELArithmetic.add("", "2."));
}
@Test
public void testBug47371bigInteger() throws Exception {
Assert.assertEquals(BigInteger.valueOf(0),
ELArithmetic.multiply("", BigInteger.valueOf(1)));
}
@Test
public void testBug47371long() throws Exception {
Assert.assertEquals(Long.valueOf(1), ELArithmetic.add("", Integer.valueOf(1)));
}
@Test
public void testBug47371long2() throws Exception {
Assert.assertEquals(Long.valueOf(-3), ELArithmetic.subtract("1", "4"));
}
@Test
public void testBug47371doubleString2() throws Exception {
Assert.assertEquals(Double.valueOf(2), ELArithmetic.add("1.", "1"));
}
}

View File

@@ -0,0 +1,283 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
import java.beans.PropertyEditorManager;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.el.ELException;
import javax.el.ELManager;
import org.junit.Assert;
import org.junit.Test;
public class TestELSupport {
@Test
public void testEquals() {
Assert.assertTrue(ELSupport.equals(null, "01", Long.valueOf(1)));
}
@Test
public void testBigDecimal() {
testIsSame(new BigDecimal(
"0.123456789012345678901234567890123456789012345678901234567890123456789"));
}
@Test
public void testBigInteger() {
testIsSame(new BigInteger(
"1234567890123456789012345678901234567890123456789012345678901234567890"));
}
@Test
public void testLong() {
testIsSame(Long.valueOf(0x0102030405060708L));
}
@Test
public void testInteger() {
testIsSame(Integer.valueOf(0x01020304));
}
@Test
public void testShort() {
testIsSame(Short.valueOf((short) 0x0102));
}
@Test
public void testByte() {
testIsSame(Byte.valueOf((byte) 0xEF));
}
@Test
public void testDouble() {
testIsSame(Double.valueOf(0.123456789012345678901234));
}
@Test
public void testFloat() {
testIsSame(Float.valueOf(0.123456F));
}
@Test
public void testCoerceIntegerToNumber() {
Integer input = Integer.valueOf(4390241);
Object output = ELSupport.coerceToType(null, input, Number.class);
Assert.assertEquals(input, output);
}
@Test
public void testCoerceNullToNumber() {
Object output = ELSupport.coerceToType(null, null, Number.class);
Assert.assertNull(output);
}
@Test
public void testCoerceEnumAToEnumA() {
Object output = null;
try {
output = ELSupport.coerceToEnum(null, TestEnumA.VALA1, TestEnumA.class);
} finally {
Assert.assertEquals(TestEnumA.VALA1, output);
}
}
@Test
public void testCoerceEnumAToEnumB() {
Object output = null;
try {
output = ELSupport.coerceToEnum(null, TestEnumA.VALA1, TestEnumB.class);
} catch (ELException ele) {
// Ignore
}
Assert.assertNull(output);
}
@Test
public void testCoerceEnumAToEnumC() {
Object output = null;
try {
output = ELSupport.coerceToEnum(null, TestEnumA.VALA1, TestEnumC.class);
} catch (ELException ele) {
// Ignore
}
Assert.assertNull(output);
}
@Test
public void testCoerceToType01() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, Integer.class);
Assert.assertNull("Result: " + result, result);
}
@Test
public void testCoerceToType02() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, int.class);
Assert.assertEquals(Integer.valueOf(0), result);
}
@Test
public void testCoerceToType03() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, boolean.class);
Assert.assertEquals(Boolean.valueOf(null), result);
}
@Test
public void testCoerceToType04() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, String.class);
Assert.assertEquals("", result);
}
@Test
public void testCoerceToType05() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, Character.class);
Assert.assertNull("Result: " + result, result);
}
@Test
public void testCoerceToType06() {
Object result = ELManager.getExpressionFactory().coerceToType(
"", Character.class);
Assert.assertEquals(Character.valueOf((char) 0), result);
}
@Test
public void testCoerceToType07() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, char.class);
Assert.assertEquals(Character.valueOf((char) 0), result);
}
@Test
public void testCoerceToType08() {
Object result = ELManager.getExpressionFactory().coerceToType(
"", char.class);
Assert.assertEquals(Character.valueOf((char) 0), result);
}
@Test
public void testCoerceToType09() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, Boolean.class);
Assert.assertNull("Result: " + result, result);
}
@Test
public void testCoerceToType10() {
Object result = ELManager.getExpressionFactory().coerceToType(
"", Boolean.class);
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void testCoerceToType11() {
Object result = ELManager.getExpressionFactory().coerceToType(
null, boolean.class);
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void testCoerceToType12() {
Object result = ELManager.getExpressionFactory().coerceToType(
"", boolean.class);
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void testCoerceToType13() {
Object result = ELManager.getExpressionFactory().coerceToType(
"", TesterType.class);
Assert.assertNull(result);
}
@Test
public void testCoerceToType14() {
PropertyEditorManager.registerEditor(TesterType.class, TesterTypeEditorNoError.class);
Object result = ELManager.getExpressionFactory().coerceToType(
"Foo", TesterType.class);
Assert.assertTrue(result instanceof TesterType);
Assert.assertEquals("Foo", ((TesterType) result).getValue());
}
@Test(expected=ELException.class)
public void testCoerceToType15() {
PropertyEditorManager.registerEditor(TesterType.class, TesterTypeEditorError.class);
Object result = ELManager.getExpressionFactory().coerceToType(
"Foo", TesterType.class);
Assert.assertTrue(result instanceof TesterType);
Assert.assertEquals("Foo", ((TesterType) result).getValue());
}
@Test
public void testCoerceToType16() {
PropertyEditorManager.registerEditor(TesterType.class, TesterTypeEditorError.class);
Object result = ELManager.getExpressionFactory().coerceToType(
"", TesterType.class);
Assert.assertNull(result);
}
@Test
public void testCoerceToNumber01() {
Object result = ELSupport.coerceToNumber(
null, (Object) null, Integer.class);
Assert.assertNull("Result: " + result, result);
}
@Test
public void testCoerceToNumber02() {
Object result = ELSupport.coerceToNumber(
null, (Object) null, int.class);
Assert.assertEquals(Integer.valueOf(0), result);
}
@Test
public void testCoerceToBoolean01() {
Object result = ELSupport.coerceToBoolean(null, null, true);
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void testCoerceToBoolean02() {
Object result = ELSupport.coerceToBoolean(null, null, false);
Assert.assertNull("Result: " + result, result);
}
private static void testIsSame(Object value) {
Assert.assertEquals(value, ELSupport.coerceToNumber(null, value, value.getClass()));
}
private static enum TestEnumA {
VALA1,
VALA2
}
private static enum TestEnumB {
VALB1,
VALB2
}
private static enum TestEnumC {
VALA1,
VALA2,
VALB1,
VALB2
}
}

View File

@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
public class TesterBean {
}

View File

@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
public class TesterType {
private final String value;
public TesterType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}

View File

@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;
public abstract class TesterTypeEditorBase implements PropertyEditor {
protected TesterType type = null;
@Override
public void setValue(Object value) {
}
@Override
public Object getValue() {
return type;
}
@Override
public boolean isPaintable() {
return false;
}
@Override
public void paintValue(Graphics gfx, Rectangle box) {
}
@Override
public String getJavaInitializationString() {
return null;
}
@Override
public String getAsText() {
return null;
}
@Override
public String[] getTags() {
return null;
}
@Override
public Component getCustomEditor() {
return null;
}
@Override
public boolean supportsCustomEditor() {
return false;
}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
}
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
}
}

View File

@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
public class TesterTypeEditorError extends TesterTypeEditorBase {
@Override
public void setAsText(String text) throws IllegalArgumentException {
throw new IllegalArgumentException();
}
}

View File

@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
public class TesterTypeEditorNoError extends TesterTypeEditorBase {
@Override
public void setAsText(String text) throws IllegalArgumentException {
type = new TesterType(text);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.lang;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ExpressionFactory;
import javax.el.TesterELContext;
import javax.el.ValueExpression;
import javax.el.VariableMapper;
import org.junit.Assert;
import org.junit.Test;
public class TesterVariableMapperImpl {
@Test
public void testSetVariable01() {
ExpressionFactory factory = ELManager.getExpressionFactory();
ELContext context = new TesterELContext();
ValueExpression ve1 =
factory.createValueExpression(context, "${1}", int.class);
ValueExpression ve2 =
factory.createValueExpression(context, "${2}", int.class);
ValueExpression ve3 =
factory.createValueExpression(context, "${3}", int.class);
VariableMapper mapper = new VariableMapperImpl();
mapper.setVariable("var1", ve1);
mapper.setVariable("var2", ve2);
mapper.setVariable("var3", ve3);
mapper.setVariable("var2", null);
Assert.assertEquals(ve1, mapper.resolveVariable("var1"));
Assert.assertNull(mapper.resolveVariable("var2"));
Assert.assertEquals(ve3, mapper.resolveVariable("var3"));
}
}

View File

@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstAnd {
@Test
public void test01() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("true && true");
Assert.assertEquals(Boolean.TRUE, result);
}
@Test
public void test02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("true && null");
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void test03() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null && true");
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void test04() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null && null");
Assert.assertEquals(Boolean.FALSE, result);
}
}

View File

@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ELProcessor;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
public class TestAstAssign {
@Test
public void testGetValue01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("bean01", new TesterBeanB());
Object result = processor.getValue(
"bean01.text = 'hello'", String.class);
Assert.assertEquals("hello", result);
}
@Test
public void testGetValue02() {
ELProcessor processor = new ELProcessor();
processor.defineBean("bean01", new TesterBeanB());
Object result = processor.getValue(
"bean01.text = 'hello'; bean01.text", String.class);
Assert.assertEquals("hello", result);
}
@Test
public void testGetType01() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
processor.defineBean("bean01", new TesterBeanB());
ValueExpression ve = factory.createValueExpression(
context, "${bean01.text = 'hello'}", String.class);
Assert.assertEquals(String.class, ve.getType(context));
Assert.assertEquals("hello", ve.getValue(context));
}
@Test
public void testGetType02() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
processor.defineBean("bean01", new TesterBeanB());
ValueExpression ve = factory.createValueExpression(
context, "${bean01.text = 'hello'; bean01.text}", String.class);
Assert.assertEquals(String.class, ve.getType(context));
Assert.assertEquals("hello", ve.getValue(context));
}
}

View File

@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstChoice {
@Test
public void test01() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null?1:2");
Assert.assertEquals(Long.valueOf(2), result);
}
}

View File

@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ELProcessor;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
public class TestAstConcatenation {
/**
* Test string concatenation.
*/
@Test
public void testConcatenation01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("'a' += 'b'", String.class);
Assert.assertEquals("ab", result);
}
/**
* Test coercion to string then concatenation.
*/
@Test
public void testConcatenation02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1 += 2", String.class);
Assert.assertEquals("12", result);
}
/**
* Test string concatenation with whitespace.
*/
@Test
public void testConcatenation03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("' a' += ' b '", String.class);
Assert.assertEquals(" a b ", result);
}
/**
* Test string concatenation with mixed types.
*/
@Test
public void testConcatenation04() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("'a' += 3", String.class);
Assert.assertEquals("a3", result);
}
/**
* Test operator precedence (+ before +=).
*/
@Test
public void testPrecedence01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1 + 2 += 3", String.class);
Assert.assertEquals("33", result);
}
/**
* Test operator precedence (+ before +=).
*/
@Test
public void testPrecedence02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1 += 2 + 3", String.class);
Assert.assertEquals("15", result);
}
/**
* Test operator precedence (+= before >).
*/
@Test
public void testPrecedence03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("10 > 2 += 3", String.class);
Assert.assertEquals("false", result);
}
/**
* Test operator precedence (+= before >).
*/
@Test
public void testPrecedence04() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1 += 2 > 3", String.class);
Assert.assertEquals("true", result);
}
@Test
public void testGetType() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
ValueExpression ve = factory.createValueExpression(
context, "${'a' += 3}", String.class);
Assert.assertEquals(String.class, ve.getType(context));
Assert.assertEquals("a3", ve.getValue(context));
}
}

View File

@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstFunction {
@Test
public void testImport01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("Integer(1000)", Integer.class);
Assert.assertEquals(Integer.valueOf(1000), result);
}
@Test
public void testImport02() {
ELProcessor processor = new ELProcessor();
processor.getELManager().getELContext().getImportHandler()
.importStatic("java.lang.Integer.valueOf");
Object result = processor.getValue("valueOf(1000)", Integer.class);
Assert.assertEquals(Integer.valueOf(1000), result);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstIdentifier {
@Test
public void testImport01() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("Integer.MAX_VALUE",
Integer.class);
Assert.assertEquals(Integer.valueOf(Integer.MAX_VALUE), result);
}
@Test
public void testImport02() {
ELProcessor processor = new ELProcessor();
processor.getELManager().getELContext().getImportHandler().importStatic(
"java.lang.Integer.MAX_VALUE");
Object result =
processor.getValue("MAX_VALUE",
Integer.class);
Assert.assertEquals(Integer.valueOf(Integer.MAX_VALUE), result);
}
}

View File

@@ -0,0 +1,236 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELException;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstLambdaExpression {
@Test
public void testSpec01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("(x->x+1)(1)", Integer.class);
Assert.assertEquals(Integer.valueOf(2), result);
}
@Test
public void testSpec02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("((x,y)->x+y)(1,2)", Integer.class);
Assert.assertEquals(Integer.valueOf(3), result);
}
@Test
public void testSpec03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("(()->64)", Integer.class);
Assert.assertEquals(Integer.valueOf(64), result);
}
@Test
public void testSpec04() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (x,y)->x+y; v(3,4)", Integer.class);
Assert.assertEquals(Integer.valueOf(7), result);
}
@Test
public void testSpec05() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("fact = n -> n==0? 1: n*fact(n-1); fact(5)",
Integer.class);
Assert.assertEquals(Integer.valueOf(120), result);
}
@Test
public void testSpec06() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(x->y->x-y)(2)(1)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testInvocation01() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(()->2)()",
Integer.class);
Assert.assertEquals(Integer.valueOf(2), result);
}
@Test
public void testNested01() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(()->y->2-y)()(1)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testNested02() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(()->y->()->2-y)()(1)()",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test(expected=ELException.class)
public void testNested03() {
ELProcessor processor = new ELProcessor();
// More method parameters than there are nested lambda expressions
processor.getValue("(()->y->()->2-y)()(1)()()",
Integer.class);
}
@Test
public void testNested04() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(()->y->()->x->x-y)()(1)()(2)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testNested05() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(()->y->()->()->x->x-y)()(1)()()(2)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testNested06() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("(()->y->()->()->x->x-y)()(1)()(3)(2)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testNested07() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("()->()->()->42",
Integer.class);
Assert.assertEquals(Integer.valueOf(42), result);
}
@Test
public void testLambdaAsFunction01() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (x->y->x-y); v(2)(1)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testLambdaAsFunction02() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (()->y->2-y); v()(1)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testLambdaAsFunction03() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (()->y->()->2-y); v()(1)()",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test(expected=ELException.class)
public void testLambdaAsFunction04() {
ELProcessor processor = new ELProcessor();
// More method parameters than there are nested lambda expressions
processor.getValue("v = (()->y->()->2-y); v()(1)()()",
Integer.class);
}
@Test
public void testLambdaAsFunction05() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (()->y->()->x->x-y); v()(1)()(2)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testLambdaAsFunction06() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (()->y->()->()->x->x-y); v()(1)()()(2)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test
public void testLambdaAsFunction07() {
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("v = (()->y->()->()->x->x-y); v()(1)()(3)(2)",
Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
@Test(expected=javax.el.ELException.class)
public void testLambdaAsFunction08() {
// Using a name space for the function is not allowed
ELProcessor processor = new ELProcessor();
Object result =
processor.getValue("foo:v = (x)->x+1; foo:v(0)", Integer.class);
Assert.assertEquals(Integer.valueOf(1), result);
}
}

View File

@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ELProcessor;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
public class TestAstListData {
private static final List<String> simpleList = new ArrayList<>();
private static final List<Object> nestedList = new ArrayList<>();
static {
simpleList.add("a");
simpleList.add("b");
simpleList.add("c");
simpleList.add("b");
simpleList.add("c");
nestedList.add(simpleList);
nestedList.add(Collections.EMPTY_LIST);
nestedList.add("d");
}
@Test
public void testSimple01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"['a','b','c', 'b', 'c']", List.class);
Assert.assertEquals(simpleList, result);
}
@Test
public void testSimple02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("[]", List.class);
Assert.assertEquals(Collections.EMPTY_LIST, result);
}
@Test
public void testNested01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[['a','b','c','b','c'],[],'d']", List.class);
Assert.assertEquals(nestedList, result);
}
@Test
public void testGetType() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
ValueExpression ve = factory.createValueExpression(
context, "${['a','b','c','b','c']}", List.class);
Assert.assertEquals(List.class, ve.getType(context));
Assert.assertEquals(simpleList, ve.getValue(context));
}
}

View File

@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ELProcessor;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
public class TestAstMapData {
private static final Map<String,String> simpleMap = new HashMap<>();
private static final Map<Object,Object> nestedMap = new HashMap<>();
static {
simpleMap.put("a", "1");
simpleMap.put("b", "2");
simpleMap.put("c", "3");
nestedMap.put("simple", simpleMap);
// {} will get parsed as an empty Set as there is nothing to hint to the
// parser that Map is expected here.
nestedMap.put("empty", Collections.EMPTY_SET);
nestedMap.put("d", "4");
}
@Test
public void testSimple01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"{'a':'1','b':'2','c':'3'}", Map.class);
Assert.assertEquals(simpleMap, result);
}
@Test
public void testSimple02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("{}", Map.class);
Assert.assertEquals(Collections.EMPTY_MAP, result);
}
@Test
public void testNested01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"{'simple':{'a':'1','b':'2','c':'3'}," +
"'empty':{}," +
"'d':'4'}", Map.class);
Assert.assertEquals(nestedMap, result);
}
@Test
public void testGetType() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
ValueExpression ve = factory.createValueExpression(
context, "${{'a':'1','b':'2','c':'3'}}", Map.class);
Assert.assertEquals(Map.class, ve.getType(context));
Assert.assertEquals(simpleMap, ve.getValue(context));
}
}

View File

@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstNot {
@Test
public void test01() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("!null");
Assert.assertEquals(Boolean.TRUE, result);
}
@Test
public void test02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("!true");
Assert.assertEquals(Boolean.FALSE, result);
}
@Test
public void test03() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("!false");
Assert.assertEquals(Boolean.TRUE, result);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
public class TestAstOr {
@Test
public void test01() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("true || true");
Assert.assertEquals(Boolean.TRUE, result);
}
@Test
public void test02() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("true || null");
Assert.assertEquals(Boolean.TRUE, result);
}
@Test
public void test03() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null || true");
Assert.assertEquals(Boolean.TRUE, result);
}
@Test
public void test04() {
ELProcessor processor = new ELProcessor();
Object result = processor.eval("null || null");
Assert.assertEquals(Boolean.FALSE, result);
}
}

View File

@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ELProcessor;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
public class TestAstSemicolon {
@Test
public void testGetValue01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1;2", String.class);
Assert.assertEquals("2", result);
}
@Test
public void testGetValue02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1;2", Integer.class);
Assert.assertEquals(Integer.valueOf(2), result);
}
@Test
public void testGetValue03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("1;2 + 3", Integer.class);
Assert.assertEquals(Integer.valueOf(5), result);
}
@Test
public void testGetType() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
ValueExpression ve = factory.createValueExpression(
context, "${1+1;2+2}", Integer.class);
Assert.assertEquals(Number.class, ve.getType(context));
Assert.assertEquals(Integer.valueOf(4), ve.getValue(context));
}
}

View File

@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.el.ELContext;
import javax.el.ELManager;
import javax.el.ELProcessor;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Test;
public class TestAstSetData {
private static final Set<String> simpleSet = new HashSet<>();
private static final Set<Object> nestedSet = new HashSet<>();
static {
simpleSet.add("a");
simpleSet.add("b");
simpleSet.add("c");
nestedSet.add(simpleSet);
nestedSet.add(Collections.EMPTY_SET);
nestedSet.add("d");
}
@Test
public void testSimple01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("{'a','b','c'}", Set.class);
Assert.assertEquals(simpleSet, result);
}
@Test
public void testSimple02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("{}", Set.class);
Assert.assertEquals(Collections.EMPTY_SET, result);
}
@Test
public void testNested01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("{{'a','b','c'},{},'d'}", Set.class);
Assert.assertEquals(nestedSet, result);
}
@Test
public void testGetType() {
ELProcessor processor = new ELProcessor();
ELContext context = processor.getELManager().getELContext();
ExpressionFactory factory = ELManager.getExpressionFactory();
ValueExpression ve = factory.createValueExpression(
context, "${{'a','b','c'}}", Set.class);
Assert.assertEquals(Set.class, ve.getType(context));
Assert.assertEquals(simpleSet, ve.getValue(context));
}
}

View File

@@ -0,0 +1,291 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
import java.io.StringReader;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.jasper.el.ELContextImpl;
import org.apache.tomcat.util.collections.SynchronizedStack;
public class TestELParser {
@Test
public void testBug49081() {
// OP's report
testExpression("#${1+1}", "#2");
// Variations on a theme
testExpression("#", "#");
testExpression("##", "##");
testExpression("###", "###");
testExpression("$", "$");
testExpression("$$", "$$");
testExpression("$$$", "$$$");
testExpression("#$", "#$");
testExpression("#$#", "#$#");
testExpression("$#", "$#");
testExpression("$#$", "$#$");
testExpression("#{1+1}", "2");
testExpression("##{1+1}", "#2");
testExpression("###{1+1}", "##2");
testExpression("${1+1}", "2");
testExpression("$${1+1}", "$2");
testExpression("$$${1+1}", "$$2");
testExpression("#${1+1}", "#2");
testExpression("#$#{1+1}", "#$2");
testExpression("$#{1+1}", "$2");
testExpression("$#${1+1}", "$#2");
}
@Test
public void testJavaKeyWordSuffix() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanA beanA = new TesterBeanA();
beanA.setInt("five");
ValueExpression var =
factory.createValueExpression(beanA, TesterBeanA.class);
context.getVariableMapper().setVariable("beanA", var);
// Should fail
Exception e = null;
try {
factory.createValueExpression(context, "${beanA.int}",
String.class);
} catch (ELException ele) {
e = ele;
}
Assert.assertNotNull(e);
}
@Test
public void testJavaKeyWordIdentifier() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanA beanA = new TesterBeanA();
beanA.setInt("five");
ValueExpression var =
factory.createValueExpression(beanA, TesterBeanA.class);
context.getVariableMapper().setVariable("this", var);
// Should fail
Exception e = null;
try {
factory.createValueExpression(context, "${this}", String.class);
} catch (ELException ele) {
e = ele;
}
Assert.assertNotNull(e);
}
@Test
public void bug56179a() {
doTestBug56179(0, "test == true");
}
@Test
public void bug56179b() {
doTestBug56179(1, "test == true");
}
@Test
public void bug56179c() {
doTestBug56179(2, "test == true");
}
@Test
public void bug56179d() {
doTestBug56179(3, "test == true");
}
@Test
public void bug56179e() {
doTestBug56179(4, "test == true");
}
@Test
public void bug56179f() {
doTestBug56179(5, "test == true");
}
@Test
public void bug56179g() {
doTestBug56179(0, "(test) == true");
}
@Test
public void bug56179h() {
doTestBug56179(1, "(test) == true");
}
@Test
public void bug56179i() {
doTestBug56179(2, "(test) == true");
}
@Test
public void bug56179j() {
doTestBug56179(3, "(test) == true");
}
@Test
public void bug56179k() {
doTestBug56179(4, "(test) == true");
}
@Test
public void bug56179l() {
doTestBug56179(5, "(test) == true");
}
@Test
public void bug56179m() {
doTestBug56179(5, "((test)) == true");
}
@Test
public void bug56179n() {
doTestBug56179(5, "(((test))) == true");
}
private void doTestBug56179(int parenthesesCount, String innerExpr) {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
ValueExpression var =
factory.createValueExpression(Boolean.TRUE, Boolean.class);
context.getVariableMapper().setVariable("test", var);
StringBuilder expr = new StringBuilder();
expr.append("${");
for (int i = 0; i < parenthesesCount; i++) {
expr.append("(");
}
expr.append(innerExpr);
for (int i = 0; i < parenthesesCount; i++) {
expr.append(")");
}
expr.append("}");
ValueExpression ve = factory.createValueExpression(
context, expr.toString(), String.class);
String result = (String) ve.getValue(context);
Assert.assertEquals("true", result);
}
@Test
public void bug56185() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
TesterBeanC beanC = new TesterBeanC();
ValueExpression var =
factory.createValueExpression(beanC, TesterBeanC.class);
context.getVariableMapper().setVariable("myBean", var);
ValueExpression ve = factory.createValueExpression(context,
"${(myBean.int1 > 1 and myBean.myBool) or "+
"((myBean.myBool or myBean.myBool1) and myBean.int1 > 1)}",
Boolean.class);
Assert.assertEquals(Boolean.FALSE, ve.getValue(context));
beanC.setInt1(2);
beanC.setMyBool1(true);
Assert.assertEquals(Boolean.TRUE, ve.getValue(context));
}
private void testExpression(String expression, String expected) {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl(factory);
ValueExpression ve = factory.createValueExpression(
context, expression, String.class);
String result = (String) ve.getValue(context);
Assert.assertEquals(expected, result);
}
/*
* Test to explore if re-using Parser instances is faster.
*
* Tests on my laptop show:
* - overhead by introducing the stack is in the noise for parsing even the
* simplest expression
* - efficiency from re-using the ELParser is measurable for even a single
* reuse of the parser
* - with large numbers of parses (~10k) performance for a trivial parse is
* three times faster
* - around the 100 iterations mark GC overhead adds significant noise to
* the results - for consistent results you either need fewer parses to
* avoid triggering GC or more parses so the GC effects are evenly
* distributed between the runs
*
* Note that the test is single threaded.
*/
@Ignore
@Test
public void testParserPerformance() throws ParseException {
final int runs = 20;
final int parseIterations = 10000;
for (int j = 0; j < runs; j ++) {
long start = System.nanoTime();
SynchronizedStack<ELParser> stack = new SynchronizedStack<>();
for (int i = 0; i < parseIterations; i ++) {
ELParser parser = stack.pop();
if (parser == null) {
parser = new ELParser(new StringReader("${'foo'}"));
} else {
parser.ReInit(new StringReader("${'foo'}"));
}
parser.CompositeExpression();
stack.push(parser);
}
long end = System.nanoTime();
System.out.println(parseIterations +
" iterations using ELParser.ReInit(...) took " + (end - start) + "ns");
}
for (int j = 0; j < runs; j ++) {
long start = System.nanoTime();
for (int i = 0; i < parseIterations; i ++) {
ELParser parser = new ELParser(new StringReader("${'foo'}"));
parser.CompositeExpression();
}
long end = System.nanoTime();
System.out.println(parseIterations +
" iterations using new ELParser(...) took " + (end - start) + "ns");
}
}
}

View File

@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
public class TesterBeanA {
private String keywordInt;
public String getInt() {
return keywordInt;
}
public void setInt(String keywordInt) {
this.keywordInt = keywordInt;
}
}

View File

@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
public class TesterBeanB {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.parser;
public class TesterBeanC {
private int int1;
private boolean myBool;
private boolean myBool1;
public int getInt1() {
return int1;
}
public void setInt1(int int1) {
this.int1 = int1;
}
public boolean isMyBool() {
return myBool;
}
public void setMyBool(boolean myBool) {
this.myBool = myBool;
}
public boolean isMyBool1() {
return myBool1;
}
public void setMyBool1(boolean myBool1) {
this.myBool1 = myBool1;
}
}

View File

@@ -0,0 +1,779 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.el.ELException;
import javax.el.ELProcessor;
import org.junit.Assert;
import org.junit.Test;
import org.apache.el.TesterBeanA;
import org.apache.el.lang.ELSupport;
public class TestCollectionOperations {
private static final TesterBeanA bean01 = new TesterBeanA();
private static final TesterBeanA bean02 = new TesterBeanA();
private static final TesterBeanA bean03 = new TesterBeanA();
private static final List<TesterBeanA> beans;
static {
List<TesterBeanA> list = new ArrayList<>();
bean01.setValLong(1);
bean01.setName("bean01");
list.add(bean01);
bean02.setValLong(2);
bean02.setName("bean02");
list.add(bean02);
bean03.setValLong(3);
bean03.setName("bean03");
list.add(bean03);
beans = Collections.unmodifiableList(list);
}
@Test
public void testToList01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue("['a','b','c'].stream().toList()",
List.class);
List<String> expected = new ArrayList<>(3);
expected.add("a");
expected.add("b");
expected.add("c");
Assert.assertEquals(expected, result);
}
@Test
public void testToList02() {
ELProcessor processor = new ELProcessor();
String[] src = new String[] { "a", "b", "c" };
processor.defineBean("src", src);
Object result = processor.getValue("src.stream().toList()",
List.class);
List<String> expected = new ArrayList<>(3);
expected.add("a");
expected.add("b");
expected.add("c");
Assert.assertEquals(expected, result);
}
@Test
public void testFilter01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().filter(b->b.valLong > 2).toList()",
List.class);
List<TesterBeanA> expected = new ArrayList<>(1);
expected.add(bean03);
Assert.assertEquals(expected, result);
}
@Test
public void testMap01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().map(b->b.name).toList()",
List.class);
List<String> expected = new ArrayList<>(3);
expected.add("bean01");
expected.add("bean02");
expected.add("bean03");
Assert.assertEquals(expected, result);
}
@Test
public void testMap02() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().filter(b->b.valLong > 1).map(b->[b.name, b.valLong]).toList()",
List.class);
Assert.assertTrue(result instanceof List);
@SuppressWarnings("unchecked")
List<List<Object>> list = (List<List<Object>>) result;
Assert.assertEquals(2, list.size());
Assert.assertEquals("bean02", list.get(0).get(0));
Assert.assertEquals(Long.valueOf(2), list.get(0).get(1));
Assert.assertEquals("bean03", list.get(1).get(0));
Assert.assertEquals(Long.valueOf(3), list.get(1).get(1));
}
@Test
public void testFlatMap01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().flatMap(b->b.name.toCharArray().stream()).toList()",
List.class);
List<Character> expected = new ArrayList<>(18);
expected.add(Character.valueOf('b'));
expected.add(Character.valueOf('e'));
expected.add(Character.valueOf('a'));
expected.add(Character.valueOf('n'));
expected.add(Character.valueOf('0'));
expected.add(Character.valueOf('1'));
expected.add(Character.valueOf('b'));
expected.add(Character.valueOf('e'));
expected.add(Character.valueOf('a'));
expected.add(Character.valueOf('n'));
expected.add(Character.valueOf('0'));
expected.add(Character.valueOf('2'));
expected.add(Character.valueOf('b'));
expected.add(Character.valueOf('e'));
expected.add(Character.valueOf('a'));
expected.add(Character.valueOf('n'));
expected.add(Character.valueOf('0'));
expected.add(Character.valueOf('3'));
Assert.assertEquals(expected, result);
}
@Test
public void testDistinct01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"['a', 'b', 'b', 'c'].stream().distinct().toList()",
List.class);
List<String> expected = new ArrayList<>(3);
expected.add("a");
expected.add("b");
expected.add("c");
Assert.assertEquals(expected, result);
}
@Test
public void testSorted01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"['c', 'd', 'b', 'a'].stream().sorted().toList()",
List.class);
List<String> expected = new ArrayList<>(4);
expected.add("a");
expected.add("b");
expected.add("c");
expected.add("d");
Assert.assertEquals(expected, result);
}
@Test
public void testSortedLambdaExpression01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"['c', 'd', 'b', 'a'].stream().sorted((x,y)->x.compareTo(y)*-1).toList()",
List.class);
List<String> expected = new ArrayList<>(4);
expected.add("d");
expected.add("c");
expected.add("b");
expected.add("a");
Assert.assertEquals(expected, result);
}
@Test
public void testForEach01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
processor.getValue(
"beans.stream().forEach(b->b.setValLong(b.valLong + 1))",
Object.class);
Assert.assertEquals(2, bean01.getValLong());
Assert.assertEquals(3, bean02.getValLong());
Assert.assertEquals(4, bean03.getValLong());
// Restore the beans to their default state
processor.getValue(
"beans.stream().forEach(b->b.setValLong(b.valLong - 1))",
Object.class);
Assert.assertEquals(1, bean01.getValLong());
Assert.assertEquals(2, bean02.getValLong());
Assert.assertEquals(3, bean03.getValLong());
}
@Test
public void testPeek01() {
ELProcessor processor = new ELProcessor();
List<TesterBeanA> debug = new ArrayList<>();
processor.defineBean("beans", beans);
processor.defineBean("debug", debug);
Object result = processor.getValue(
"beans.stream().peek(b->debug.add(b)).toList()",
Object.class);
List<TesterBeanA> expected = new ArrayList<>(3);
expected.add(bean01);
expected.add(bean02);
expected.add(bean03);
Assert.assertEquals(expected, result);
Assert.assertEquals(expected, debug);
}
@Test
public void testLimit01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().limit(2).toList()",
Object.class);
List<TesterBeanA> expected = new ArrayList<>(2);
expected.add(bean01);
expected.add(bean02);
Assert.assertEquals(expected, result);
}
@Test
public void testSubstreamStart01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().substream(1).toList()",
Object.class);
List<TesterBeanA> expected = new ArrayList<>(2);
expected.add(bean02);
expected.add(bean03);
Assert.assertEquals(expected, result);
}
@Test
public void testSubstreamStartEnd01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().substream(1,2).toList()",
Object.class);
List<TesterBeanA> expected = new ArrayList<>(2);
expected.add(bean02);
Assert.assertEquals(expected, result);
}
@Test
public void testToArray01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().toArray()",
Object.class);
Object[] expected = new Object[3];
expected[0] = bean01;
expected[1] = bean02;
expected[2] = bean03;
Assert.assertArrayEquals(expected, (Object[]) result);
}
@Test
public void testReduceLambda01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().reduce((x,y)->x+y)",
Object.class);
Assert.assertEquals(Long.valueOf(15), ((Optional) result).get());
}
@Test(expected=ELException.class)
public void testReduceLambda02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().reduce((x,y)->x+y)",
Object.class);
((Optional) result).get();
}
@Test
public void testReduceLambdaSeed01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().reduce(10, (x,y)->x+y)",
Object.class);
Assert.assertEquals(Long.valueOf(25), result);
}
@Test
public void testMax01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().max()",
Object.class);
Assert.assertEquals(Long.valueOf(5), ((Optional) result).get());
}
@Test
public void testMax02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[5,4,3,2,1].stream().max()",
Object.class);
Assert.assertEquals(Long.valueOf(5), ((Optional) result).get());
}
@Test(expected=ELException.class)
public void testMax03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().max()",
Object.class);
((Optional) result).get();
}
@Test(expected=ELException.class)
public void testMax04() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
processor.getValue(
"beans.stream().max()",
Object.class);
}
@Test
public void testMaxLambda01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().max((x,y)->x.name.compareTo(y.name))",
Object.class);
Assert.assertEquals(bean03, ((Optional) result).get());
}
@Test
public void testMaxLambda02() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
processor.setVariable("comparison", "v->(x,y)->v(x).compareTo(v(y))");
Object result = processor.getValue(
"beans.stream().max(comparison(x->x.name))",
Object.class);
Assert.assertEquals(bean03, ((Optional) result).get());
}
@Test
public void testMin01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().min()",
Object.class);
Assert.assertEquals(Long.valueOf(1), ((Optional) result).get());
}
@Test
public void testMin02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[5,4,3,2,1].stream().min()",
Object.class);
Assert.assertEquals(Long.valueOf(1), ((Optional) result).get());
}
@Test(expected=ELException.class)
public void testMin03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().min()",
Object.class);
((Optional) result).get();
}
@Test(expected=ELException.class)
public void testMin04() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
processor.getValue(
"beans.stream().min()",
Object.class);
}
@Test
public void testMinLambda01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Object result = processor.getValue(
"beans.stream().min((x,y)->x.name.compareTo(y.name))",
Object.class);
Assert.assertEquals(bean01, ((Optional) result).get());
}
@Test
public void testAverage01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().average()",
Object.class);
Number average = (Number) ((Optional) result).get();
Assert.assertTrue("Result: " + average.toString(),
ELSupport.equals(null, Long.valueOf(3), average));
}
@Test
public void testAverage02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5,6].stream().average()",
Object.class);
Number average = (Number) ((Optional) result).get();
Assert.assertTrue("Result: " + average.toString(),
ELSupport.equals(null, Double.valueOf(3.5), average));
}
@Test(expected=ELException.class)
public void testAverage03() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().average()",
Object.class);
((Optional) result).get();
}
@Test
public void testAverage04() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().average().orElseGet(()->10)",
Object.class);
Assert.assertEquals(Long.valueOf(10), result);
}
@Test
public void testAverage05() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().average().orElseGet(()->()->10)",
Object.class);
Assert.assertEquals(Long.valueOf(10), result);
}
@Test(expected=ELException.class)
public void testAverage06() {
ELProcessor processor = new ELProcessor();
processor.getValue(
"[].stream().average().orElseGet(10)",
Object.class);
}
@Test
public void testSum01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().sum()",
Object.class);
Assert.assertTrue("Result: " + result.toString(),
ELSupport.equals(null, Long.valueOf(15), result));
}
@Test
public void testSum02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().sum()",
Object.class);
Assert.assertTrue("Result: " + result.toString(),
ELSupport.equals(null, Long.valueOf(0), result));
}
@Test
public void testCount01() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[1,2,3,4,5].stream().count()",
Object.class);
Assert.assertTrue("Result: " + result.toString(),
ELSupport.equals(null, Long.valueOf(5), result));
}
@Test
public void testCount02() {
ELProcessor processor = new ELProcessor();
Object result = processor.getValue(
"[].stream().count()",
Object.class);
Assert.assertTrue("Result: " + result.toString(),
ELSupport.equals(null, Long.valueOf(0), result));
}
@Test
public void testAnyMatch01() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().anyMatch(x->x==7)",
Object.class);
Assert.assertEquals(Boolean.FALSE, result.get());
}
@Test
public void testAnyMatch02() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().anyMatch(x->x==3)",
Object.class);
Assert.assertEquals(Boolean.TRUE, result.get());
}
@Test(expected=ELException.class)
public void testAnyMatch03() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[].stream().anyMatch(x->x==7)",
Object.class);
result.get();
}
@Test
public void testAllMatch01() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().allMatch(x->x>3)",
Object.class);
Assert.assertEquals(Boolean.FALSE, result.get());
}
@Test
public void testAllMatch02() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().allMatch(x->x>0)",
Object.class);
Assert.assertEquals(Boolean.TRUE, result.get());
}
@Test
public void testAllMatch03() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().allMatch(x->x>10)",
Object.class);
Assert.assertEquals(Boolean.FALSE, result.get());
}
@Test(expected=ELException.class)
public void testAllMatch04() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[].stream().allMatch(x->x==7)",
Object.class);
result.get();
}
@Test
public void testNoneMatch01() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().allMatch(x->x>3)",
Object.class);
Assert.assertEquals(Boolean.FALSE, result.get());
}
@Test
public void testNoneMatch02() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().noneMatch(x->x>0)",
Object.class);
Assert.assertEquals(Boolean.FALSE, result.get());
}
@Test
public void testNoneMatch03() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[1,2,3,4,5].stream().noneMatch(x->x>10)",
Object.class);
Assert.assertEquals(Boolean.TRUE, result.get());
}
@Test(expected=ELException.class)
public void testNoneMatch04() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[].stream().noneMatch(x->x==7)",
Object.class);
result.get();
}
@Test
public void testFindFirst01() {
ELProcessor processor = new ELProcessor();
processor.defineBean("beans", beans);
Optional result = (Optional) processor.getValue(
"beans.stream().findFirst()",
Object.class);
Assert.assertEquals(bean01, result.get());
}
@Test(expected=ELException.class)
public void testFindFirst02() {
ELProcessor processor = new ELProcessor();
Optional result = (Optional) processor.getValue(
"[].stream().findFirst()",
Object.class);
result.get();
}
}

View File

@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.util;
import javax.el.MethodNotFoundException;
import org.junit.Test;
public class TestReflectionUtil {
private static final Tester BASE = new Tester();
/*
* Expect failure as it is not possible to identify which method named
* "testA()" is intended.
*/
@Test(expected=MethodNotFoundException.class)
public void testBug54370a() {
ReflectionUtil.getMethod(null, BASE, "testA",
new Class[] {null, String.class},
new Object[] {null, ""});
}
/*
* Expect failure as it is not possible to identify which method named
* "testB()" is intended. Note: In EL null can always be coerced to a valid
* value for a primitive.
*/
@Test(expected=MethodNotFoundException.class)
public void testBug54370b() {
ReflectionUtil.getMethod(null, BASE, "testB",
new Class[] {null, String.class},
new Object[] {null, ""});
}
@Test
public void testBug54370c() {
ReflectionUtil.getMethod(null, BASE, "testC",
new Class[] {null},
new Object[] {null});
}
@Test
public void testBug54370d() {
ReflectionUtil.getMethod(null, BASE, "testD",
new Class[] {null},
new Object[] {null});
}
}

View File

@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.el.util;
import java.io.InputStream;
public class Tester {
@SuppressWarnings("unused")
public void testA(InputStream param1, String param2) {
// NO-OP
}
@SuppressWarnings("unused")
public void testA(Long param1, String param2) {
// NO-OP
}
@SuppressWarnings("unused")
public void testB(InputStream param1, String param2) {
// NO-OP
}
@SuppressWarnings("unused")
public void testB(long param1, String param2) {
// NO-OP
}
@SuppressWarnings("unused")
public void testC(long param1) {
// NO-OP
}
@SuppressWarnings("unused")
public void testD(String param1) {
// NO-OP
}
}