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-94f89814c4dfpull/2/head
parent
b082a7037b
commit
11fae39504
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -115,8 +115,9 @@ public class GroupMembership implements Serializable {
|
||||||
+ "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));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -29,10 +41,44 @@ public class EntitiesTestCore {
|
||||||
.withRedefinedSuperclass()
|
.withRedefinedSuperclass()
|
||||||
.verify();
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyToString() throws IntrospectionException,
|
||||||
|
InstantiationException,
|
||||||
|
IllegalAccessException,
|
||||||
|
IllegalArgumentException,
|
||||||
|
InvocationTargetException {
|
||||||
|
final Object obj = entityClass.newInstance();
|
||||||
|
// final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
|
||||||
//
|
//
|
||||||
// @Test
|
// final PropertyDescriptor[] properties = beanInfo
|
||||||
// public void verifyToString() {
|
// .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()));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue