Java tutorial
/*$Id: DomainTestHelper.java 13547 2009-03-26 14:42:50Z jens $*/ /* **************************************************************************** * * * (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.test.domainmodels; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; 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; /** * DomainTestHelper. * * @author Erik Romson, erik@zenior.no * @author $Author: jens $ * @version $Rev: 13547 $ * @date $Date: 2009-03-26 15:42:50 +0100 (Thu, 26 Mar 2009) $ * copyright ABM-Utvikling */ public final class DomainTestHelper { static final List<Class<?>> P_TYPES = new ArrayList<Class<?>>(); @SuppressWarnings("unused") private static final Log logger = (Log) LogFactory.getLog(DomainTestHelper.class); private DomainTestHelper() { } static { P_TYPES.add(String.class); P_TYPES.add(Long.class); P_TYPES.add(Boolean.TYPE); P_TYPES.add(Long.TYPE); P_TYPES.add(Integer.TYPE); P_TYPES.add(Date.class); P_TYPES.add(BigDecimal.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; } else if (propertyDescriptor.getName().equals("valueAsString")) { continue; } else if (P_TYPES.contains(propertyDescriptor.getPropertyType()) && propertyDescriptor.getWriteMethod() != null) { Object obje; // obje=ConvertUtils.convert(String.valueOf(System.currentTimeMillis() // +(long)(Math.random()*100d)),propertyDescriptor.getPropertyType()); obje = ConvertUtils.convert( String.valueOf((int) (System.currentTimeMillis() + (int) (Math.random() * 100d))), propertyDescriptor.getPropertyType()); PropertyUtils.setProperty(obj, propertyDescriptor.getName(), obje); } } return obj; } }