commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alessio Pace <alessio.p...@gmail.com>
Subject [Digester] Digester does not seem to me to make use of StringArrayConverter (Test case here)
Date Wed, 04 Jan 2006 17:40:13 GMT
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


Mime
View raw message