This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch WW-5233-tiles in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/WW-5233-tiles by this push: new 3207bfd0e WW-5233 Copies Tiles OGNL related tests 3207bfd0e is described below commit 3207bfd0ed44139c4de81d8daccb53a60ca84582 Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Sun Oct 2 13:27:34 2022 +0200 WW-5233 Copies Tiles OGNL related tests --- .../tiles/ognl/AnyScopePropertyAccessorTest.java | 152 ++++++++++++++ .../tiles/ognl/DelegatePropertyAccessorTest.java | 111 ++++++++++ .../NestedObjectDelegatePropertyAccessorTest.java | 106 ++++++++++ .../tiles/ognl/OGNLAttributeEvaluatorTest.java | 229 +++++++++++++++++++++ .../tiles/ognl/ScopePropertyAccessorTest.java | 91 ++++++++ ...pplicationContextNestedObjectExtractorTest.java | 51 +++++ ...ContextPropertyAccessorDelegateFactoryTest.java | 189 +++++++++++++++++ 7 files changed, 929 insertions(+) diff --git a/plugins/tiles/src/test/java/org/apache/tiles/ognl/AnyScopePropertyAccessorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/AnyScopePropertyAccessorTest.java new file mode 100644 index 000000000..c7f3465cc --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/AnyScopePropertyAccessorTest.java @@ -0,0 +1,152 @@ +/* + * 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.ognl; + +import org.apache.tiles.request.Request; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +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; + +/** + * Tests {@link AnyScopePropertyAccessor}. + */ +public class AnyScopePropertyAccessorTest { + + /** + * The accessor to test. + */ + private AnyScopePropertyAccessor accessor; + + /** + * Sets up the test. + */ + @Before + public void setUp() { + accessor = new AnyScopePropertyAccessor(); + } + + /** + * Test method for {@link AnyScopePropertyAccessor#getProperty(Map, Object, Object)}. + */ + @Test + public void testGetProperty() { + Request request = createMock(Request.class); + Map<String, Object> oneScope = createMock(Map.class); + Map<String, Object> twoScope = createMock(Map.class); + + expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes(); + expect(request.getContext("one")).andReturn(oneScope).anyTimes(); + expect(request.getContext("two")).andReturn(twoScope).anyTimes(); + expect(oneScope.containsKey("name1")).andReturn(true); + expect(oneScope.get("name1")).andReturn("value1"); + expect(oneScope.containsKey("name2")).andReturn(false); + expect(oneScope.containsKey("name3")).andReturn(false); + expect(twoScope.containsKey("name2")).andReturn(true); + expect(twoScope.get("name2")).andReturn("value2"); + expect(twoScope.containsKey("name3")).andReturn(false); + + replay(request, oneScope, twoScope); + assertEquals("value1", accessor.getProperty(null, request, "name1")); + assertEquals("value2", accessor.getProperty(null, request, "name2")); + assertNull(accessor.getProperty(null, request, "name3")); + verify(request, oneScope, twoScope); + } + + @Test + public void testGetSourceAccessor() { + Request request = createMock(Request.class); + Map<String, Object> oneScope = createMock(Map.class); + Map<String, Object> twoScope = createMock(Map.class); + + expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes(); + expect(request.getContext("one")).andReturn(oneScope).anyTimes(); + expect(request.getContext("two")).andReturn(twoScope).anyTimes(); + expect(oneScope.containsKey("name1")).andReturn(true); + expect(oneScope.containsKey("name2")).andReturn(false); + expect(oneScope.containsKey("name3")).andReturn(false); + expect(twoScope.containsKey("name2")).andReturn(true); + expect(twoScope.containsKey("name3")).andReturn(false); + + replay(request, oneScope, twoScope); + assertEquals(".getContext(\"one\").get(index)", accessor.getSourceAccessor(null, request, "name1")); + assertEquals(".getContext(\"two\").get(index)", accessor.getSourceAccessor(null, request, "name2")); + assertNull(accessor.getSourceAccessor(null, request, "name3")); + verify(request, oneScope, twoScope); + } + + @Test + public void testGetSourceSetter() { + Request request = createMock(Request.class); + Map<String, Object> oneScope = createMock(Map.class); + Map<String, Object> twoScope = createMock(Map.class); + + expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes(); + expect(request.getContext("one")).andReturn(oneScope).anyTimes(); + expect(request.getContext("two")).andReturn(twoScope).anyTimes(); + expect(oneScope.containsKey("name1")).andReturn(true); + expect(oneScope.containsKey("name2")).andReturn(false); + expect(oneScope.containsKey("name3")).andReturn(false); + expect(twoScope.containsKey("name2")).andReturn(true); + expect(twoScope.containsKey("name3")).andReturn(false); + + replay(request, oneScope, twoScope); + assertEquals(".getContext(\"one\").put(index, target)", accessor.getSourceSetter(null, request, "name1")); + assertEquals(".getContext(\"two\").put(index, target)", accessor.getSourceSetter(null, request, "name2")); + assertEquals(".getContext(\"one\").put(index, target)", accessor.getSourceSetter(null, request, "name3")); + verify(request, oneScope, twoScope); + } + + /** + * Test method for {@link AnyScopePropertyAccessor#setProperty(Map, Object, Object, Object)}. + */ + @Test + public void testSetProperty() { + Request request = createMock(Request.class); + Map<String, Object> oneScope = createMock(Map.class); + Map<String, Object> twoScope = createMock(Map.class); + + expect(request.getAvailableScopes()).andReturn(Arrays.asList("one", "two")).anyTimes(); + expect(request.getContext("one")).andReturn(oneScope).anyTimes(); + expect(request.getContext("two")).andReturn(twoScope).anyTimes(); + expect(oneScope.containsKey("name1")).andReturn(true); + expect(oneScope.put("name1", "otherValue1")).andReturn("value1"); + expect(oneScope.containsKey("name2")).andReturn(false); + expect(oneScope.containsKey("name3")).andReturn(false); + expect(twoScope.containsKey("name2")).andReturn(true); + expect(twoScope.put("name2", "otherValue2")).andReturn("value2"); + expect(twoScope.containsKey("name3")).andReturn(false); + expect(oneScope.put("name3", "otherValue3")).andReturn(null); + + replay(request, oneScope, twoScope); + accessor.setProperty(null, request, "name1", "otherValue1"); + accessor.setProperty(null, request, "name2", "otherValue2"); + accessor.setProperty(null, request, "name3", "otherValue3"); + verify(request, oneScope, twoScope); + } + +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/ognl/DelegatePropertyAccessorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/DelegatePropertyAccessorTest.java new file mode 100644 index 000000000..fc5470d74 --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/DelegatePropertyAccessorTest.java @@ -0,0 +1,111 @@ +/* + * 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.ognl; + +import ognl.OgnlContext; +import ognl.OgnlException; +import ognl.PropertyAccessor; +import org.junit.Test; + +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; + +/** + * Tests {@link DelegatePropertyAccessor}. + */ +public class DelegatePropertyAccessorTest { + + /** + * Test method for {@link DelegatePropertyAccessor#getProperty(Map, Object, Object)}. + * + * @throws OgnlException If something goes wrong. + */ + @Test + public void testGetProperty() throws OgnlException { + PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class); + PropertyAccessor mockAccessor = createMock(PropertyAccessor.class); + Map<String, Object> context = createMock(Map.class); + expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor); + expect(mockAccessor.getProperty(context, 1, "property")).andReturn("value"); + + replay(factory, mockAccessor, context); + PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory); + assertEquals("value", accessor.getProperty(context, 1, "property")); + verify(factory, mockAccessor, context); + } + + /** + * Test method for {@link DelegatePropertyAccessor#setProperty(Map, Object, Object, Object)}. + * + * @throws OgnlException If something goes wrong. + */ + @Test + public void testSetProperty() throws OgnlException { + PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class); + PropertyAccessor mockAccessor = createMock(PropertyAccessor.class); + Map<String, Object> context = createMock(Map.class); + expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor); + mockAccessor.setProperty(context, 1, "property", "value"); + + replay(factory, mockAccessor, context); + PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory); + accessor.setProperty(context, 1, "property", "value"); + verify(factory, mockAccessor, context); + } + + /** + * Test method for {@link DelegatePropertyAccessor#getSourceAccessor(OgnlContext, Object, Object)}. + */ + @Test + public void testGetSourceAccessor() { + PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class); + PropertyAccessor mockAccessor = createMock(PropertyAccessor.class); + OgnlContext context = createMock(OgnlContext.class); + expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor); + expect(mockAccessor.getSourceAccessor(context, 1, "property")).andReturn("method"); + + replay(factory, mockAccessor, context); + PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory); + assertEquals("method", accessor.getSourceAccessor(context, 1, "property")); + verify(factory, mockAccessor, context); + } + + /** + * Test method for {@link DelegatePropertyAccessor#getSourceSetter(OgnlContext, Object, Object)}. + */ + @Test + public void testGetSourceSetter() { + PropertyAccessorDelegateFactory<Integer> factory = createMock(PropertyAccessorDelegateFactory.class); + PropertyAccessor mockAccessor = createMock(PropertyAccessor.class); + OgnlContext context = createMock(OgnlContext.class); + expect(factory.getPropertyAccessor("property", 1)).andReturn(mockAccessor); + expect(mockAccessor.getSourceSetter(context, 1, "property")).andReturn("method"); + + replay(factory, mockAccessor, context); + PropertyAccessor accessor = new DelegatePropertyAccessor<>(factory); + assertEquals("method", accessor.getSourceSetter(context, 1, "property")); + verify(factory, mockAccessor, context); + } +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/ognl/NestedObjectDelegatePropertyAccessorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/NestedObjectDelegatePropertyAccessorTest.java new file mode 100644 index 000000000..0425d3c1c --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/NestedObjectDelegatePropertyAccessorTest.java @@ -0,0 +1,106 @@ +/* + * 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.ognl; + +import ognl.OgnlContext; +import ognl.OgnlException; +import ognl.PropertyAccessor; +import org.junit.Test; + +import java.util.Map; + +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertEquals; + +/** + * Tests {@link NestedObjectDelegatePropertyAccessor}. + */ +public class NestedObjectDelegatePropertyAccessorTest { + + /** + * Test method for {@link NestedObjectDelegatePropertyAccessor#getProperty(Map, Object, Object)}. + * @throws OgnlException If something goes wrong. + */ + @Test + public void testGetProperty() throws OgnlException { + NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class); + PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class); + Map<String, Object> context = createMock(Map.class); + expect(propertyAccessor.getProperty(context, "nested", "property")).andReturn("value"); + expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested"); + + replay(nestedObjectExtractor, propertyAccessor, context); + PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor); + assertEquals("value", accessor.getProperty(context, 1, "property")); + verify(nestedObjectExtractor, propertyAccessor, context); + } + + /** + * Test method for {@link NestedObjectDelegatePropertyAccessor#setProperty(Map, Object, Object, Object)}. + * @throws OgnlException If something goes wrong. + */ + @Test + public void testSetProperty() throws OgnlException { + NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class); + PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class); + Map<String, Object> context = createMock(Map.class); + propertyAccessor.setProperty(context, "nested", "property", "value"); + expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested"); + + replay(nestedObjectExtractor, propertyAccessor, context); + PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor); + accessor.setProperty(context, 1, "property", "value"); + verify(nestedObjectExtractor, propertyAccessor, context); + } + + /** + * Test method for {@link NestedObjectDelegatePropertyAccessor#getSourceAccessor(OgnlContext, Object, Object)}. + */ + @Test + public void testGetSourceAccessor() { + NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class); + PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class); + OgnlContext context = createMock(OgnlContext.class); + expect(propertyAccessor.getSourceAccessor(context, "nested", "property")).andReturn("method"); + expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested"); + + replay(nestedObjectExtractor, propertyAccessor, context); + PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor); + assertEquals("method", accessor.getSourceAccessor(context, 1, "property")); + verify(nestedObjectExtractor, propertyAccessor, context); + } + + /** + * Test method for {@link NestedObjectDelegatePropertyAccessor#getSourceSetter(OgnlContext, Object, Object)}. + */ + @Test + public void testGetSourceSetter() { + NestedObjectExtractor<Integer> nestedObjectExtractor = createMock(NestedObjectExtractor.class); + PropertyAccessor propertyAccessor = createMock(PropertyAccessor.class); + OgnlContext context = createMock(OgnlContext.class); + expect(propertyAccessor.getSourceSetter(context, "nested", "property")).andReturn("method"); + expect(nestedObjectExtractor.getNestedObject(1)).andReturn("nested"); + + replay(nestedObjectExtractor, propertyAccessor, context); + PropertyAccessor accessor = new NestedObjectDelegatePropertyAccessor<>(nestedObjectExtractor, propertyAccessor); + assertEquals("method", accessor.getSourceSetter(context, 1, "property")); + verify(nestedObjectExtractor, propertyAccessor, context); + } + +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/ognl/OGNLAttributeEvaluatorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/OGNLAttributeEvaluatorTest.java new file mode 100644 index 000000000..fbfe2081b --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/OGNLAttributeEvaluatorTest.java @@ -0,0 +1,229 @@ +/* + * $Id$ + * + * 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.ognl; + +import ognl.OgnlException; +import ognl.OgnlRuntime; +import ognl.PropertyAccessor; +import org.apache.tiles.api.Attribute; +import org.apache.tiles.api.Expression; +import org.apache.tiles.core.evaluator.EvaluationException; +import org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.Request; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashMap; +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; + +/** + * Tests {@link OGNLAttributeEvaluator}. + * + * @version $Rev$ $Date$$ + */ +public class OGNLAttributeEvaluatorTest { + + /** + * The evaluator to test. + */ + private OGNLAttributeEvaluator evaluator; + + /** + * The request object to use. + */ + private Request request; + + /** + * The application context. + */ + private ApplicationContext applicationContext; + + /** + * Sets up the test. + * + * @throws OgnlException If something goes wrong. + */ + @Before + public void setUp() throws OgnlException { + PropertyAccessor objectPropertyAccessor = OgnlRuntime.getPropertyAccessor(Object.class); + PropertyAccessor applicationContextPropertyAccessor = new NestedObjectDelegatePropertyAccessor<>( + new TilesApplicationContextNestedObjectExtractor(), + objectPropertyAccessor + ); + PropertyAccessor anyScopePropertyAccessor = new AnyScopePropertyAccessor(); + PropertyAccessor scopePropertyAccessor = new ScopePropertyAccessor(); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, + applicationContextPropertyAccessor, + anyScopePropertyAccessor, + scopePropertyAccessor + ); + PropertyAccessor tilesRequestAccessor = new DelegatePropertyAccessor<>(factory); + OgnlRuntime.setPropertyAccessor(Request.class, tilesRequestAccessor); + evaluator = new OGNLAttributeEvaluator(); + 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 = createMock(Request.class); + expect(request.getContext("request")).andReturn(requestScope) + .anyTimes(); + expect(request.getContext("session")).andReturn(sessionScope) + .anyTimes(); + expect(request.getContext("application")).andReturn(applicationScope) + .anyTimes(); + expect(request.getAvailableScopes()).andReturn(Arrays.asList("request", "session", "application")) + .anyTimes(); + applicationContext = createMock(ApplicationContext.class); + expect(request.getApplicationContext()).andReturn(applicationContext) + .anyTimes(); + expect(applicationContext.getApplicationScope()).andReturn(applicationScope) + .anyTimes(); + replay(request, applicationContext); + } + + /** + * Tears down the test. + */ + @After + public void tearDown() { + verify(request, applicationContext); + } + + /** + * Tests + * {@link OGNLAttributeEvaluator#evaluate(Attribute, Request)}. + */ + @Test + 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 OGNLAttributeEvaluator#evaluate(String, Request)}. + */ + @Test + 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", "String literal", evaluator + .evaluate(expression, request)); + } + + /** + * Tests {@link OGNLAttributeEvaluator#evaluate(String, Request)}. + */ + @Test(expected = IllegalArgumentException.class) + public void testEvaluateNull() { + evaluator.evaluate((String) null, request); + } + + /** + * Tests {@link OGNLAttributeEvaluator#evaluate(String, Request)}. + */ + @Test(expected = EvaluationException.class) + public void testEvaluateOgnlException() { + evaluator.evaluate("wrong|||!!!!yes###", 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/ognl/ScopePropertyAccessorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/ScopePropertyAccessorTest.java new file mode 100644 index 000000000..86b29449c --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/ScopePropertyAccessorTest.java @@ -0,0 +1,91 @@ +/* + * 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.ognl; + +import org.apache.tiles.request.Request; +import org.junit.Before; +import org.junit.Test; + +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; + +/** + * Tests {@link ScopePropertyAccessor}. + */ +public class ScopePropertyAccessorTest { + + /** + * The accessor to test. + */ + private ScopePropertyAccessor accessor; + + /** + * Sets up the test. + */ + @Before + public void setUp() { + accessor = new ScopePropertyAccessor(); + } + + /** + * Test method for {@link ScopePropertyAccessor#getProperty(Map, Object, Object)}. + */ + @Test + public void testGetProperty() { + Request request = createMock(Request.class); + Map<String, Object> oneScope = createMock(Map.class); + + expect(request.getContext("one")).andReturn(oneScope); + + replay(request); + assertEquals(oneScope, accessor.getProperty(null, request, "oneScope")); + assertNull(accessor.getProperty(null, request, "whatever")); + verify(request); + } + + @Test + public void testGetSourceAccessor() { + Request request = createMock(Request.class); + + replay(request); + assertEquals(".getContext(\"one\")", accessor.getSourceAccessor(null, request, "oneScope")); + assertNull(accessor.getSourceAccessor(null, request, "whatever")); + verify(request); + } + + @Test + public void testGetSourceSetter() { + assertNull(accessor.getSourceSetter(null, null, "whatever")); + } + + /** + * Test method for {@link ScopePropertyAccessor#setProperty(Map, Object, Object, Object)}. + */ + @Test + public void testSetProperty() { + accessor.setProperty(null, null, "whatever", "whateverValue"); + } + +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/ognl/TilesApplicationContextNestedObjectExtractorTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/TilesApplicationContextNestedObjectExtractorTest.java new file mode 100644 index 000000000..b791a646c --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/TilesApplicationContextNestedObjectExtractorTest.java @@ -0,0 +1,51 @@ +/* + * 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.ognl; + +import org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.Request; +import org.junit.Test; + +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 TilesApplicationContextNestedObjectExtractor}. + */ +public class TilesApplicationContextNestedObjectExtractorTest { + + /** + * Tests {@link TilesApplicationContextNestedObjectExtractor#getNestedObject(Request)}. + */ + @Test + public void testGetNestedObject() { + Request request = createMock(Request.class); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + expect(request.getApplicationContext()).andReturn(applicationContext); + + replay(request, applicationContext); + NestedObjectExtractor<Request> extractor = new TilesApplicationContextNestedObjectExtractor(); + assertEquals(applicationContext, extractor.getNestedObject(request)); + verify(request, applicationContext); + } +} diff --git a/plugins/tiles/src/test/java/org/apache/tiles/ognl/TilesContextPropertyAccessorDelegateFactoryTest.java b/plugins/tiles/src/test/java/org/apache/tiles/ognl/TilesContextPropertyAccessorDelegateFactoryTest.java new file mode 100644 index 000000000..8a70ae220 --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/tiles/ognl/TilesContextPropertyAccessorDelegateFactoryTest.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.ognl; + +import ognl.PropertyAccessor; +import org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.Request; +import org.junit.Test; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.assertEquals; + +/** + * Tests {@link TilesContextPropertyAccessorDelegateFactory}. + */ +public class TilesContextPropertyAccessorDelegateFactoryTest { + + /** + * Test method for + * {@link TilesContextPropertyAccessorDelegateFactory#getPropertyAccessor(String, Request)} + * . + */ + @Test + public void testGetPropertyAccessorRequest() { + PropertyAccessor objectPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationContextPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor requestScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor sessionScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationScopePropertyAccessor = createMock(PropertyAccessor.class); + Request request = createMock(Request.class); + + replay(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor); + assertEquals(objectPropertyAccessor, factory.getPropertyAccessor("writer", request)); + + verify(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + } + + /** + * Test method for + * {@link TilesContextPropertyAccessorDelegateFactory#getPropertyAccessor(String, Request)} + * . + */ + @Test + public void testGetPropertyAccessorApplication() { + PropertyAccessor objectPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationContextPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor requestScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor sessionScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationScopePropertyAccessor = createMock(PropertyAccessor.class); + Request request = createMock(Request.class); + + replay(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor); + assertEquals(applicationContextPropertyAccessor, factory.getPropertyAccessor("initParams", request)); + + verify(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + } + + /** + * Test method for + * {@link TilesContextPropertyAccessorDelegateFactory#getPropertyAccessor(String, Request)} + * . + */ + @Test + public void testGetPropertyAccessorRequestScope() { + PropertyAccessor objectPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationContextPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor requestScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor sessionScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationScopePropertyAccessor = createMock(PropertyAccessor.class); + Request request = createMock(Request.class); + + replay(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor); + assertEquals(requestScopePropertyAccessor, factory.getPropertyAccessor("attribute", request)); + + verify(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + } + + /** + * Test method for + * {@link TilesContextPropertyAccessorDelegateFactory#getPropertyAccessor(String, Request)} + * . + */ + @Test + public void testGetPropertyAccessorSessionScope() { + PropertyAccessor objectPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationContextPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor requestScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor sessionScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationScopePropertyAccessor = createMock(PropertyAccessor.class); + Request request = createMock(Request.class); + + replay(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor); + assertEquals(requestScopePropertyAccessor, factory.getPropertyAccessor("attribute", request)); + + verify(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request); + } + + /** + * Test method for + * {@link TilesContextPropertyAccessorDelegateFactory#getPropertyAccessor(String, Request)} + * . + */ + @Test + public void testGetPropertyAccessorApplicationScope() { + PropertyAccessor objectPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationContextPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor requestScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor sessionScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationScopePropertyAccessor = createMock(PropertyAccessor.class); + Request request = createMock(Request.class); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + + replay(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request, applicationContext); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor); + assertEquals(requestScopePropertyAccessor, factory.getPropertyAccessor("attribute", request)); + + verify(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request, applicationContext); + } + + /** + * Test method for + * {@link TilesContextPropertyAccessorDelegateFactory#getPropertyAccessor(String, Request)} + * . + */ + @Test + public void testGetPropertyAccessorRequestScopeDefault() { + PropertyAccessor objectPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationContextPropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor requestScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor sessionScopePropertyAccessor = createMock(PropertyAccessor.class); + PropertyAccessor applicationScopePropertyAccessor = createMock(PropertyAccessor.class); + Request request = createMock(Request.class); + ApplicationContext applicationContext = createMock(ApplicationContext.class); + + replay(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request, applicationContext); + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor); + assertEquals(requestScopePropertyAccessor, factory.getPropertyAccessor("attribute", request)); + + verify(objectPropertyAccessor, applicationContextPropertyAccessor, requestScopePropertyAccessor, + sessionScopePropertyAccessor, applicationScopePropertyAccessor, request, applicationContext); + } + +}