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) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
if (!(obj instanceof EmailAddress)) {
|
||||
return false;
|
||||
}
|
||||
final EmailAddress other = (EmailAddress) obj;
|
||||
|
|
|
|||
|
|
@ -115,8 +115,9 @@ public class GroupMembership implements Serializable {
|
|||
+ "membershipId = %d, "
|
||||
+ "user = %s, "
|
||||
+ "group = %s, "
|
||||
+ " },"
|
||||
+ super.toString(),
|
||||
+ " },",
|
||||
super.toString(),
|
||||
membershipId,
|
||||
Objects.toString(user),
|
||||
Objects.toString(group));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -29,10 +41,44 @@ public class EntitiesTestCore {
|
|||
.withRedefinedSuperclass()
|
||||
.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void verifyToString() throws IntrospectionException,
|
||||
InstantiationException,
|
||||
IllegalAccessException,
|
||||
IllegalArgumentException,
|
||||
InvocationTargetException {
|
||||
final Object obj = entityClass.newInstance();
|
||||
// final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass);
|
||||
//
|
||||
// @Test
|
||||
// public void verifyToString() {
|
||||
//
|
||||
// }
|
||||
// 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()));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue