CCM NG: Test for checking toString implementations for null safety

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3441 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2015-05-29 18:19:45 +00:00
parent b082a7037b
commit 11fae39504
4 changed files with 65 additions and 18 deletions

View File

@ -86,7 +86,7 @@ public class EmailAddress implements Serializable {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (!(obj instanceof EmailAddress)) {
return false;
}
final EmailAddress other = (EmailAddress) obj;

View File

@ -77,7 +77,7 @@ public class GroupMembership implements Serializable {
public int hashCode() {
int hash = 3;
hash = 37 * hash
+ (int) (this.membershipId ^ (this.membershipId >>> 32));
+ (int) (this.membershipId ^ (this.membershipId >>> 32));
hash = 37 * hash + Objects.hashCode(this.group);
hash = 37 * hash + Objects.hashCode(this.user);
return hash;
@ -112,11 +112,12 @@ public class GroupMembership implements Serializable {
@Override
public String toString() {
return String.format("%s{ "
+ "membershipId = %d, "
+ "user = %s, "
+ "group = %s, "
+ " },"
+ super.toString(),
+ "membershipId = %d, "
+ "user = %s, "
+ "group = %s, "
+ " },",
super.toString(),
membershipId,
Objects.toString(user),
Objects.toString(group));
}

View File

@ -23,7 +23,7 @@ import org.libreccm.tests.categories.UnitTest;
@Category(UnitTest.class)
public class CcmCoreEntitiesTest extends EntitiesTestCore {
@Parameterized.Parameters
@Parameterized.Parameters(name = "{0}")
public static Collection<Class<?>> data() {
return Arrays.asList(new Class<?>[]{
CcmObject.class,

View File

@ -7,8 +7,20 @@ package org.libreccm.core;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.Assert;
import org.junit.Test;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -24,15 +36,49 @@ public class EntitiesTestCore {
@Test
public void verifyEqualsAndHashCode() {
EqualsVerifier
.forClass(entityClass)
.suppress(Warning.STRICT_INHERITANCE)
.withRedefinedSuperclass()
.verify();
.forClass(entityClass)
.suppress(Warning.STRICT_INHERITANCE)
.withRedefinedSuperclass()
.verify();
}
//
// @Test
// public void verifyToString() {
//
// }
@Test
public void verifyToString() throws IntrospectionException,
InstantiationException,
IllegalAccessException,
IllegalArgumentException,
InvocationTargetException {
final Object obj = entityClass.newInstance();
// final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
//
// final PropertyDescriptor[] properties = beanInfo
// .getPropertyDescriptors();
// for (PropertyDescriptor property : properties) {
// final Method setter = property.getWriteMethod();
// setter.invoke(obj, new Object[]{null});
final Field[] fields = entityClass.getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers())
&& !field.getType().isPrimitive()) {
field.setAccessible(true);
field.set(obj, null);
}
}
try {
obj.toString();
} catch (NullPointerException ex) {
final StringWriter strWriter = new StringWriter();
final PrintWriter writer = new PrintWriter(strWriter);
ex.printStackTrace(writer);
Assert.fail(String.format(
"toString() implemention of class \"%s\" "
+ "is not null safe:\n %s",
entityClass.getName(),
strWriter.toString()));
}
}
}