Skip to content

Commit

Permalink
WW-5233 Copies Tiles EL related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Oct 2, 2022
1 parent 9b4bc63 commit 044021c
Show file tree
Hide file tree
Showing 6 changed files with 1,056 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* 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.tiles.el;

import com.sun.el.ExpressionFactoryImpl;
import junit.framework.TestCase;
import org.apache.tiles.api.Attribute;
import org.apache.tiles.api.Expression;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.Request;
import org.easymock.EasyMock;

import javax.el.ArrayELResolver;
import javax.el.BeanELResolver;
import javax.el.CompositeELResolver;
import javax.el.ELResolver;
import javax.el.ListELResolver;
import javax.el.MapELResolver;
import javax.el.ResourceBundleELResolver;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* Tests {@link ELAttributeEvaluator}.
*/
public class ELAttributeEvaluatorTest extends TestCase {

/**
* The evaluator to test.
*/
private ELAttributeEvaluator evaluator;

/**
* The request object to use.
*/
private Request request;

/** {@inheritDoc} */
@Override
protected void setUp() throws Exception {
super.setUp();
evaluator = new ELAttributeEvaluator();
Map<String, Object> requestScope = new HashMap<>();
Map<String, Object> sessionScope = new HashMap<>();
Map<String, Object> applicationScope = new HashMap<>();
requestScope.put("object1", "value");
sessionScope.put("object2", 1);
applicationScope.put("object3", 2.0F);
requestScope.put("paulaBean", new PaulaBean());
request = EasyMock.createMock(Request.class);
EasyMock.expect(request.getContext("request")).andReturn(requestScope)
.anyTimes();
EasyMock.expect(request.getContext("session")).andReturn(sessionScope)
.anyTimes();
EasyMock.expect(request.getContext("application")).andReturn(
applicationScope).anyTimes();
EasyMock.expect(request.getAvailableScopes()).andReturn(
Arrays.asList("request", "session", "application")).anyTimes();
ApplicationContext applicationContext = EasyMock
.createMock(ApplicationContext.class);
EasyMock.expect(request.getApplicationContext()).andReturn(
applicationContext).anyTimes();
EasyMock.replay(request, applicationContext);

evaluator.setExpressionFactory(new ExpressionFactoryImpl());
ELResolver elResolver = new CompositeELResolver() {
{
BeanELResolver beanElResolver = new BeanELResolver(false);
add(new ScopeELResolver());
add(new TilesContextELResolver(beanElResolver));
add(new TilesContextBeanELResolver());
add(new ArrayELResolver(false));
add(new ListELResolver(false));
add(new MapELResolver(false));
add(new ResourceBundleELResolver());
add(beanElResolver);
}
};
evaluator.setResolver(elResolver);
}

/**
* Tests
* {@link ELAttributeEvaluator#evaluate(Attribute, Request)}.
*/
public void testEvaluate() {
Attribute attribute = new Attribute();
attribute.setExpressionObject(new Expression("${requestScope.object1}"));
assertEquals("The value is not correct", "value", evaluator.evaluate(
attribute, request));
attribute.setExpressionObject(new Expression("${sessionScope.object2}"));
assertEquals("The value is not correct", 1, evaluator
.evaluate(attribute, request));
attribute.setExpressionObject(new Expression("${applicationScope.object3}"));
assertEquals("The value is not correct", 2.0F, evaluator
.evaluate(attribute, request));
attribute.setExpressionObject(new Expression("${object1}"));
assertEquals("The value is not correct", "value", evaluator.evaluate(
attribute, request));
attribute.setExpressionObject(new Expression("${object2}"));
assertEquals("The value is not correct", 1, evaluator
.evaluate(attribute, request));
attribute.setExpressionObject(new Expression("${object3}"));
assertEquals("The value is not correct", 2.0F, evaluator
.evaluate(attribute, request));
attribute.setExpressionObject(new Expression("${paulaBean.paula}"));
assertEquals("The value is not correct", "Brillant", evaluator
.evaluate(attribute, request));
attribute.setExpressionObject(new Expression("String literal"));
assertEquals("The value is not correct", "String literal", evaluator
.evaluate(attribute, request));
attribute.setValue(2);
assertEquals("The value is not correct", 2, evaluator
.evaluate(attribute, request));
attribute.setValue("${object1}");
assertEquals("The value has been evaluated", "${object1}", evaluator
.evaluate(attribute, request));
}

/**
* Tests
* {@link ELAttributeEvaluator#evaluate(String, Request)}.
*/
public void testEvaluateString() {
String expression = "${requestScope.object1}";
assertEquals("The value is not correct", "value", evaluator.evaluate(
expression, request));
expression = "${sessionScope.object2}";
assertEquals("The value is not correct", 1, evaluator
.evaluate(expression, request));
expression = "${applicationScope.object3}";
assertEquals("The value is not correct", 2.0F, evaluator
.evaluate(expression, request));
expression = "${object1}";
assertEquals("The value is not correct", "value", evaluator.evaluate(
expression, request));
expression = "${object2}";
assertEquals("The value is not correct", 1, evaluator
.evaluate(expression, request));
expression = "${object3}";
assertEquals("The value is not correct", 2.0F, evaluator
.evaluate(expression, request));
expression = "${paulaBean.paula}";
assertEquals("The value is not correct", "Brillant", evaluator
.evaluate(expression, request));
expression = "String literal";
assertEquals("The value is not correct", expression, evaluator
.evaluate(expression, request));
}

/**
* This is The Brillant Paula Bean (sic) just like it was posted to:
* http://thedailywtf.com/Articles/The_Brillant_Paula_Bean.aspx
* I hope that there is no copyright on it.
*/
public static class PaulaBean {

/**
* Paula is brillant, really.
*/
private final String paula = "Brillant";

/**
* Returns brillant.
*
* @return "Brillant".
*/
public String getPaula() {
return paula;
}
}
}
120 changes: 120 additions & 0 deletions plugins/tiles/src/test/java/org/apache/tiles/el/ELContextImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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.tiles.el;

import org.junit.Before;
import org.junit.Test;

import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.ValueExpression;
import javax.el.VariableMapper;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* Tests {@link ELContextImpl}.
*/
public class ELContextImplTest {

/**
* The EL context to test.
*/
private ELContextImpl context;

/**
* The EL resolver.
*/
private ELResolver resolver;

/**
* Sets up the test.
*/
@Before
public void setUp() {
resolver = createMock(ELResolver.class);
context = new ELContextImpl(resolver);
}

/**
* Test method for {@link ELContextImpl#getELResolver()}.
*/
@Test
public void testGetELResolver() {
replay(resolver);
assertEquals(resolver, context.getELResolver());
verify(resolver);
}

/**
* Test method for {@link ELContextImpl#setFunctionMapper(FunctionMapper)}.
*/
@Test
public void testSetFunctionMapper() {
FunctionMapper functionMapper = createMock(FunctionMapper.class);

replay(resolver, functionMapper);
context.setFunctionMapper(functionMapper);
assertEquals(functionMapper, context.getFunctionMapper());
verify(resolver, functionMapper);
}

/**
* Test method for {@link ELContextImpl#setVariableMapper(VariableMapper)}.
*/
@Test
public void testSetVariableMapper() {
VariableMapper variableMapper = createMock(VariableMapper.class);

replay(resolver, variableMapper);
context.setVariableMapper(variableMapper);
assertEquals(variableMapper, context.getVariableMapper());
verify(resolver, variableMapper);
}

/**
* Tests {@link ELContextImpl#getFunctionMapper()}.
*/
@Test
public void testNullFunctionMapper() {
replay(resolver);
FunctionMapper functionMapper = context.getFunctionMapper();
assertNull(functionMapper.resolveFunction("whatever", "it_IT"));
verify(resolver);
}

/**
* Tests {@link ELContextImpl#getVariableMapper()}.
*/
@Test
public void testVariableMapperImpl() {
ValueExpression expression = createMock(ValueExpression.class);

replay(resolver, expression);
VariableMapper variableMapper = context.getVariableMapper();
assertNull(variableMapper.resolveVariable("whatever"));
variableMapper.setVariable("var", expression);
assertEquals(expression, variableMapper.resolveVariable("var"));
verify(resolver, expression);
}
}
Original file line number Diff line number Diff line change
@@ -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.tiles.el;

import org.apache.tiles.request.ApplicationContext;
import org.junit.Test;

import javax.el.ExpressionFactory;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspApplicationContext;
import javax.servlet.jsp.JspFactory;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;

/**
* Tests {@link JspExpressionFactoryFactory}.
*/
public class JspExpressionFactoryFactoryTest {

/**
* Test method for {@link JspExpressionFactoryFactory#getExpressionFactory()}.
*/
@Test
public void testGetExpressionFactory() {
ApplicationContext applicationContext = createMock(ApplicationContext.class);
ServletContext servletContext = createMock(ServletContext.class);
JspFactory jspFactory = createMock(JspFactory.class);
JspApplicationContext jspApplicationContext = createMock(JspApplicationContext.class);
ExpressionFactory expressionFactory = createMock(ExpressionFactory.class);

expect(applicationContext.getContext()).andReturn(servletContext);
expect(jspFactory.getJspApplicationContext(servletContext)).andReturn(jspApplicationContext);
expect(jspApplicationContext.getExpressionFactory()).andReturn(expressionFactory);

replay(applicationContext, servletContext, jspFactory,
jspApplicationContext, expressionFactory);
JspFactory.setDefaultFactory(jspFactory);
JspExpressionFactoryFactory factory = new JspExpressionFactoryFactory();
factory.setApplicationContext(applicationContext);
assertEquals(expressionFactory, factory.getExpressionFactory());
verify(applicationContext, servletContext, jspFactory,
jspApplicationContext, expressionFactory);
}

/**
* Test method for {@link JspExpressionFactoryFactory#getExpressionFactory()}.
*/
@Test(expected = IllegalArgumentException.class)
public void testSetApplicationContextIllegal() {
ApplicationContext applicationContext = createMock(ApplicationContext.class);
Integer servletContext = 1;

expect(applicationContext.getContext()).andReturn(servletContext);

replay(applicationContext);
try {
JspExpressionFactoryFactory factory = new JspExpressionFactoryFactory();
factory.setApplicationContext(applicationContext);
} finally {
verify(applicationContext);
}
}

}

0 comments on commit 044021c

Please sign in to comment.