You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Alessio Pace <al...@gmail.com> on 2006/01/04 18:40:13 UTC

[Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)

Hi, in my last post I was trying to dig into the String to String[]
conversion with no success.

So, I splitted it up and did a test case to try to figure out what's wrong.

Here is a Book java bean:

public class Book {
	private int year;
	private String[] authors;
	
	public Book() {}

	public String[] getAuthors() {
		return this.authors;
	}

	public void setAuthors(String[] authors) {
		this.authors = authors;
	}

	public int getYear() {
		return this.year;
	}

	public void setYear(int year) {
		this.year = year;
	}
}



And here is the test case to read it from XML using Digester:

public void testDigester() throws Exception {
		
        String bookAsString = "" +
		"   <book>" +
		"      <year>2005</year>" +
		"      <authors>First,Second</authors>" +
		"   </book>";
		
		// assert StringArrayConverter is there (but it will not be used by
Digester!!)
		Converter converter = ConvertUtils.lookup(new String[] {}.getClass());
		assertNotNull(converter);
		assertTrue(converter instanceof StringArrayConverter);
		
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.addBeanPropertySetter("book/year");
		digester.addBeanPropertySetter("book/authors");
		digester.push(new Book());
		
		Book book = (Book) digester.parse(new StringReader(bookAsString));
		
		assertNotNull(book);
		assertEquals(2005, book.getYear());  // assert 2005: OK

		String[] authorsAsArray = book.getAuthors();
		System.out.println("Array size: "+ authorsAsArray.length + " |
content: " + Arrays.deepToString(authorsAsArray));    // prints
[First,Second]
		
              assertEquals(2, authorsAsArray.length);  // assert 2
authors: FAILS (gives 1)
	}


So in the end Digester make a String[] of size 1, whose content is
"First,Second".


Using ConverterUtils alone instead works:

public void testConvertUtilsItself() throws Exception{
		String authorsAsString = "First,Second";
		Object result = ConvertUtils.convert(authorsAsString, new String[]
{}.getClass());
		String[] authorsAsArray = (String[]) result;
		System.out.println("Array size: "+ authorsAsArray.length + " |
content: " + Arrays.deepToString(authorsAsArray));
		assertEquals(2, authorsAsArray.length);  // assert 2 authors: OK
		
}


So, if Digester has register a StringArrayConverter, why doesn't it use it??

Thanks in advance

--
Alessio Pace.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)

Posted by Alessio Pace <al...@gmail.com>.
Here is it the URL of the bug submission:

http://issues.apache.org/bugzilla/show_bug.cgi?id=38172

Thanks for your interest,
Alessio Pace.

On 1/7/06, Alessio Pace <al...@gmail.com> wrote:
> >
> > I would tend to regard that as a bug in BeanUtils, but that's no help to
> > you. Registering a converter to solve your original problem is obviously
> > not going to work.
> >
>
> Ok, thanks for the confirm.
>
> I will submit the bug on ASF bugzilla providing something similar to
> my 2 tests of the previous email (using one BeanUtils and the other
> ConvertUtils), what do you think?
>
>
> --
> Alessio Pace.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)

Posted by Alessio Pace <al...@gmail.com>.
>
> I would tend to regard that as a bug in BeanUtils, but that's no help to
> you. Registering a converter to solve your original problem is obviously
> not going to work.
>

Ok, thanks for the confirm.

I will submit the bug on ASF bugzilla providing something similar to
my 2 tests of the previous email (using one BeanUtils and the other
ConvertUtils), what do you think?


--
Alessio Pace.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)

Posted by Simon Kitching <sk...@apache.org>.
On Thu, 2006-01-05 at 12:48 +0100, Alessio Pace wrote:
> Well, so here 2 tests, hope it is what you meant:
> 
> -the first uses ConvertUtils and passes
> -the second uses BeanUtils and fails

Yes, that is what I meant. I was a little surprised at your results, so
wrote an independent test myself, and came to the same conclusion:
ConvertUtils supports String->String[], but BeanUtils just doesn't call
the converter.

I would tend to regard that as a bug in BeanUtils, but that's no help to
you. Registering a converter to solve your original problem is obviously
not going to work. 

You might try using CallMethodRule rather than BeanPropertySetter; that
rule optionally takes an array of paramtypes, and converts all its
inputs to those types before invoking the target method. It's meant
really for cases where the target method takes some interface or
abstract type but may well cause the ConvertUtils.convert to be
executed.

Otherwise it looks like you'll have to use a custom Rule class after
all..

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)

Posted by Alessio Pace <al...@gmail.com>.
Well, so here 2 tests, hope it is what you meant:

-the first uses ConvertUtils and passes
-the second uses BeanUtils and fails

public void testConvertUtilsItself() throws Exception{
		String authorsAsString = "First,Second";
		Object result = ConvertUtils.convert(authorsAsString, new String[]
{}.getClass());
		String[] authorsAsArray = (String[]) result;
		System.out.println("Array size: "+ authorsAsArray.length + " |
content: " + Arrays.deepToString(authorsAsArray));
		assertEquals(2, authorsAsArray.length);  // assert 2 authors: OK
}
	
public void testBeanUtils() throws Exception {
		Book book = new Book();
		BeanUtils.setProperty(book, "authors", "{First,Second}");
		String[] authorsAsArray =  book.getAuthors();
		System.out.println("Array size: "+ authorsAsArray.length + " |
content: " + Arrays.deepToString(authorsAsArray));
		assertEquals(2, authorsAsArray.length);  // assert 2 authors: FAILS
		
}

In both cases the setter whose parameter is a String[] is called, in
the first case correctly passing a 2 length string array {"First",
"Second"}, in the second case a 1 length string arrat
{"First,Second"}.

I am really not an expert of the commons-beanutils but those 2
different beahviours seem a little strange to me.

Alessio Pace



On 1/5/06, Simon Kitching <sk...@apache.org> wrote:
> On Wed, 2006-01-04 at 18:40 +0100, Alessio Pace wrote:
> > Hi, in my last post I was trying to dig into the String to String[]
> > conversion with no success.
> >
> > So, I splitted it up and did a test case to try to figure out what's wrong.
>
> The BeanPropertySetterRule does the invocation with this line of code:
>
>     BeanUtils.setProperty(top, property, bodyText);
>
> Where "top" is the object the property is being set on, "property" is a
> string containing the name of the javabean property and "bodyText" is
> the string to be converted and passed to the property.
>
> Could you try your example on that? If this correctly invokes
> ConvertUtils then there is a problem with Digester. If not, there is a
> problem with BeanUtils (or your converter).
>
> Regards,
>
> Simon
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)

Posted by Simon Kitching <sk...@apache.org>.
On Wed, 2006-01-04 at 18:40 +0100, Alessio Pace wrote:
> Hi, in my last post I was trying to dig into the String to String[]
> conversion with no success.
> 
> So, I splitted it up and did a test case to try to figure out what's wrong.

The BeanPropertySetterRule does the invocation with this line of code:

    BeanUtils.setProperty(top, property, bodyText);

Where "top" is the object the property is being set on, "property" is a
string containing the name of the javabean property and "bodyText" is
the string to be converted and passed to the property.

Could you try your example on that? If this correctly invokes
ConvertUtils then there is a problem with Digester. If not, there is a
problem with BeanUtils (or your converter).

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org