From 044021ca1a629c66f10d4ffd7e63ac00fb257c78 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sun, 2 Oct 2022 13:16:59 +0200 Subject: [PATCH] WW-5233 Copies Tiles EL related tests --- .../tiles/el/ELAttributeEvaluatorTest.java | 189 +++++++++++ .../apache/tiles/el/ELContextImplTest.java | 120 +++++++ .../el/JspExpressionFactoryFactoryTest.java | 84 +++++ .../apache/tiles/el/ScopeELResolverTest.java | 175 ++++++++++ .../el/TilesContextBeanELResolverTest.java | 299 ++++++++++++++++++ .../tiles/el/TilesContextELResolverTest.java | 189 +++++++++++ 6 files changed, 1056 insertions(+) create mode 100644 plugins/tiles/src/test/java/org/apache/tiles/el/ELAttributeEvaluatorTest.java create mode 100644 plugins/tiles/src/test/java/org/apache/tiles/el/ELContextImplTest.java create mode 100644 plugins/tiles/src/test/java/org/apache/tiles/el/JspExpressionFactoryFactoryTest.java create mode 100644 plugins/tiles/src/test/java/org/apache/tiles/el/ScopeELResolverTest.java create mode 100644 plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextBeanELResolverTest.java create mode 100644 plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextELResolverTest.java diff --git a/plugins/tiles/src/test/java/org/apache/tiles/el/ELAttributeEvaluatorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/el/ELAttributeEvaluatorTest.java new file mode 100644 index 0000000000..8c1c794bab --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/el/ELAttributeEvaluatorTest.java @@ -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 requestScope = new HashMap<>(); + Map sessionScope = new HashMap<>(); + Map 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; + } + } +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/el/ELContextImplTest.java b/plugins/tiles/src/test/java/org/apache/tiles/el/ELContextImplTest.java new file mode 100644 index 0000000000..b9a835318f --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/el/ELContextImplTest.java @@ -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); + } +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/el/JspExpressionFactoryFactoryTest.java b/plugins/tiles/src/test/java/org/apache/tiles/el/JspExpressionFactoryFactoryTest.java new file mode 100644 index 0000000000..b7092e5304 --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/el/JspExpressionFactoryFactoryTest.java @@ -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); + } + } + +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/el/ScopeELResolverTest.java b/plugins/tiles/src/test/java/org/apache/tiles/el/ScopeELResolverTest.java new file mode 100644 index 0000000000..bdd8d799e5 --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/el/ScopeELResolverTest.java @@ -0,0 +1,175 @@ +/* + * 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.apache.tiles.request.Request; +import org.junit.Before; +import org.junit.Test; + +import javax.el.ELContext; +import java.beans.FeatureDescriptor; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +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; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests {@link ScopeELResolver}. + */ +public class ScopeELResolverTest { + + /** + * The resolver to test. + */ + private ScopeELResolver resolver; + + /** + * {@inheritDoc} + */ + @Before + public void setUp() { + resolver = new ScopeELResolver(); + } + + /** + * Tests {@link ScopeELResolver#getCommonPropertyType(ELContext, Object)}. + */ + @Test + public void testGetCommonPropertyType() { + ELContext elContext = createMock(ELContext.class); + + replay(elContext); + assertNull(resolver.getCommonPropertyType(elContext, 1)); + assertEquals(Map.class, resolver.getCommonPropertyType(elContext, null)); + verify(elContext); + } + + /** + * Tests {@link ScopeELResolver#getFeatureDescriptors(ELContext, Object)}. + */ + @Test + public void testGetFeatureDescriptors() { + ELContext elContext = createMock(ELContext.class); + Request request = createMock(Request.class); + + expect(elContext.getContext(Request.class)).andReturn(request); + expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")); + + replay(elContext, request); + assertFalse(resolver.getFeatureDescriptors(elContext, 1).hasNext()); + Iterator descriptors = resolver.getFeatureDescriptors(elContext, null); + FeatureDescriptor descriptor = descriptors.next(); + assertEquals("oneScope", descriptor.getName()); + descriptor = descriptors.next(); + assertEquals("twoScope", descriptor.getName()); + assertFalse(descriptors.hasNext()); + verify(elContext, request); + } + + /** + * Test method for + * {@link ScopeELResolver#getType(ELContext, Object, Object)}. + */ + @Test + public void testGetType() { + Request request = createMock(Request.class); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + ELContext context = new ELContextImpl(resolver); + replay(request, applicationContext); + context.putContext(Request.class, request); + context.putContext(ApplicationContext.class, applicationContext); + assertNull(resolver.getType(context, 1, "whatever")); + assertEquals("The requestScope object is not a map.", Map.class, + resolver.getType(context, null, "requestScope")); + assertEquals("The sessionScope object is not a map.", Map.class, + resolver.getType(context, null, "sessionScope")); + assertEquals("The applicationScope object is not a map.", Map.class, + resolver.getType(context, null, "applicationScope")); + } + + /** + * Test method for + * {@link ScopeELResolver#getValue(ELContext, Object, Object)}. + */ + @Test + public void testGetValue() { + Map requestScope = new HashMap<>(); + requestScope.put("objectKey", "objectValue"); + Map sessionScope = new HashMap<>(); + sessionScope.put("sessionObjectKey", "sessionObjectValue"); + Map applicationScope = new HashMap<>(); + applicationScope.put("applicationObjectKey", "applicationObjectValue"); + Request request = createMock(Request.class); + expect(request.getContext("request")).andReturn(requestScope); + expect(request.getContext("session")).andReturn(sessionScope); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + expect(request.getContext("application")).andReturn(applicationScope); + ELContext context = new ELContextImpl(resolver); + replay(request, applicationContext); + context.putContext(Request.class, request); + context.putContext(ApplicationContext.class, applicationContext); + assertNull(resolver.getValue(context, 1, "whatever")); + assertEquals("The requestScope map does not correspond", requestScope, + resolver.getValue(context, null, "requestScope")); + assertEquals("The sessionScope map does not correspond", sessionScope, + resolver.getValue(context, null, "sessionScope")); + assertEquals("The applicationScope map does not correspond", + applicationScope, resolver.getValue(context, null, + "applicationScope")); + } + + /** + * Tests {@link ScopeELResolver#isReadOnly(ELContext, Object, Object)}. + */ + @Test + public void testIsReadOnly() { + ELContext elContext = createMock(ELContext.class); + + replay(elContext); + assertTrue(resolver.isReadOnly(elContext, null, "whatever")); + verify(elContext); + } + + /** + * Tests {@link ScopeELResolver#isReadOnly(ELContext, Object, Object)}. + */ + @Test(expected = NullPointerException.class) + public void testIsReadOnlyNPE() { + resolver.isReadOnly(null, null, "whatever"); + } + + /** + * Tests {@link ScopeELResolver#setValue(ELContext, Object, Object, Object)}. + */ + @Test + public void testSetValue() { + // Just to complete code coverage! + resolver.setValue(null, null, null, null); + } +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextBeanELResolverTest.java b/plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextBeanELResolverTest.java new file mode 100644 index 0000000000..5a1214ec2d --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextBeanELResolverTest.java @@ -0,0 +1,299 @@ +/* + * 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.apache.tiles.request.Request; +import org.junit.Before; +import org.junit.Test; + +import javax.el.ELContext; +import java.beans.FeatureDescriptor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +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; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests {@link TilesContextBeanELResolver}. + */ +public class TilesContextBeanELResolverTest { + + /** + * The resolver to test. + */ + private TilesContextBeanELResolver resolver; + + /** + * Sets up the test. + */ + @Before + public void setUp() { + resolver = new TilesContextBeanELResolver(); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#getCommonPropertyType(ELContext, Object)}. + */ + @Test + public void testGetCommonPropertyType() { + Class clazz = resolver.getCommonPropertyType(null, null); + assertEquals("The class is not correct", String.class, clazz); + clazz = resolver.getCommonPropertyType(null, "Base object"); + assertNull("The class for non root objects must be null", clazz); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#getFeatureDescriptors(ELContext, Object)}. + */ + @Test + public void testGetFeatureDescriptors() { + Map requestScope = new HashMap<>(); + Map sessionScope = new HashMap<>(); + Map applicationScope = new HashMap<>(); + requestScope.put("object1", "value"); + sessionScope.put("object2", 1); + applicationScope.put("object3", 2.0F); + Request request = createMock(Request.class); + expect(request.getContext("request")).andReturn(requestScope) + .anyTimes(); + expect(request.getContext("session")).andReturn(sessionScope) + .anyTimes(); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + expect(request.getContext("application")).andReturn( + applicationScope).anyTimes(); + expect(request.getAvailableScopes()).andReturn( + Arrays.asList("request", "session", "application")) + .anyTimes(); + replay(request, applicationContext); + + ELContext context = new ELContextImpl(resolver); + context.putContext(Request.class, request); + context.putContext(ApplicationContext.class, applicationContext); + + List expected = new ArrayList<>(); + resolver.collectBeanInfo(requestScope, expected); + resolver.collectBeanInfo(sessionScope, expected); + resolver.collectBeanInfo(applicationScope, expected); + Iterator featureIt = resolver.getFeatureDescriptors( + context, null); + Iterator expectedIt = expected.iterator(); + while (featureIt.hasNext() && expectedIt.hasNext()) { + FeatureDescriptor expectedDescriptor = expectedIt.next(); + FeatureDescriptor descriptor = featureIt.next(); + assertEquals("The feature is not the same", expectedDescriptor + .getDisplayName(), descriptor.getDisplayName()); + assertEquals("The feature is not the same", expectedDescriptor + .getName(), descriptor.getName()); + assertEquals("The feature is not the same", expectedDescriptor + .getShortDescription(), descriptor.getShortDescription()); + assertEquals("The feature is not the same", expectedDescriptor + .getValue("type"), descriptor.getValue("type")); + assertEquals("The feature is not the same", expectedDescriptor + .getValue("resolvableAtDesignTime"), descriptor + .getValue("resolvableAtDesignTime")); + assertEquals("The feature is not the same", expectedDescriptor + .isExpert(), descriptor.isExpert()); + assertEquals("The feature is not the same", expectedDescriptor + .isHidden(), descriptor.isHidden()); + assertEquals("The feature is not the same", expectedDescriptor + .isPreferred(), descriptor.isPreferred()); + } + assertTrue("The feature descriptors are not of the same size", + !featureIt.hasNext() && !expectedIt.hasNext()); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#getType(ELContext, Object, Object)}. + */ + @Test + public void testGetType() { + Map requestScope = new HashMap<>(); + Map sessionScope = new HashMap<>(); + Map applicationScope = new HashMap<>(); + requestScope.put("object1", "value"); + sessionScope.put("object2", 1); + applicationScope.put("object3", 2.0F); + Request request = createMock(Request.class); + expect(request.getContext("request")).andReturn(requestScope) + .anyTimes(); + expect(request.getContext("session")).andReturn(sessionScope) + .anyTimes(); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + expect(request.getContext("application")).andReturn( + applicationScope).anyTimes(); + expect(request.getAvailableScopes()).andReturn( + Arrays.asList("request", "session", "application")) + .anyTimes(); + replay(request, applicationContext); + + ELContext context = new ELContextImpl(resolver); + context.putContext(Request.class, request); + context.putContext(ApplicationContext.class, applicationContext); + + assertEquals("The type is not correct", String.class, resolver.getType( + context, null, "object1")); + assertEquals("The type is not correct", Integer.class, resolver.getType( + context, null, "object2")); + assertEquals("The type is not correct", Float.class, resolver.getType( + context, null, "object3")); + assertNull(resolver.getType(context, 1, "whatever")); + assertNull(resolver.getType(context, null, "object4")); + verify(request, applicationContext); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#getValue(ELContext, Object, Object)}. + */ + @Test + public void testGetValue() { + Map requestScope = new HashMap<>(); + Map sessionScope = new HashMap<>(); + Map applicationScope = new HashMap<>(); + requestScope.put("object1", "value"); + sessionScope.put("object2", 1); + applicationScope.put("object3", 2.0F); + Request request = createMock(Request.class); + expect(request.getContext("request")).andReturn(requestScope) + .anyTimes(); + expect(request.getContext("session")).andReturn(sessionScope) + .anyTimes(); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + expect(request.getContext("application")).andReturn( + applicationScope).anyTimes(); + expect(request.getAvailableScopes()).andReturn( + Arrays.asList("request", "session", "application")) + .anyTimes(); + replay(request, applicationContext); + + ELContext context = new ELContextImpl(resolver); + context.putContext(Request.class, request); + context.putContext(ApplicationContext.class, applicationContext); + + assertEquals("The value is not correct", "value", resolver.getValue( + context, null, "object1")); + assertEquals("The value is not correct", 1, resolver + .getValue(context, null, "object2")); + assertEquals("The value is not correct", 2.0F, resolver + .getValue(context, null, "object3")); + assertNull(resolver.getValue(context, 1, "whatever")); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#isReadOnly(ELContext, Object, Object)}. + */ + @Test + public void testIsReadOnlyELContextObjectObject() { + ELContext context = new ELContextImpl(resolver); + assertTrue("The value is not read only", resolver.isReadOnly(context, + null, null)); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#isReadOnly(ELContext, Object, Object)}. + */ + @Test(expected = NullPointerException.class) + public void testIsReadOnlyNPE() { + resolver.isReadOnly(null, null, null); + } + + /** + * Tests {@link TilesContextBeanELResolver#setValue(ELContext, Object, Object, Object)}. + */ + @Test + public void testSetValue() { + // Just to complete code coverage! + resolver.setValue(null, null, null, null); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#findObjectByProperty(ELContext, Object)}. + */ + @Test + public void testFindObjectByProperty() { + Map requestScope = new HashMap<>(); + Map sessionScope = new HashMap<>(); + Map applicationScope = new HashMap<>(); + requestScope.put("object1", "value"); + sessionScope.put("object2", 1); + applicationScope.put("object3", 2.0F); + Request request = createMock(Request.class); + expect(request.getContext("request")).andReturn(requestScope) + .anyTimes(); + expect(request.getContext("session")).andReturn(sessionScope) + .anyTimes(); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + expect(request.getContext("application")).andReturn( + applicationScope).anyTimes(); + expect(request.getAvailableScopes()).andReturn( + Arrays.asList("request", "session", "application")) + .anyTimes(); + replay(request, applicationContext); + + ELContext context = new ELContextImpl(resolver); + context.putContext(Request.class, request); + context.putContext(ApplicationContext.class, applicationContext); + + assertEquals("The value is not correct", "value", resolver + .findObjectByProperty(context, "object1")); + assertEquals("The value is not correct", 1, resolver + .findObjectByProperty(context, "object2")); + assertEquals("The value is not correct", 2.0F, resolver + .findObjectByProperty(context, "object3")); + } + + /** + * Test method for + * {@link TilesContextBeanELResolver#getObject(Map, String)}. + */ + @Test + public void testGetObject() { + Map map = new HashMap<>(); + map.put("object1", "value"); + assertEquals("The value is not correct", "value", resolver.getObject( + map, "object1")); + assertNull("The value is not null", resolver.getObject(map, "object2")); + assertNull("The value is not null", resolver.getObject(null, "object1")); + } + + /** + * Tests {@link TilesContextBeanELResolver#collectBeanInfo(Map, List)}. + */ + @Test + public void testCollectBeanInfoEmpty() { + resolver.collectBeanInfo(null, null); + } +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextELResolverTest.java b/plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextELResolverTest.java new file mode 100644 index 0000000000..c3685aaf9d --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/el/TilesContextELResolverTest.java @@ -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 org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.Request; +import org.apache.tiles.request.reflect.ClassUtil; +import org.junit.Before; +import org.junit.Test; + +import javax.el.ELContext; +import javax.el.ELResolver; +import java.beans.FeatureDescriptor; +import java.beans.PropertyDescriptor; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests {@link TilesContextELResolver}. + */ +public class TilesContextELResolverTest { + + /** + * The bean resolver. + */ + private ELResolver beanElResolver; + + /** + * The resolver to test. + */ + private TilesContextELResolver resolver; + + /** + * Sets up the test. + */ + @Before + public void setUp() { + beanElResolver = createMock(ELResolver.class); + resolver = new TilesContextELResolver(beanElResolver); + } + + /** + * Test method for + * {@link TilesContextELResolver#getCommonPropertyType(ELContext, Object)}. + */ + @Test + public void testGetCommonPropertyTypeELContextObject() { + replay(beanElResolver); + Class clazz = resolver.getCommonPropertyType(null, null); + assertEquals("The class is not correct", String.class, clazz); + clazz = resolver.getCommonPropertyType(null, "Base object"); + assertNull("The class for non root objects must be null", clazz); + verify(beanElResolver); + } + + /** + * Test method for + * {@link TilesContextELResolver#getFeatureDescriptors(ELContext, Object)}. + */ + @Test + public void testGetFeatureDescriptorsELContextObject() { + replay(beanElResolver); + assertNull(resolver.getFeatureDescriptors(null, 1)); + Map expected = new LinkedHashMap<>(); + ClassUtil.collectBeanInfo(Request.class, expected); + ClassUtil.collectBeanInfo(ApplicationContext.class, expected); + Iterator featureIt = resolver.getFeatureDescriptors( + null, null); + Iterator expectedIt = expected.values().iterator(); + while (featureIt.hasNext() && expectedIt.hasNext()) { + assertEquals("The feature is not the same", expectedIt.next(), + featureIt.next()); + } + assertTrue("The feature descriptors are not of the same size", + !featureIt.hasNext() && !expectedIt.hasNext()); + verify(beanElResolver); + } + + /** + * Tests {@link TilesContextBeanELResolver#getType(ELContext, Object, Object)}. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + @Test + public void testGetType() { + ELContext elContext = createMock(ELContext.class); + Request request = createMock(Request.class); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + + expect(elContext.getContext(Request.class)).andReturn(request); + expect(elContext.getContext(ApplicationContext.class)).andReturn(applicationContext); + expect(beanElResolver.getType(elContext, request, "responseCommitted")).andReturn((Class) Boolean.class); + expect(beanElResolver.getType(elContext, applicationContext, "initParams")).andReturn((Class) Map.class); + elContext.setPropertyResolved(true); + expectLastCall().times(2); + + replay(beanElResolver, elContext, request, applicationContext); + assertNull(resolver.getType(elContext, 1, "whatever")); + assertEquals(Boolean.class, resolver.getType(elContext, null, "responseCommitted")); + assertEquals(Map.class, resolver.getType(elContext, null, "initParams")); + verify(beanElResolver, elContext, request, applicationContext); + } + + /** + * Tests {@link TilesContextBeanELResolver#getValue(ELContext, Object, Object)}. + */ + @Test + public void testGetValue() { + ELContext elContext = createMock(ELContext.class); + Request request = createMock(Request.class); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + @SuppressWarnings("rawtypes") + Map map = createMock(Map.class); + + expect(elContext.getContext(Request.class)).andReturn(request); + expect(elContext.getContext(ApplicationContext.class)).andReturn(applicationContext); + expect(beanElResolver.getValue(elContext, request, "responseCommitted")).andReturn(true); + expect(beanElResolver.getValue(elContext, applicationContext, "initParams")).andReturn(map); + elContext.setPropertyResolved(true); + expectLastCall().times(2); + + replay(beanElResolver, elContext, request, applicationContext, map); + assertNull(resolver.getValue(elContext, 1, "whatever")); + assertEquals(true, resolver.getValue(elContext, null, "responseCommitted")); + assertEquals(map, resolver.getValue(elContext, null, "initParams")); + verify(beanElResolver, elContext, request, applicationContext, map); + } + + /** + * Test method for + * {@link TilesContextELResolver#isReadOnly(ELContext, Object, Object)}. + */ + @Test + public void testIsReadOnly() { + replay(beanElResolver); + ELContext context = new ELContextImpl(resolver); + assertTrue("The value is not read only", resolver.isReadOnly(context, + null, null)); + verify(beanElResolver); + } + + /** + * Test method for + * {@link TilesContextELResolver#isReadOnly(ELContext, Object, Object)}. + */ + @Test(expected = NullPointerException.class) + public void testIsReadOnlyNPE() { + replay(beanElResolver); + try { + resolver.isReadOnly(null, null, null); + } finally { + verify(beanElResolver); + } + } + + /** + * Tests {@link TilesContextELResolver#setValue(ELContext, Object, Object, Object)}. + */ + @Test + public void testSetValue() { + // Just to complete code coverage! + resolver.setValue(null, null, null, null); + } +}