ar.com.zauber.commons.mom.test.CxfStyleUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.zauber.commons.mom.test.CxfStyleUnitTest.java

Source

/**
 * Copyright (c) 2009-2011 Zauber S.A. <http://www.zaubersoftware.com/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Copyright (c) 2011 Zauber S.A.  -- All rights reserved
 */

package ar.com.zauber.commons.mom.test;

import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.LazyDynaMap;
import org.apache.commons.beanutils.WrapDynaBean;
import org.junit.Test;

import ar.com.zauber.commons.mom.CXFStyle;

/**
 * TODO: Description of the class, Comments in english by default
 * 
 * @author Gaston Ponti
 * @since Sep 7, 2011
 */

public class CxfStyleUnitTest {

    private BeanUtilsBean beanUtils = new BeanUtilsBean();

    @Test
    public void canConvertSimpleBean() throws Exception {
        assertCanConvert(new BeanStyleObject(), "stringProperty", newMap());
    }

    @Test
    public void canConvertListBean() throws Exception {
        assertCanConvert(new BeanStyleObject(), "listProperty", newMap());
    }

    @Test
    public void canConvertEnumBean() throws Exception {
        assertCanConvert(new BeanStyleObject(), "enumProperty", newMap());
    }

    @Test
    public void canConvertEnumCxf() throws Exception {
        assertCanConvert(new CxfStyleObject(), "enumProperty", newMap());
    }

    @Test
    public void canConvertStringCxf() throws Exception {
        assertCanConvert(new CxfStyleObject(), "stringProperty", newMap());
    }

    @Test
    public void canConvertListCxf() throws Exception {
        assertCanConvert(new CxfStyleObject(), "listProperty", newMap());
    }

    @Test
    public void canConvertArrayBean() throws Exception {
        assertCanConvert(new BeanStyleObject(), "arrayProperty", newMap());
    }

    @Test
    public void canConvertListOfMapsCxf() throws Exception {
        assertCanConvert(new CxfStyleObject(), "otherListProperty", newMap());
    }

    public HashMap<String, Object> newMap() {
        return new HashMap<String, Object>() {
            {
                put("stringProperty", "hola");
                put("enumProperty", TimeUnit.DAYS);
                put("listProperty", Arrays.asList(10, 0));
                put("arrayProperty", Arrays.asList(10, 0));
                put("otherListProperty", Arrays.<Map<String, Object>>asList(new HashMap<String, Object>() {
                    {
                        put("cant", 10);
                        put("name", "foo");
                    }
                }));
            }
        };
    }

    /**
     * @param target
     * @param name
     * @param source
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public void assertCanConvert(Object target, String name, HashMap<String, Object> source) throws Exception {
        assertTrue(CXFStyle.STYLE.isSeteable(beanUtils, source.get(name), target, name));
    }

    /**
     * TODO: Description of the class, Comments in english by default
     * 
     * @author Gaston Ponti
     * @since Sep 7, 2011
     */

    @SuppressWarnings("unused")
    public static final class BeanStyleObject {
        private String stringProperty;
        private TimeUnit enumProperty;
        private List<Integer> listProperty;
        private int[] arrayProperty;

        /**
         * Sets the enumProperty.
         * 
         * @param enumProperty with the enumProperty.
         */
        public void setEnumProperty(TimeUnit enumProperty) {
            this.enumProperty = enumProperty;
        }

        /**
         * Sets the listProperty.
         * 
         * @param listProperty with the listProperty.
         */
        public void setListProperty(List<Integer> listProperty) {
            this.listProperty = listProperty;
        }

        /**
         * Sets the stringProperty.
         * 
         * @param stringProperty with the stringProperty.
         */
        public void setStringProperty(String stringProperty) {
            this.stringProperty = stringProperty;
        }

        /**
         * Sets the arrayProperty. 
         *
         * @param arrayProperty  with the arrayProperty.
         */
        public void setArrayProperty(int[] arrayProperty) {
            this.arrayProperty = arrayProperty;
        }

        public String getStringProperty() {
            return stringProperty;
        }
    }

}