Author: markt
Date: Wed Oct 29 23:02:44 2014
New Revision: 1635328

URL: http://svn.apache.org/r1635328
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=57148
When coercing an object to a given type and a PropertyEditor has been 
registered for the type correctly coerce the empty string to null if the 
PropertyEditor throws an exception.
Patch by kkolinko
Unit tests by markt

Added:
    tomcat/trunk/test/org/apache/el/lang/TesterBean.java   (with props)
    tomcat/trunk/test/org/apache/el/lang/TesterType.java   (with props)
    tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorBase.java   (with 
props)
    tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorError.java   (with 
props)
    tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorNoError.java   (with 
props)
Modified:
    tomcat/trunk/java/org/apache/el/lang/ELSupport.java
    tomcat/trunk/test/org/apache/el/lang/TestELSupport.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: tomcat/trunk/java/org/apache/el/lang/ELSupport.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/lang/ELSupport.java?rev=1635328&r1=1635327&r2=1635328&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/el/lang/ELSupport.java (original)
+++ tomcat/trunk/java/org/apache/el/lang/ELSupport.java Wed Oct 29 23:02:44 2014
@@ -465,12 +465,24 @@ public class ELSupport {
         if (obj == null)
             return null;
         if (obj instanceof String) {
-            if ("".equals(obj))
-                return null;
             PropertyEditor editor = PropertyEditorManager.findEditor(type);
-            if (editor != null) {
-                editor.setAsText((String) obj);
-                return editor.getValue();
+            if (editor == null) {
+                if ("".equals(obj)) {
+                    return null;
+                }
+                throw new ELException(MessageFactory.get("error.convert", obj,
+                        obj.getClass(), type));
+            } else {
+                try {
+                    editor.setAsText((String) obj);
+                    return editor.getValue();
+                } catch (RuntimeException e) {
+                    if ("".equals(obj)) {
+                        return null;
+                    }
+                    throw new ELException(MessageFactory.get("error.convert",
+                            obj, obj.getClass(), type), e);
+                }
             }
         }
 

Modified: tomcat/trunk/test/org/apache/el/lang/TestELSupport.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/lang/TestELSupport.java?rev=1635328&r1=1635327&r2=1635328&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/el/lang/TestELSupport.java (original)
+++ tomcat/trunk/test/org/apache/el/lang/TestELSupport.java Wed Oct 29 23:02:44 
2014
@@ -16,6 +16,7 @@
  */
 package org.apache.el.lang;
 
+import java.beans.PropertyEditorManager;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
@@ -207,6 +208,39 @@ public class TestELSupport {
     }
 
     @Test
+    public void testCoerceToType13() {
+        Object result = ELManager.getExpressionFactory().coerceToType(
+                "", TesterType.class);
+        Assert.assertNull(result);
+    }
+
+    @Test
+    public void testCoerceToType14() {
+        PropertyEditorManager.registerEditor(TesterType.class, 
TesterTypeEditorNoError.class);
+        Object result = ELManager.getExpressionFactory().coerceToType(
+                "Foo", TesterType.class);
+        Assert.assertTrue(result instanceof TesterType);
+        Assert.assertEquals("Foo", ((TesterType) result).getValue());
+    }
+
+    @Test(expected=ELException.class)
+    public void testCoerceToType15() {
+        PropertyEditorManager.registerEditor(TesterType.class, 
TesterTypeEditorError.class);
+        Object result = ELManager.getExpressionFactory().coerceToType(
+                "Foo", TesterType.class);
+        Assert.assertTrue(result instanceof TesterType);
+        Assert.assertEquals("Foo", ((TesterType) result).getValue());
+    }
+
+    @Test
+    public void testCoerceToType16() {
+        PropertyEditorManager.registerEditor(TesterType.class, 
TesterTypeEditorError.class);
+        Object result = ELManager.getExpressionFactory().coerceToType(
+                "", TesterType.class);
+        Assert.assertNull(result);
+    }
+
+    @Test
     public void testCoerceToNumber01() {
         Object result = ELSupport.coerceToNumber(
                 (Object) null, Integer.class);

Added: tomcat/trunk/test/org/apache/el/lang/TesterBean.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/lang/TesterBean.java?rev=1635328&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/el/lang/TesterBean.java (added)
+++ tomcat/trunk/test/org/apache/el/lang/TesterBean.java Wed Oct 29 23:02:44 
2014
@@ -0,0 +1,21 @@
+/*
+ * 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.el.lang;
+
+public class TesterBean {
+
+}

Propchange: tomcat/trunk/test/org/apache/el/lang/TesterBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/test/org/apache/el/lang/TesterType.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/lang/TesterType.java?rev=1635328&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/el/lang/TesterType.java (added)
+++ tomcat/trunk/test/org/apache/el/lang/TesterType.java Wed Oct 29 23:02:44 
2014
@@ -0,0 +1,30 @@
+/*
+ * 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.el.lang;
+
+public class TesterType {
+
+    private final String value;
+
+    public TesterType(String value) {
+        this.value = value;
+    }
+
+    public String getValue() {
+        return value;
+    }
+}

Propchange: tomcat/trunk/test/org/apache/el/lang/TesterType.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorBase.java?rev=1635328&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorBase.java (added)
+++ tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorBase.java Wed Oct 29 
23:02:44 2014
@@ -0,0 +1,79 @@
+/*
+ * 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.el.lang;
+
+import java.awt.Component;
+import java.awt.Graphics;
+import java.awt.Rectangle;
+import java.beans.PropertyChangeListener;
+import java.beans.PropertyEditor;
+
+public abstract class TesterTypeEditorBase implements PropertyEditor {
+
+    protected TesterType type = null;
+
+    @Override
+    public void setValue(Object value) {
+    }
+
+    @Override
+    public Object getValue() {
+        return type;
+    }
+
+    @Override
+    public boolean isPaintable() {
+        return false;
+    }
+
+    @Override
+    public void paintValue(Graphics gfx, Rectangle box) {
+    }
+
+    @Override
+    public String getJavaInitializationString() {
+        return null;
+    }
+
+    @Override
+    public String getAsText() {
+        return null;
+    }
+
+    @Override
+    public String[] getTags() {
+        return null;
+    }
+
+    @Override
+    public Component getCustomEditor() {
+        return null;
+    }
+
+    @Override
+    public boolean supportsCustomEditor() {
+        return false;
+    }
+
+    @Override
+    public void addPropertyChangeListener(PropertyChangeListener listener) {
+    }
+
+    @Override
+    public void removePropertyChangeListener(PropertyChangeListener listener) {
+    }
+}

Propchange: tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorError.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorError.java?rev=1635328&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorError.java (added)
+++ tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorError.java Wed Oct 29 
23:02:44 2014
@@ -0,0 +1,25 @@
+/*
+ * 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.el.lang;
+
+public class TesterTypeEditorError extends TesterTypeEditorBase {
+
+    @Override
+    public void setAsText(String text) throws IllegalArgumentException {
+        throw new IllegalArgumentException();
+    }
+}

Propchange: tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorError.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorNoError.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorNoError.java?rev=1635328&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorNoError.java (added)
+++ tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorNoError.java Wed Oct 
29 23:02:44 2014
@@ -0,0 +1,25 @@
+/*
+ * 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.el.lang;
+
+public class TesterTypeEditorNoError extends TesterTypeEditorBase {
+
+    @Override
+    public void setAsText(String text) throws IllegalArgumentException {
+        type = new TesterType(text);
+    }
+}

Propchange: tomcat/trunk/test/org/apache/el/lang/TesterTypeEditorNoError.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1635328&r1=1635327&r2=1635328&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Wed Oct 29 23:02:44 2014
@@ -282,6 +282,12 @@
         Do not throw an exception on missing JSP file servlet initialization.
         (remm)
       </fix>
+      <fix>
+        <bug>57148</bug>: When coercing an object to a given type and a
+        <code>PropertyEditor</code> has been registered for the type correctly
+        coerce the empty string to <code>null</code> if the
+        <code>PropertyEditor</code> throws an exception. (kkolinko/markt)
+      </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