001package org.apache.commons.digester3;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.assertTrue;
023
024import java.io.ByteArrayInputStream;
025import java.io.IOException;
026import java.lang.reflect.InvocationTargetException;
027import java.util.HashMap;
028import java.util.Map;
029
030import org.apache.commons.beanutils.BeanUtilsBean;
031import org.apache.commons.beanutils.ConvertUtils;
032import org.apache.commons.beanutils.ConvertUtilsBean;
033import org.apache.commons.beanutils.PropertyUtilsBean;
034import org.junit.Test;
035import org.xml.sax.SAXException;
036
037/**
038 * DIGESTER-133
039 */
040public final class Digester133TestCase
041{
042
043    public static class MyClass
044        extends HashMap<String, String>
045    {
046        private static final long serialVersionUID = 723339335374093719L;
047
048        private boolean flag = false;
049
050        public boolean isFlag()
051
052        {
053            return flag;
054        }
055
056        public void setFlag( boolean flag )
057
058        {
059            this.flag = flag;
060        }
061    }
062
063    public static class MyPropertyUtilsBean extends PropertyUtilsBean
064    {
065
066        @Override
067        protected Object getPropertyOfMapBean( @SuppressWarnings( "rawtypes" ) Map bean, String propertyName )
068            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
069        {
070            if ( isReadable( bean, propertyName ) )
071            {
072                return getSimpleProperty( bean, propertyName );
073            }
074            return super.getPropertyOfMapBean( bean, propertyName );
075        }
076
077        @Override
078        protected void setPropertyOfMapBean( @SuppressWarnings( "rawtypes" ) Map bean, String propertyName, Object value )
079            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
080        {
081            if ( isWriteable( bean, propertyName ) )
082            {
083                Class<?> propertyType = getPropertyType( bean, propertyName );
084                setSimpleProperty( bean, propertyName, ConvertUtils.convert( value, propertyType ) );
085            }
086            else
087            {
088                super.setPropertyOfMapBean( bean, propertyName, value );
089            }
090        }
091
092    }
093
094    @Test
095    public void testDigester()
096        throws IOException, SAXException
097    {
098        PropertyUtilsBean propertyUtils = new MyPropertyUtilsBean();
099        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}