generic Pojo (getter+setter) testing

This commit is contained in:
Michael Hoennig 2017-04-13 18:59:11 +02:00
parent 30211f65ae
commit f918b70d5e
12 changed files with 229 additions and 6 deletions

View File

@ -91,4 +91,9 @@ public class MemberShare implements Serializable {
this.comment = comment;
}
@Override
public String toString() {
return "MemberShare [id=" + id + ", customer=" + customer.getName() + ", date=" + date + ", action=" + action
+ ", quantity=" + quantity + ", comment=" + comment + "]";
}
}

View File

@ -13,6 +13,8 @@ import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.apache.commons.lang3.StringUtils;
@Table(name="domain_option")
@Entity(name="DomainOption")
@SequenceGenerator(name = "DomainOptionSeqGen", sequenceName = "domain_option_id_seq")
@ -49,14 +51,14 @@ public class DomainOption {
public boolean equals(Object obj) {
if (obj instanceof DomainOption) {
DomainOption opt = (DomainOption) obj;
return getName().equals(opt.getName());
return StringUtils.equals(getName(), opt.getName());
}
return false;
}
@Override
public int hashCode() {
return getName().hashCode();
return (getName()!=null ? getName() : "").hashCode();
}
@Override

View File

@ -95,7 +95,7 @@ public class PacComponent implements Serializable {
@Override
public String toString() {
return "pac=" + pac.getName() + ";comp=" + getBaseComponent().getFeature() + ";quantity=" + getQuantity();
return "pac=" + (pac!=null?pac.getName():"") + ";comp=" + (getBaseComponent()!=null?getBaseComponent().getFeature():"") + ";quantity=" + getQuantity();
}
}

View File

@ -0,0 +1,23 @@
package de.hsadmin.bo.customer;
import static de.hsadmin.test.PojoTestUtil.*;
import org.junit.Test;
import de.hsadmin.test.PojoTestUtil;
public class CustomerPackageBoPojoTest {
@Test
public void ensureProperSetterGetterImplementations() {
PojoTestUtil.ensureProperSetterGetterImplementations("de.hsadmin.bo.customer",
include(de.hsadmin.bo.customer.Contact.class),
include(de.hsadmin.bo.customer.Customer.class),
include(de.hsadmin.bo.customer.IndicatorVAT.class),
include(de.hsadmin.bo.customer.SEPADirectDebit.class),
include(de.hsadmin.bo.customer.MemberShare.class),
include(de.hsadmin.bo.customer.ShareAction.class),
include(de.hsadmin.bo.customer.MemberAsset.class),
include(de.hsadmin.bo.customer.AssetAction.class));
}
}

View File

@ -0,0 +1,36 @@
package de.hsadmin.bo.customer;
import static org.junit.Assert.assertEquals;
import org.joda.time.LocalDate;
import org.junit.Test;
import de.hsadmin.dao.customer.CustomerDaoTest;
import de.hsadmin.test.JpaBasedTest;
public class MemberShareTest extends JpaBasedTest {
@Test
public void testMemberShareAgainstDatabase() {
// given
Customer customer = store(CustomerDaoTest.createNewCustomer(10001, "testCust"));
// when
MemberShare newEntity = store(createNewMemberShare(customer));
MemberShare foundEntity = getEM().find(MemberShare.class, newEntity.getId());
// then
assertEquals(newEntity.toString(), foundEntity.toString());
}
public static MemberShare createNewMemberShare(final Customer customer) {
MemberShare newEntity = new MemberShare();
newEntity.setCustomer(customer);
newEntity.setAction(ShareAction.SUBSCRIPTION);
newEntity.setDate(new LocalDate(2017, 4, 13).toDate());
newEntity.setQuantity(5);
newEntity.setComment("test comment");
return newEntity;
}
}

View File

@ -0,0 +1,18 @@
package de.hsadmin.bo.domain;
import static de.hsadmin.test.PojoTestUtil.*;
import org.junit.Test;
import de.hsadmin.test.PojoTestUtil;
public class DomainPackageBoTest {
@Test
public void ensureProperSetterGetterImplementations() {
PojoTestUtil.ensureProperSetterGetterImplementations("de.hsadmin.bo.domain",
include(de.hsadmin.bo.domain.Domain.class),
include(de.hsadmin.bo.domain.DomainOption.class),
include(de.hsadmin.bo.domain.EMailAddress.class));
}
}

View File

@ -0,0 +1,25 @@
package de.hsadmin.bo.pac;
import static de.hsadmin.test.PojoTestUtil.*;
import org.junit.Test;
import de.hsadmin.test.PojoTestUtil;
public class PacPackageBoPojoTest {
@Test
public void ensureProperSetterGetterImplementations() {
PojoTestUtil.ensureProperSetterGetterImplementations("de.hsadmin.bo.pac",
include(de.hsadmin.bo.pac.Hive.class),
include(de.hsadmin.bo.pac.BasePac.class),
include(de.hsadmin.bo.pac.Pac.class),
include(de.hsadmin.bo.pac.Component.class),
include(de.hsadmin.bo.pac.BaseComponent.class),
include(de.hsadmin.bo.pac.PacComponent.class),
include(de.hsadmin.bo.pac.EMailAlias.class),
include(de.hsadmin.bo.pac.UnixUser.class),
include(de.hsadmin.bo.pac.INetAddress.class),
exclude(de.hsadmin.bo.pac.PacEntityListener.class));
}
}

View File

@ -26,7 +26,7 @@ public class CustomerDaoTest extends JpaBasedTest {
assertEquals(newCustomer.getId(), foundCustomer.getId());
}
private Customer createNewCustomer(int memberNo, String memberCode) {
public static Customer createNewCustomer(int memberNo, String memberCode) {
Customer newCustomer = new Customer();
newCustomer.setMemberNo(memberNo);
newCustomer.setName(memberCode);

View File

@ -0,0 +1,68 @@
package de.hsadmin.test;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.PojoClassFilter;
import com.openpojo.reflection.filters.FilterChain;
import com.openpojo.reflection.filters.FilterClassName;
import com.openpojo.reflection.filters.FilterNestedClasses;
import com.openpojo.reflection.filters.FilterPackageInfo;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.validation.Validator;
import com.openpojo.validation.ValidatorBuilder;
import com.openpojo.validation.test.impl.GetterTester;
import com.openpojo.validation.test.impl.SetterTester;
public class PojoTestUtil {
public static void ensureProperSetterGetterImplementations(final String packageUnderTest, String... expectedClasseNames ) {
ensureExpectedClassesAreTested(packageUnderTest, Arrays.asList(expectedClasseNames) );
Validator validator = ValidatorBuilder.create()
// See com.openpojo.validation.test.impl for more ...
.with(new SetterTester())
.with(new GetterTester())
.build();
validator.validate(packageUnderTest, new FilterPackageInfo());
}
public static String include(Class<?> clazz) {
return clazz.getName();
}
public static String exclude(Class<?> clazz) {
return clazz.getName();
}
private static void ensureExpectedClassesAreTested(final String packageUnderTest, final List<String> expectedClassNames) {
Collections.sort(expectedClassNames);
List<String> pojoClasseNames = new ArrayList<String>();
for ( PojoClass p: PojoClassFactory.getPojoClasses(packageUnderTest,
new FilterChain(new FilterPackageInfo(), new FilterNestedClasses(), new FilterInverse(new FilterClassName(".*Test$"))) ) ) {
pojoClasseNames.add( p.getClazz().getName() );
}
Collections.sort(pojoClasseNames);
assertEquals("precondition failed", expectedClassNames.toString().replaceAll(", ", "\n"), pojoClasseNames.toString().replaceAll(", ", "\n"));
}
private static class FilterInverse implements PojoClassFilter {
private PojoClassFilter delegate;
public FilterInverse(PojoClassFilter delegate) {
this.delegate = delegate;
}
@Override
public boolean include(PojoClass pojoClass) {
return !delegate.include(pojoClass);
}
}
}

View File

@ -4,14 +4,24 @@
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="HSADMIN_H2_TEST_DB" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<!-- auto scanning fails if there are methods returning entities in the test :-( -->
<class>de.hsadmin.bo.customer.Customer</class>
<class>de.hsadmin.bo.customer.Contact</class>
<class>de.hsadmin.bo.customer.SEPADirectDebit</class>
<class>de.hsadmin.bo.pac.INetAddress</class>
<class>de.hsadmin.bo.pac.Hive</class>
<class>de.hsadmin.bo.pac.Pac</class>
<class>de.hsadmin.bo.pac.BasePac</class>
<class>de.hsadmin.bo.pac.Component</class>
<class>de.hsadmin.bo.pac.BaseComponent</class>
<class>de.hsadmin.bo.pac.PacComponent</class>
<class>de.hsadmin.bo.pac.UnixUser</class>
<class>de.hsadmin.bo.customer.MemberShare</class>
<class>de.hsadmin.bo.customer.MemberAsset</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- HSQLDB
@ -32,5 +42,5 @@
<!-- OpenJPA -->
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>
</persistence-unit>
</persistence>

View File

@ -5,6 +5,11 @@ import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.lang3.builder.EqualsBuilder;
import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.common.error.UserError;
@ -62,4 +67,29 @@ public class ReflectionUtil {
}
}
public static Collection<String> getDeclaredFieldNames(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
Collection<String> fieldNames = new ArrayList<String>();
for ( Field f: fields ) {
if ( (f.getModifiers() & Modifier.STATIC) == 0 ) {
fieldNames.add(f.getName());
}
}
return fieldNames;
}
public static boolean reflectionEquals(Object o1, Object o2) {
Collection<String> excludesFieldNames = new ArrayList<String>();
// ignore generated fields, e.g. for OpenJPA proxies
if ( o1.getClass().isSynthetic() ) {
excludesFieldNames.addAll(ReflectionUtil.getDeclaredFieldNames(o1.getClass()));
}
ReflectionUtil.getDeclaredFieldNames(o2.getClass());
if ( o2.getClass().isSynthetic() ) {
excludesFieldNames.addAll(ReflectionUtil.getDeclaredFieldNames(o2.getClass()));
}
return EqualsBuilder.reflectionEquals(o1, 2, excludesFieldNames);
}
}

View File

@ -51,6 +51,12 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.openpojo</groupId>
<artifactId>openpojo</artifactId>
<version>0.8.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>