I now went into the ant taglib sourcecode, to track down the problem of "[jelly] problem: setting Map property on bean?"
http://www.mail-archive.com/[EMAIL PROTECTED]/msg17993.html


I found out that there are two bugs in there:

1. any non-string attribute is converted to string:
setAttribute(String name, Object value) is converting all values to strings by calling value.toString().
I also removed the check for Expression (seemed redundant), as all the expressions seem to be resolved before they are passed to this method.
All the test cases still pass, so I assume that this is ok.
If this is not the case then ignore this removal.


2. even if the value is not converted to string and is passed through as-is, there is a bug in finding the appropriate setter method.
The code seems to assume that storeElement() method in IntrospectionHelper (in ant) throws an exception, if the element is not supported by the task. However, this is not the case: if the element is not supported, storeElement() silently returns without any errors. I think this is a bug in Ant, and I will report it there as well. As a workaround for now, I added a call to getElementType() that throws an exception if the element is not supported.


Also, I added testcase for this + some more logging info and comments here-and-there...

Patches and files are attached.

Rgds,
Neeme
/*
 * $Header: 
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/JellyTestSuite.java,v
 1.8 2002/07/06 13:53:39 dion Exp $
 * $Revision: 1.8 $
 * $Date: 2002/07/06 13:53:39 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 * 
 * $Id: JellyTestSuite.java,v 1.8 2002/07/06 13:53:39 dion Exp $
 */
package org.apache.commons.jelly.ant.task;

import java.util.Map;
import java.util.HashMap;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;

/*

<taskdef
   name="maptask"
   classname="org.apache.commons.jelly.ant.task.MapTask">
   <classpath>
      <pathelement path="somewhere"/>
   </classpath>
</taskdef>

<j:new className="java.util.HashMap" var="map"/>
<maptask map=${map}/>

Ant:
   [maptask] map set!

Maven/Jelly:
map set!

*/

/** 
 * A sample Task to test out the Ant introspection logic
 *
 * @author Neeme Praks ([EMAIL PROTECTED])
 * @version $Revision: 1.8 $
 */
public class MapTask extends Task {
    private Map map = null;

    public void execute() throws BuildException {
        if (map == null) {
            throw new BuildException("Map has to be set!");
        }
    }

    public void setMap(Map map) {
        System.out.println("map set!");
        this.map = map;
    }

}
Index: suite.jelly
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/ant/src/test/org/apache/commons/jelly/ant/suite.jelly,v
retrieving revision 1.10
diff -r1.10 suite.jelly
220a221,238
> <test:case name="setPropertyAsMap">
> 
>     <taskdef
>         name="maptask"
>         classname="org.apache.commons.jelly.ant.task.MapTask">
>     </taskdef>
> 
>     <j:new className="java.util.HashMap" var="map"/>
>     <maptask map="${map}"/>
>       
>     <j:catch var="ex">
>         <maptask/>
>     </j:catch>        
> 
>     <test:assert test="${ex != null}">We should have created an 
> exception</test:assert>
>       
> </test:case>
> 
Index: AntTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/ant/src/java/org/apache/commons/jelly/tags/ant/AntTag.java,v
retrieving revision 1.23
diff -r1.23 AntTag.java
82a83
> import org.apache.tools.ant.BuildException;
370,377c371
<             if ( value instanceof Expression )
<             {
<                 super.setAttribute( name, ((Expression) 
value).evaluateRecurse(context) );
<             }
<             else
<             {
<               super.setAttribute( name, value.toString() );
<               }                            
---
>             super.setAttribute( name, value );
382,384c376,378
<         if ( log.isDebugEnabled() ) {
<             log.debug( "Setting bean property on: "+  object + " name: " + name + " 
value: " + value );
<         }
---
>         if ( log.isDebugEnabled() )
>             log.debug( "Setting bean property on: " + object + 
>                 " name: " + name + " value: " + value );
387a382,383
>         //ant only handles attributes (and converts these) from string source, 
>         //no other source types are supported
394a391,393
>                 if ( log.isDebugEnabled() ) 
>                     log.debug( "setting ant property on: " + object + 
>                         " (" + name + "=" + value + ") failed, trying to set 
> addConfigured", e );
399c398,400
< 
---
>             //we need to call also getElementType() because storeElement() does not 
> throw an
>             //exception when there is no such binding defined
>             ih.getElementType(name);
402c403,406
<         catch (Exception e) {
---
>         catch (BuildException e) {
>             if ( log.isDebugEnabled() ) 
>                 log.debug( "setting addConfigured on: " + object + 
>                     " (" + name + "=" + value + ") failed, trying to set bean 
> property", e );
Index: DummyTask.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/ant/src/test/org/apache/commons/jelly/ant/task/DummyTask.java,v
retrieving revision 1.3
diff -r1.3 DummyTask.java
71c71
<    classname="jellybug.NestedTask"
---
>    classname="org.apache.commons.jelly.ant.task.NestedTask">
161c161
<     
---
> 
Index: example.jelly
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/ant/src/test/org/apache/commons/jelly/ant/task/example.jelly,v
retrieving revision 1.2
diff -r1.2 example.jelly
6,21c6,9
<       <taskdef
<       name="nested"
<     classname="org.apache.commons.jelly.ant.task.DummyTask">
<       </taskdef>
<       
<       <nested force="true">
<    <ding/>
<    <dang/>
<    <dong/>
<    <hiphop/>
<    <wontstop/>
<    <tillyoudrop/>
<    <hipHop/>
<    <wontStop/>
<    <tillYouDrop/>
<       </nested>
---
>     <taskdef
>         name="nested"
>         classname="org.apache.commons.jelly.ant.task.DummyTask">
>     </taskdef>
22a11,22
>     <nested force="true">
>         <ding/>
>         <dang/>
>         <dong/>
>         <hiphop/>
>         <wontstop/>
>         <tillyoudrop/>
>         <hipHop/>
>         <wontStop/>
>         <tillYouDrop/>
>     </nested>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to