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) { if (obj == null) {
return false; return false;
} }
if (getClass() != obj.getClass()) { if (!(obj instanceof EmailAddress)) {
return false; return false;
} }
final EmailAddress other = (EmailAddress) obj; final EmailAddress other = (EmailAddress) obj;

View File

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

View File

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

View File

@ -7,8 +7,20 @@ package org.libreccm.core;
import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning; import nl.jqno.equalsverifier.Warning;
import org.junit.Assert;
import org.junit.Test; 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> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -24,15 +36,49 @@ public class EntitiesTestCore {
@Test @Test
public void verifyEqualsAndHashCode() { public void verifyEqualsAndHashCode() {
EqualsVerifier EqualsVerifier
.forClass(entityClass) .forClass(entityClass)
.suppress(Warning.STRICT_INHERITANCE) .suppress(Warning.STRICT_INHERITANCE)
.withRedefinedSuperclass() .withRedefinedSuperclass()
.verify(); .verify();
} }
//
// @Test @Test
// public void verifyToString() { 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()));
}
}
} }