1 package org.apache.commons.digester3;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.apache.commons.beanutils.BeanUtilsBean;
31 import org.apache.commons.beanutils.ConvertUtils;
32 import org.apache.commons.beanutils.ConvertUtilsBean;
33 import org.apache.commons.beanutils.PropertyUtilsBean;
34 import org.junit.Test;
35 import org.xml.sax.SAXException;
36
37
38
39
40 public final class Digester133TestCase
41 {
42
43 public static class MyClass
44 extends HashMap<String, String>
45 {
46 private static final long serialVersionUID = 723339335374093719L;
47
48 private boolean flag = false;
49
50 public boolean isFlag()
51
52 {
53 return flag;
54 }
55
56 public void setFlag( boolean flag )
57
58 {
59 this.flag = flag;
60 }
61 }
62
63 public static class MyPropertyUtilsBean extends PropertyUtilsBean
64 {
65
66 @Override
67 protected Object getPropertyOfMapBean( @SuppressWarnings( "rawtypes" ) Map bean, String propertyName )
68 throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
69 {
70 if ( isReadable( bean, propertyName ) )
71 {
72 return getSimpleProperty( bean, propertyName );
73 }
74 return super.getPropertyOfMapBean( bean, propertyName );
75 }
76
77 @Override
78 protected void setPropertyOfMapBean( @SuppressWarnings( "rawtypes" ) Map bean, String propertyName, Object value )
79 throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
80 {
81 if ( isWriteable( bean, propertyName ) )
82 {
83 Class<?> propertyType = getPropertyType( bean, propertyName );
84 setSimpleProperty( bean, propertyName, ConvertUtils.convert( value, propertyType ) );
85 }
86 else
87 {
88 super.setPropertyOfMapBean( bean, propertyName, value );
89 }
90 }
91
92 }
93
94 @Test
95 public void testDigester()
96 throws IOException, SAXException
97 {
98 PropertyUtilsBean propertyUtils = new MyPropertyUtilsBean();
99 ConvertUtilsBean convertUtils = new ConvertUtilsBean();
100 BeanUtilsBean beanUtils = new BeanUtilsBean( convertUtils, propertyUtils );
101 BeanUtilsBean.setInstance( beanUtils );
102
103 final String xml = "<myclass flag='true' />";
104 final Digester digester = new Digester();
105 digester.addObjectCreate( "myclass", MyClass.class );
106 digester.addSetProperties( "myclass" );
107 final MyClass res = digester.parse( new ByteArrayInputStream( xml.getBytes() ) );
108 assertTrue( res.isFlag() );
109 }
110
111 }