You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2008/03/13 20:00:25 UTC
svn commit: r636835 - in /tiles/framework/trunk/tiles-core/src:
main/java/org/apache/tiles/evaluator/el/
test/java/org/apache/tiles/evaluator/el/
Author: apetrelli
Date: Thu Mar 13 12:00:24 2008
New Revision: 636835
URL: http://svn.apache.org/viewvc?rev=636835&view=rev
Log:
TILES-48
Fixed resolvers.
Added tests for resolvers.
Added:
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java (with props)
Modified:
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextBeanELResolver.java
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextELResolver.java
Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextBeanELResolver.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextBeanELResolver.java?rev=636835&r1=636834&r2=636835&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextBeanELResolver.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextBeanELResolver.java Thu Mar 13 12:00:24 2008
@@ -20,10 +20,7 @@
*/
package org.apache.tiles.evaluator.el;
-import java.beans.BeanInfo;
import java.beans.FeatureDescriptor;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -32,8 +29,6 @@
import javax.el.ELContext;
import javax.el.ELResolver;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.context.TilesRequestContext;
@@ -45,12 +40,6 @@
*/
public class TilesContextBeanELResolver extends ELResolver {
- /**
- * The logging object.
- */
- private static final Log LOG = LogFactory
- .getLog(TilesContextBeanELResolver.class);
-
/** {@inheritDoc} */
@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
@@ -132,23 +121,17 @@
return;
}
- for (Object bean : map.values()) {
- BeanInfo info = null;
- try {
- info = Introspector.getBeanInfo(bean.getClass());
- } catch (Exception ex) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Cannot inspect bean " + bean.getClass(), ex);
- }
- }
- if (info == null) {
- return;
- }
- for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
- pd.setValue("type", pd.getPropertyType());
- pd.setValue("resolvableAtDesignTime", Boolean.FALSE);
- list.add(pd);
- }
+ for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
+ FeatureDescriptor descriptor = new FeatureDescriptor();
+ descriptor.setDisplayName(entry.getKey());
+ descriptor.setExpert(false);
+ descriptor.setHidden(false);
+ descriptor.setName(entry.getKey());
+ descriptor.setPreferred(true);
+ descriptor.setShortDescription("");
+ descriptor.setValue("type", String.class);
+ descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE);
+ list.add(descriptor);
}
}
Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextELResolver.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextELResolver.java?rev=636835&r1=636834&r2=636835&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextELResolver.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/evaluator/el/TilesContextELResolver.java Thu Mar 13 12:00:24 2008
@@ -25,11 +25,14 @@
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import javax.el.BeanELResolver;
import javax.el.ELContext;
+import javax.el.PropertyNotFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -56,15 +59,33 @@
*
* @since 2.1.0
*/
- private List<FeatureDescriptor> descriptors;
+ protected List<FeatureDescriptor> descriptors;
+
+ /**
+ * Contains the properties of {@link TilesRequestContext}.
+ *
+ * @since 2.1.0
+ */
+ protected Set<String> requestProperties;
+
+ /**
+ * Contains the properties of {@link TilesApplicationContext}.
+ *
+ * @since 2.1.0
+ */
+ protected Set<String> applicationProperties;
/**
* Constructor.
*/
public TilesContextELResolver() {
- List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
- collectBeanInfo(TilesRequestContext.class, list);
- collectBeanInfo(TilesApplicationContext.class, list);
+ descriptors = new ArrayList<FeatureDescriptor>();
+ requestProperties = new HashSet<String>();
+ applicationProperties = new HashSet<String>();
+ collectBeanInfo(TilesRequestContext.class, descriptors,
+ requestProperties);
+ collectBeanInfo(TilesApplicationContext.class, descriptors,
+ applicationProperties);
}
/** {@inheritDoc} */
@@ -98,14 +119,20 @@
return null;
}
- TilesRequestContext request = (TilesRequestContext) context
- .getContext(TilesRequestContext.class);
-
- Class<?> retValue = super.getType(context, request, property);
- if (retValue == null) {
+ Class<?> retValue;
+ if (requestProperties.contains(property)) {
+ TilesRequestContext request = (TilesRequestContext) context
+ .getContext(TilesRequestContext.class);
+ retValue = super.getType(context, request, property);
+ } else if (applicationProperties.contains(property)) {
TilesApplicationContext applicationContext = (TilesApplicationContext) context
.getContext(TilesApplicationContext.class);
retValue = super.getType(context, applicationContext, property);
+ } else {
+ throw new PropertyNotFoundException(
+ "Cannot find property "
+ + property
+ + " neither in TilesRequestContext nor in TilesApplicationContext");
}
context.setPropertyResolved(true);
return retValue;
@@ -119,16 +146,23 @@
return null;
}
- TilesRequestContext request = (TilesRequestContext) context
- .getContext(TilesRequestContext.class);
+ Object retValue;
- Object retValue = super.getValue(context, request, property);
- if (retValue == null) {
+ if (requestProperties.contains(property)) {
+ TilesRequestContext request = (TilesRequestContext) context
+ .getContext(TilesRequestContext.class);
+ retValue = super.getValue(context, request, property);
+ } else if (applicationProperties.contains(property)) {
TilesApplicationContext applicationContext = (TilesApplicationContext) context
.getContext(TilesApplicationContext.class);
retValue = super.getValue(context, applicationContext, property);
+ } else {
+ throw new PropertyNotFoundException(
+ "Cannot find property "
+ + property
+ + " neither in TilesRequestContext nor in TilesApplicationContext");
}
- context.setPropertyResolved(true);
+
return retValue;
}
@@ -154,8 +188,11 @@
*
* @param clazz The class to be inspected.
* @param list The list to fill.
+ * @param properties The properties set to be filled.
+ * @since 2.1.0
*/
- protected void collectBeanInfo(Class<?> clazz, List<FeatureDescriptor> list) {
+ protected void collectBeanInfo(Class<?> clazz,
+ List<FeatureDescriptor> list, Set<String> properties) {
BeanInfo info = null;
try {
info = Introspector.getBeanInfo(clazz);
@@ -171,6 +208,7 @@
pd.setValue("type", pd.getPropertyType());
pd.setValue("resolvableAtDesignTime", Boolean.TRUE);
list.add(pd);
+ properties.add(pd.getName());
}
}
}
Added: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java?rev=636835&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java (added)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java Thu Mar 13 12:00:24 2008
@@ -0,0 +1,153 @@
+/*
+ * $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.evaluator.el;
+
+import java.beans.FeatureDescriptor;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.context.TilesRequestContext;
+import org.easymock.EasyMock;
+
+import de.odysseus.el.util.SimpleContext;
+
+/**
+ * Tests {@link TilesContextELResolver}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class TilesContextELResolverTest extends TestCase {
+
+ /**
+ * The resolver to test.
+ */
+ private TilesContextELResolver resolver;
+
+ /** {@inheritDoc} */
+ protected void setUp() throws Exception {
+ super.setUp();
+ resolver = new TilesContextELResolver();
+ }
+
+ /**
+ * Test method for
+ * {@link TilesContextELResolver#getCommonPropertyType(javax.el.ELContext, java.lang.Object)}.
+ */
+ public void testGetCommonPropertyTypeELContextObject() {
+ 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 TilesContextELResolver#getFeatureDescriptors(javax.el.ELContext, java.lang.Object)}.
+ */
+ public void testGetFeatureDescriptorsELContextObject() {
+ List<FeatureDescriptor> expected = new ArrayList<FeatureDescriptor>();
+ Set<String> properties = new HashSet<String>();
+ resolver.collectBeanInfo(TilesRequestContext.class, expected,
+ properties);
+ resolver.collectBeanInfo(TilesApplicationContext.class, expected,
+ properties);
+ Iterator<FeatureDescriptor> featureIt = resolver.getFeatureDescriptors(
+ null, null);
+ Iterator<FeatureDescriptor> expectedIt = expected.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());
+ }
+
+ /**
+ * Test method for
+ * {@link TilesContextELResolver#getType(javax.el.ELContext, java.lang.Object, java.lang.Object)}.
+ */
+ public void testGetType() {
+ TilesRequestContext request = EasyMock
+ .createMock(TilesRequestContext.class);
+ TilesApplicationContext applicationContext = EasyMock
+ .createMock(TilesApplicationContext.class);
+ SimpleContext context = new SimpleContext(resolver);
+ EasyMock.replay(request, applicationContext);
+ context.putContext(TilesRequestContext.class, request);
+ context.putContext(TilesApplicationContext.class, applicationContext);
+ 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 TilesContextELResolver#getValue(javax.el.ELContext, java.lang.Object, java.lang.Object)}.
+ */
+ public void testGetValue() {
+ Map<String, Object> requestScope = new HashMap<String, Object>();
+ requestScope.put("objectKey", "objectValue");
+ Map<String, Object> sessionScope = new HashMap<String, Object>();
+ sessionScope.put("sessionObjectKey", "sessionObjectValue");
+ Map<String, Object> applicationScope = new HashMap<String, Object>();
+ applicationScope.put("applicationObjectKey", "applicationObjectValue");
+ TilesRequestContext request = EasyMock
+ .createMock(TilesRequestContext.class);
+ EasyMock.expect(request.getRequestScope()).andReturn(requestScope);
+ EasyMock.expect(request.getSessionScope()).andReturn(sessionScope);
+ TilesApplicationContext applicationContext = EasyMock
+ .createMock(TilesApplicationContext.class);
+ EasyMock.expect(applicationContext.getApplicationScope()).andReturn(
+ applicationScope);
+ SimpleContext context = new SimpleContext(resolver);
+ EasyMock.replay(request, applicationContext);
+ context.putContext(TilesRequestContext.class, request);
+ context.putContext(TilesApplicationContext.class, applicationContext);
+ 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"));
+ }
+
+ /**
+ * Test method for
+ * {@link TilesContextELResolver#isReadOnly(javax.el.ELContext, java.lang.Object, java.lang.Object)}.
+ */
+ public void testIsReadOnly() {
+ SimpleContext context = new SimpleContext(resolver);
+ assertTrue("The value is not read only", resolver.isReadOnly(context,
+ null, null));
+ }
+}
Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/evaluator/el/TilesContextELResolverTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL