package com.elab.core.dao.row;

import com.db.service.model.TTest;
import org.junit.Test;
import org.springframework.beans.*;

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

public class JoinRowMapperTest {

    @Test
    public void mapRow() {
        TTest test = new TTest();
        test.setTestId("1");
        test.setUsername("asdfasd");
        test.setId(1);
        test.setName("ggg");
        test.setGirlName("asdfasd");
        test.setGirlStatus("aa");
        test.setTime(new Date());
        test.setSex("man");
        int count = 100000;
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            TTest fanshe = fanshe(test);
            if (i == 0) {
                System.out.println(fanshe.toString());
            }
        }
        long time = System.currentTimeMillis() - start;
        System.out.println(time);


    }

    private TTest fanshe(TTest test) {
        TTest mappedObject = BeanUtils.instantiate(TTest.class);

        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);

        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(TTest.class);

        for (int index = 0; index <= propertyDescriptors.length - 1; index++) {
            PropertyDescriptor pd = propertyDescriptors[index];
            String column = pd.getName();
            if (pd != null) {
                try {
                    Object value = org.apache.commons.beanutils.BeanUtils.getProperty(test, column);
                    try {
                        bw.setPropertyValue(pd.getName(), value);
                    } catch (TypeMismatchException e) {

                    }
                } catch (NotWritablePropertyException ex) {

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
        return mappedObject;
    }
}