Author: violetagg
Date: Mon Aug 26 16:02:28 2013
New Revision: 1517586

URL: http://svn.apache.org/r1517586
Log:
Merged revision 1517536 from tomcat/trunk:
javax.el.BeanELResolver:
- getFeatureDescriptors, getCommonPropertyType do not throw NPE when the 
supplied context is null.
- unit test is added

Added:
    tomcat/tc7.0.x/trunk/test/javax/el/TesterBean.java   (with props)
Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/javax/el/BeanELResolver.java
    tomcat/tc7.0.x/trunk/test/javax/el/TestBeanELResolver.java
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1517536

Modified: tomcat/tc7.0.x/trunk/java/javax/el/BeanELResolver.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/javax/el/BeanELResolver.java?rev=1517586&r1=1517585&r2=1517586&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/javax/el/BeanELResolver.java (original)
+++ tomcat/tc7.0.x/trunk/java/javax/el/BeanELResolver.java Mon Aug 26 16:02:28 
2013
@@ -175,10 +175,6 @@ public class BeanELResolver extends ELRe
 
     @Override
     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext 
context, Object base) {
-        if (context == null) {
-            throw new NullPointerException();
-        }
-
         if (base == null) {
             return null;
         }
@@ -200,10 +196,6 @@ public class BeanELResolver extends ELRe
 
     @Override
     public Class<?> getCommonPropertyType(ELContext context, Object base) {
-        if (context == null) {
-            throw new NullPointerException();
-        }
-
         if (base != null) {
             return Object.class;
         }

Modified: tomcat/tc7.0.x/trunk/test/javax/el/TestBeanELResolver.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/javax/el/TestBeanELResolver.java?rev=1517586&r1=1517585&r2=1517586&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/test/javax/el/TestBeanELResolver.java (original)
+++ tomcat/tc7.0.x/trunk/test/javax/el/TestBeanELResolver.java Mon Aug 26 
16:02:28 2013
@@ -16,14 +16,26 @@
  */
 package javax.el;
 
-import org.junit.Assert;
+import java.beans.FeatureDescriptor;
+import java.beans.PropertyDescriptor;
+import java.util.Iterator;
 
+import org.junit.Assert;
 import org.junit.Test;
 
 import org.apache.jasper.el.ELContextImpl;
 
 public class TestBeanELResolver {
 
+    private static final String METHOD01_NAME = "toString";
+    private static final String METHOD02_NAME = "<init>";
+    private static final String METHOD03_NAME = "nonExistingMethod";
+    private static final String BEAN_NAME = "test";
+    private static final String PROPERTY01_NAME = "valueA";
+    private static final String PROPERTY02_NAME = "valueB";
+    private static final String PROPERTY03_NAME = "name";
+    private static final String PROPERTY_VALUE = "test1";
+
     @Test
     public void testBug53421() {
         ExpressionFactory factory = ExpressionFactory.newInstance();
@@ -54,6 +66,398 @@ public class TestBeanELResolver {
                 msg.contains(type));
     }
 
+    /**
+     * Tests that a null context results in an NPE as per EL Javadoc.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testGetType01() {
+        BeanELResolver resolver = new BeanELResolver();
+        resolver.getType(null, new Object(), new Object());
+    }
+
+    /**
+     * Tests that a valid property is not resolved if base is null.
+     */
+    @Test
+    public void testGetType02() {
+        doNegativeTest(null, new Object(), MethodUnderTest.GET_TYPE, true);
+    }
+
+    /**
+     * Tests that a valid property is resolved.
+     */
+    @Test
+    public void testGetType03() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        Class<?> result = resolver.getType(context, new Bean(), 
PROPERTY01_NAME);
+
+        Assert.assertEquals(String.class, result);
+        Assert.assertTrue(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that an exception will be thrown when the property does not exist.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testGetType04() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.getType(context, new Bean(), PROPERTY02_NAME);
+    }
+
+    /**
+     * Tests that an exception will be thrown when a coercion cannot be
+     * performed.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testGetType05() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.getType(context, new Bean(), new Object());
+    }
+
+    /**
+     * Tests that a null context results in an NPE as per EL Javadoc.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testGetValue01() {
+        BeanELResolver resolver = new BeanELResolver();
+        resolver.getValue(null, new Object(), new Object());
+    }
+
+    /**
+     * Tests that a valid property is not resolved if base is null.
+     */
+    @Test
+    public void testGetValue02() {
+        doNegativeTest(null, new Object(), MethodUnderTest.GET_VALUE, true);
+    }
+
+    /**
+     * Tests that a valid property is resolved.
+     */
+    @Test
+    public void testGetValue03() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        Object result = resolver.getValue(context, new TesterBean(BEAN_NAME), 
PROPERTY03_NAME);
+
+        Assert.assertEquals(BEAN_NAME, result);
+        Assert.assertTrue(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that an exception will be thrown when the property does not exist.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testGetValue04() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.getValue(context, new Bean(), PROPERTY02_NAME);
+    }
+
+    /**
+     * Tests that an exception will be thrown when a coercion cannot be
+     * performed.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testGetValue05() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.getValue(context, new Bean(), new Object());
+    }
+
+    /**
+     * Tests that an exception will be thrown when the property is not 
readable.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testGetValue06() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.getValue(context, new Bean(), PROPERTY01_NAME);
+    }
+
+    /**
+     * Tests that getter method throws exception which should be propagated.
+     */
+    @Test(expected = ELException.class)
+    public void testGetValue07() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.getValue(context, new TesterBean(BEAN_NAME), PROPERTY01_NAME);
+    }
+
+    /**
+     * Tests that a null context results in an NPE as per EL Javadoc.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testSetValue01() {
+        BeanELResolver resolver = new BeanELResolver();
+        resolver.setValue(null, new Object(), new Object(), new Object());
+    }
+
+    /**
+     * Tests that a valid property is not resolved if base is null.
+     */
+    @Test
+    public void testSetValue02() {
+        doNegativeTest(null, new Object(), MethodUnderTest.SET_VALUE, true);
+    }
+
+    /**
+     * Tests that an exception is thrown when readOnly is true.
+     */
+    @Test(expected = PropertyNotWritableException.class)
+    public void testSetValue03() {
+        BeanELResolver resolver = new BeanELResolver(true);
+        ELContext context = new ELContextImpl();
+
+        resolver.setValue(context, new Bean(), new Object(), new Object());
+    }
+
+    /**
+     * Tests that a valid property is resolved.
+     */
+    @Test
+    public void testSetValue04() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        TesterBean bean = new TesterBean(BEAN_NAME);
+        resolver.setValue(context, bean, PROPERTY03_NAME, PROPERTY_VALUE);
+
+        Assert.assertEquals(PROPERTY_VALUE, resolver.getValue(context, bean, 
PROPERTY03_NAME));
+        Assert.assertTrue(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that an exception will be thrown when a coercion cannot be
+     * performed.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testSetValue05() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.setValue(context, new Bean(), new Object(), PROPERTY_VALUE);
+    }
+
+    /**
+     * Tests that an exception will be thrown when the property does not exist.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testSetValue06() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.setValue(context, new Bean(), PROPERTY02_NAME, 
PROPERTY_VALUE);
+    }
+
+    /**
+     * Tests that an exception will be thrown when the property does not have
+     * setter method.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testSetValue07() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.setValue(context, new TesterBean(BEAN_NAME), PROPERTY01_NAME, 
PROPERTY_VALUE);
+    }
+
+    /**
+     * Tests that a null context results in an NPE as per EL Javadoc.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testIsReadOnly01() {
+        BeanELResolver resolver = new BeanELResolver();
+        resolver.isReadOnly(null, new Object(), new Object());
+    }
+
+    /**
+     * Tests that the propertyResolved is false if base is null.
+     */
+    @Test
+    public void testIsReadOnly02() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.isReadOnly(context, null, new Object());
+
+        Assert.assertFalse(context.isPropertyResolved());
+
+        resolver = new BeanELResolver(true);
+
+        resolver.isReadOnly(context, null, new Object());
+
+        Assert.assertFalse(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that if the BeanELResolver is constructed with readOnly the method
+     * will return always true.
+     */
+    @Test
+    public void testIsReadOnly03() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        boolean result = resolver.isReadOnly(context, new 
TesterBean(BEAN_NAME), PROPERTY03_NAME);
+
+        Assert.assertFalse(result);
+        Assert.assertTrue(context.isPropertyResolved());
+
+        resolver = new BeanELResolver(true);
+
+        result = resolver.isReadOnly(context, new TesterBean(BEAN_NAME), 
PROPERTY03_NAME);
+
+        Assert.assertTrue(result);
+        Assert.assertTrue(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that an exception is thrown when a coercion cannot be performed.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testIsReadOnly04() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.isReadOnly(context, new TesterBean(BEAN_NAME), new 
Integer(0));
+    }
+
+    /**
+     * Tests that an exception will be thrown when the property does not exist.
+     */
+    @Test(expected = PropertyNotFoundException.class)
+    public void testIsReadOnly05() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.isReadOnly(context, new Bean(), PROPERTY02_NAME);
+    }
+
+    /**
+     * Tests that true will be returned when the property does not have setter
+     * method.
+     */
+    @Test
+    public void testIsReadOnly06() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        boolean result = resolver.isReadOnly(context, new 
TesterBean(BEAN_NAME), PROPERTY01_NAME);
+
+        Assert.assertTrue(result);
+        Assert.assertTrue(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that a valid FeatureDescriptors are not returned if base is not
+     * Map.
+     */
+    @Test
+    public void testGetFeatureDescriptors01() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        Iterator<FeatureDescriptor> result = 
resolver.getFeatureDescriptors(context, null);
+
+        Assert.assertNull(result);
+    }
+
+    /**
+     * Tests that a valid FeatureDescriptors are returned.
+     */
+    @Test
+    public void testGetFeatureDescriptors02() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        Iterator<FeatureDescriptor> result = 
resolver.getFeatureDescriptors(context, new Bean());
+
+        while (result.hasNext()) {
+            PropertyDescriptor featureDescriptor = (PropertyDescriptor) 
result.next();
+            Assert.assertEquals(featureDescriptor.getPropertyType(),
+                    featureDescriptor.getValue(ELResolver.TYPE));
+            Assert.assertEquals(Boolean.TRUE,
+                    
featureDescriptor.getValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME));
+        }
+    }
+
+    /**
+     * Tests that a null context results in an NPE as per EL Javadoc.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testInvoke01() {
+        BeanELResolver resolver = new BeanELResolver();
+        resolver.invoke(null, new Object(), new Object(), new Class<?>[0], new 
Object[0]);
+    }
+
+    /**
+     * Tests that a valid property is not resolved if base is null.
+     */
+    @Test
+    public void testInvoke02() {
+        doNegativeTest(null, new Object(), MethodUnderTest.INVOKE, true);
+    }
+
+    /**
+     * Tests a method invocation.
+     */
+    @Test
+    public void testInvoke03() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        Object result = resolver.invoke(context, new TesterBean(BEAN_NAME), 
METHOD01_NAME,
+                new Class<?>[] {}, new Object[] {});
+
+        Assert.assertEquals(BEAN_NAME, result);
+        Assert.assertTrue(context.isPropertyResolved());
+    }
+
+    /**
+     * Tests that the method name cannot be coerced to String.
+     */
+    @Test
+    public void testInvoke04() {
+        doNegativeTest(new Bean(), null, MethodUnderTest.INVOKE, true);
+    }
+
+    /**
+     * Tests that a call to &lt;init&gt; as a method name will throw an 
exception.
+     */
+    @Test(expected = MethodNotFoundException.class)
+    public void testInvoke05() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.invoke(context, new TesterBean(BEAN_NAME), METHOD02_NAME, new 
Class<?>[] {},
+                new Object[] {});
+    }
+
+    /**
+     * Tests that a call to a non existing method will throw an exception.
+     */
+    @Test(expected = MethodNotFoundException.class)
+    public void testInvoke06() {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        resolver.invoke(context, new TesterBean(BEAN_NAME), METHOD03_NAME, new 
Class<?>[] {},
+                new Object[] {});
+    }
+
     private static class Bean {
 
         @SuppressWarnings("unused")
@@ -61,4 +465,44 @@ public class TestBeanELResolver {
             // NOOP
         }
     }
+
+    private void doNegativeTest(Object base, Object trigger, MethodUnderTest 
method,
+            boolean checkResult) {
+        BeanELResolver resolver = new BeanELResolver();
+        ELContext context = new ELContextImpl();
+
+        Object result = null;
+        switch (method) {
+        case GET_VALUE: {
+            result = resolver.getValue(context, base, trigger);
+            break;
+        }
+        case SET_VALUE: {
+            resolver.setValue(context, base, trigger, new Object());
+            break;
+        }
+        case GET_TYPE: {
+            result = resolver.getType(context, base, trigger);
+            break;
+        }
+        case INVOKE: {
+            result = resolver.invoke(context, base, trigger, new Class<?>[0], 
new Object[0]);
+            break;
+        }
+        default: {
+            // Should never happen
+            Assert.fail("Missing case for method");
+        }
+        }
+
+        if (checkResult) {
+            Assert.assertNull(result);
+        }
+        Assert.assertFalse(context.isPropertyResolved());
+    }
+
+    private static enum MethodUnderTest {
+        GET_VALUE, SET_VALUE, GET_TYPE, INVOKE
+    }
+
 }

Added: tomcat/tc7.0.x/trunk/test/javax/el/TesterBean.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/javax/el/TesterBean.java?rev=1517586&view=auto
==============================================================================
--- tomcat/tc7.0.x/trunk/test/javax/el/TesterBean.java (added)
+++ tomcat/tc7.0.x/trunk/test/javax/el/TesterBean.java Mon Aug 26 16:02:28 2013
@@ -0,0 +1,43 @@
+/*
+ * 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 javax.el;
+
+public class TesterBean {
+
+    private String name;
+
+    public TesterBean(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String toString() {
+        return getName();
+    }
+
+    public String getValueA() throws Exception {
+        throw new Exception();
+    }
+}

Propchange: tomcat/tc7.0.x/trunk/test/javax/el/TesterBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1517586&r1=1517585&r2=1517586&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Mon Aug 26 16:02:28 2013
@@ -227,6 +227,14 @@
         <bug>55309</bug>: Fix concurrency issue with JSP compilation and the
         tag plug-in manager. Patch provided by Sheldon Shao. (markt)
       </fix>
+      <fix>
+        Ensure that
+        
<code>javax.el.BeanELResolver.getFeatureDescriptors(ELContext,Object)</code>
+        and
+        
<code>javax.el.BeanELResolver.getCommonPropertyType(ELContext,Object)</code>
+        do not throw <code>NullPointerException</code> when the provided 
context
+        is null. (violetagg)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Cluster">



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to