no.abmu.finances.domain.DomainTestHelper.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.finances.domain.DomainTestHelper.java

Source

/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2006 ABM-utvikling                        *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */
package no.abmu.finances.domain;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * DomainTestHelper.
 * 
 * @author Erik Romson, erik@zenior.no
 * @author $Author: jens $
 * @version $Rev: 8483 $
 * @date $Date: 2008-02-13 17:08:06 +0100 (Wed, 13 Feb 2008) $
 * @copyright ABM-Utvikling
 */
public final class DomainTestHelper {

    static final List P_TYPES = new ArrayList();

    private static final Log logger = (Log) LogFactory.getLog(DomainTestHelper.class);

    // Suppress default constructor for noninstantiability
    private DomainTestHelper() {
        // This constructor will never be invoked.
        // See Item 3 on page 12 in
        // Joshua Bloch: Effective Java: Programming Language Guide, 2001
        // ISBN 0-201-31005-8
    }

    static {
        P_TYPES.add(String.class);
        P_TYPES.add(Boolean.TYPE);
        P_TYPES.add(Long.TYPE);
        P_TYPES.add(Integer.TYPE);
        P_TYPES.add(Float.TYPE);
        P_TYPES.add(Date.class);
    }

    static {
        ConvertUtils.deregister(Date.class);
        ConvertUtils.register(new Converter() {
            public Object convert(Class aClass, Object o) {
                if (o instanceof Date) {
                    return o;
                }
                try {
                    return new Date(Long.parseLong((String) o));
                } catch (NumberFormatException e) {
                    return o;
                }
            }
        }, Date.class);
    }

    public static Object populateBean(Object obj)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj);

        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor propertyDescriptor = propertyDescriptors[i];

            if (propertyDescriptor.getName().equals("id")) {
                continue;
            }
            if (P_TYPES.contains(propertyDescriptor.getPropertyType())
                    && propertyDescriptor.getWriteMethod() != null) {

                Object obje;
                obje = ConvertUtils.convert(
                        String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))),
                        propertyDescriptor.getPropertyType());
                PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje);
            }
        }
        return obj;
    }
}