View Javadoc

1   package org.apache.commons.digester3;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * DIGESTER-133
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 }