Java tutorial
/*$Id: DomainTestHelper.java 9131 2008-03-16 23:43:45Z 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.utilh3; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.PropertyUtils; import java.util.List; import java.util.ArrayList; import java.util.Date; import java.lang.reflect.InvocationTargetException; import java.beans.PropertyDescriptor; /** * DomainTestHelper. * * @author Erik Romson, erik@zenior.no * @author $Author: jens $ * @version $Rev: 9131 $ * @date $Date: 2008-03-17 00:43:45 +0100 (Mon, 17 Mar 2008) $ * copyright ABM-Utvikling */ public class DomainTestHelper { private static final Log logger = (Log) LogFactory.getLog(DomainTestHelper.class); static List P_TYPES = new ArrayList(); static { P_TYPES.add(String.class); P_TYPES.add(Boolean.TYPE); P_TYPES.add(Long.TYPE); P_TYPES.add(Integer.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; } 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; } }