Search in sources :

Example 1 with AbstractHibernateObject

use of org.candlepin.model.AbstractHibernateObject in project candlepin by candlepin.

the class CandlepinDTOTest method testPopulateWithEntity.

@Test
@Parameters(method = "getValuesPopulationByEntity")
public void testPopulateWithEntity(String valueName, Object input, Object defaultValue) throws Exception {
    Method accessor = null;
    Method mutator = null;
    try {
        accessor = CandlepinDTO.class.getDeclaredMethod("get" + valueName, null);
    } catch (NoSuchMethodException e) {
        accessor = CandlepinDTO.class.getDeclaredMethod("is" + valueName, null);
    }
    try {
        mutator = AbstractHibernateObject.class.getDeclaredMethod("set" + valueName, input.getClass());
    } catch (NoSuchMethodException e) {
        if (Collection.class.isAssignableFrom(input.getClass())) {
            mutator = AbstractHibernateObject.class.getDeclaredMethod("set" + valueName, Collection.class);
        } else if (Boolean.class.isAssignableFrom(input.getClass())) {
            mutator = AbstractHibernateObject.class.getDeclaredMethod("set" + valueName, boolean.class);
        } else {
            throw e;
        }
    }
    CandlepinDTO base = new CandlepinDTOImpl();
    AbstractHibernateObject source = new HibernateObjectImpl();
    mutator.invoke(source, input);
    base.populate(source);
    // Verify only the specified field was set
    for (Method method : CandlepinDTO.class.getDeclaredMethods()) {
        if (method.getName().matches("^(get|is)\\w+")) {
            Object output = method.invoke(base, null);
            if (method.getName().equals(accessor.getName())) {
                if (input instanceof Collection) {
                    assertTrue(output instanceof Collection);
                    assertTrue(Util.collectionsAreEqual((Collection) input, (Collection) output));
                } else {
                    assertEquals(input, output);
                }
            } else {
                for (Object[] values : this.getValuesPopulationByEntity()) {
                    if (method.getName().endsWith((String) values[0])) {
                        if (values[2] instanceof Collection) {
                            assertTrue(output instanceof Collection);
                            assertTrue(Util.collectionsAreEqual((Collection) values[2], (Collection) output));
                        } else {
                            assertEquals(values[2], output);
                        }
                    }
                }
            }
        }
    }
}
Also used : AbstractHibernateObject(org.candlepin.model.AbstractHibernateObject) Collection(java.util.Collection) AbstractHibernateObject(org.candlepin.model.AbstractHibernateObject) Method(java.lang.reflect.Method) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Parameters (junitparams.Parameters)1 AbstractHibernateObject (org.candlepin.model.AbstractHibernateObject)1 Test (org.junit.Test)1