Giter Site home page Giter Site logo

ec2filestest's Introduction

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Timestamp; import java.util.Date; import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("rawtypes") public class BaseSetterGetterTest {

private final Class[] clazz = new Class[]{ 
    /* YOUR POJO CLASS HERE */
    UserResponse.class, ProductEntity.class, ProductDTO.class
};

@SuppressWarnings("unchecked")
public void testSetterGetter(Class[] clazz) {

    for (Class aClass : clazz) {
        Object instance;
        try {
            instance = aClass.getDeclaredConstructor().newInstance();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field f : declaredFields) {
                try {
                    String fieldName = f.getName();
                    if (fieldName.equals("serialVersionUID")) {
                        continue;
                    }
                    String name = StringUtils.capitalize(fieldName);
                    String getter = "get" + name;
                    String setter = "set" + name;
                    Object value = null;
                    Class<?> fType = f.getType();
                    if (fType.isAssignableFrom(String.class)) {
                        value = "str";
                    } else if (fType.isAssignableFrom(Long.class) || fType.isAssignableFrom(long.class)) {
                        value = Long.valueOf("1");
                    } else if (fType.isAssignableFrom(Integer.class) || fType.isAssignableFrom(int.class)) {
                        value = Integer.valueOf("1");
                    } else if (fType.isAssignableFrom(Double.class) || fType.isAssignableFrom(double.class)) {
                        value = Double.valueOf("1.0");
                    } else if (fType.isAssignableFrom(Date.class)) {
                        value = new Date();
                    } else if (fType.isAssignableFrom(Timestamp.class)) {
                        value = new Timestamp(new Date().getTime());
                    } else if (fType.isAssignableFrom(Boolean.class)) {
                        value = Boolean.FALSE;
                    } else if (fType.isAssignableFrom(boolean.class)) {
                        value = Boolean.FALSE;
                        getter = "is" + name;
                    } // end the rest Type you expect, such as Collection
                    Method getterMethod = aClass.getMethod(getter);
                    Method setterMethod = aClass.getMethod(setter, getterMethod.getReturnType());
                    setterMethod.invoke(instance, value);
                    Object result = getterMethod.invoke(instance);
                    assertThat(result).as("in class %s and fields %s ", aClass.getName(), fieldName).isEqualTo(value);
                } catch (IllegalArgumentException e) {
                    System.out.println("aClass = " + aClass);
                    e.printStackTrace();
                    throw e;
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
            // toString test
            assertThat(instance.toString()).as("toString in class %s ", aClass.getName()).isNotBlank();

            // equals n hashCode test
            Set<Object> set = Set.of(instance);
            assertThat(set.contains(instance)).as("equals n hashCode in class %s ", aClass.getName()).isTrue();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

}

ec2filestest's People

Contributors

vh1ne avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.