public abstract class AbstractPackageSanityTests
extends junit.framework.TestCase
AbstractPackageSanityTests
. Currently sanity checks include NullPointerTester
, EqualsTester
and SerializableTester
. For example:
public class PackageSanityTests extends AbstractPackageSanityTests {}
Note that only top-level classes with either a non-private constructor or a non-private static factory method to construct instances can have their instance methods checked. For example:
public class Address { private final String city; private final String state; private final String zipcode; public Address(String city, String state, String zipcode) {...} @Override public boolean equals(Object obj) {...} @Override public int hashCode() {...} ... }
No cascading checks are performed against the return values of methods unless the method is a
static factory method. Neither are semantics of mutation methods such as someList.add(obj)
checked. For more detailed discussion of supported and unsupported cases, see
testEquals()
, testNulls()
and testSerializable()
.
For testing against the returned instances from a static factory class, such as
interface Book {...} public class Books { public static Book hardcover(String title) {...} public static Book paperback(String title) {...} }
please use ClassSanityTester.forAllPublicStaticMethods(java.lang.Class<?>)
.
If not all classes on the classpath should be covered, ignoreClasses(com.google.common.base.Predicate<? super java.lang.Class<?>>)
can be used to
exclude certain classes. As a special case, classes with an underscore in the name (like AutoValue_Foo
) can be excluded using ignoreClasses(
.
UNDERSCORE_IN_NAME
)
setDefault(java.lang.Class<T>, T)
allows subclasses to specify default values for types.
This class incurs IO because it scans the classpath and reads classpath resources.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
AbstractPackageSanityTests.Chopper |
Modifier and Type | Field and Description |
---|---|
private Predicate<java.lang.Class<?>> |
classFilter |
private static ImmutableList<java.lang.String> |
EQUALS_TEST_METHOD_NAMES |
private java.util.logging.Logger |
logger |
private static ImmutableList<java.lang.String> |
NULL_TEST_METHOD_NAMES |
private static ImmutableList<java.lang.String> |
SERIALIZABLE_TEST_METHOD_NAMES |
private static AbstractPackageSanityTests.Chopper |
TEST_SUFFIX |
private ClassSanityTester |
tester |
static Predicate<java.lang.Class<?>> |
UNDERSCORE_IN_NAME
A predicate that matches classes with an underscore in the class name.
|
private NullPointerTester.Visibility |
visibility |
Constructor and Description |
---|
AbstractPackageSanityTests() |
Modifier and Type | Method and Description |
---|---|
(package private) java.util.List<java.lang.Class<?>> |
findClassesToTest(java.lang.Iterable<? extends java.lang.Class<?>> classes,
java.lang.Iterable<java.lang.String> explicitTestNames)
Finds the classes not ending with a test suffix and not covered by an explicit test whose name
is
explicitTestName . |
private static boolean |
hasTest(java.lang.Class<?> testClass,
java.lang.Iterable<java.lang.String> testNames) |
protected void |
ignoreClasses(Predicate<? super java.lang.Class<?>> condition)
Specifies that classes that satisfy the given predicate aren't tested for sanity.
|
private static boolean |
isEqualsDefined(java.lang.Class<?> cls) |
private java.util.List<java.lang.Class<?>> |
loadClassesInPackage() |
protected void |
publicApiOnly()
Restricts the sanity tests for public API only.
|
private static junit.framework.AssertionFailedError |
sanityError(java.lang.Class<?> cls,
java.util.List<java.lang.String> explicitTestNames,
java.lang.String description,
java.lang.Throwable e) |
protected <T> void |
setDefault(java.lang.Class<T> type,
T value)
Sets the default value for
type , when dummy value for a parameter of the same type
needs to be created in order to invoke a method or constructor. |
protected <T> void |
setDistinctValues(java.lang.Class<T> type,
T value1,
T value2)
Sets two distinct values for
type . |
void |
testEquals()
Tests
equals() and hashCode() implementations for every top-level class in the
package, that explicitly implements Object.equals(java.lang.Object) . |
void |
testNulls()
Performs
NullPointerTester checks for all top-level classes in the package. |
void |
testSerializable()
Tests all top-level
Serializable classes in the package. |
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, setUp, tearDown, toString
public static final Predicate<java.lang.Class<?>> UNDERSCORE_IN_NAME
ignoreClasses(com.google.common.base.Predicate<? super java.lang.Class<?>>)
to exclude generated classes, such as the AutoValue_Foo
classes
generated by AutoValue.private static final ImmutableList<java.lang.String> NULL_TEST_METHOD_NAMES
private static final ImmutableList<java.lang.String> SERIALIZABLE_TEST_METHOD_NAMES
private static final ImmutableList<java.lang.String> EQUALS_TEST_METHOD_NAMES
private static final AbstractPackageSanityTests.Chopper TEST_SUFFIX
private final java.util.logging.Logger logger
private final ClassSanityTester tester
private NullPointerTester.Visibility visibility
private Predicate<java.lang.Class<?>> classFilter
protected final void publicApiOnly()
public void testSerializable() throws java.lang.Exception
Serializable
classes in the package. For a serializable Class
C
:
C
explicitly implements Object.equals(java.lang.Object)
, the deserialized instance will
be checked to be equal to the instance before serialization.
C
doesn't explicitly implement equals
but instead inherits it from a
superclass, no equality check is done on the deserialized instance because it's not clear
whether the author intended for the class to be a value type.
AbstractPackageSanityTests
doesn't know how to construct, the test will fail.
C
, C
is skipped for serialization test, even if it implements Serializable
.
C
or C
's subtype.
In all cases, if C
needs custom logic for testing serialization, you can add an
explicit testSerializable()
test in the corresponding CTest
class, and C
will be excluded from automated serialization test performed by this method.
java.lang.Exception
public void testNulls() throws java.lang.Exception
NullPointerTester
checks for all top-level classes in the package. For a class
C
NullPointerTester
) should throw
NullPointerException
.
AbstractPackageSanityTests
doesn't know how to construct, the test will fail.
C
, instance methods are skipped for nulls test.
C
or C
's subtype.
In all cases, if C
needs custom logic for testing nulls, you can add an explicit
testNulls()
test in the corresponding CTest
class, and C
will be
excluded from the automated null tests performed by this method.
java.lang.Exception
public void testEquals() throws java.lang.Exception
equals()
and hashCode()
implementations for every top-level class in the
package, that explicitly implements Object.equals(java.lang.Object)
. For a class C
:
List.add(E)
, or functional update methods such as Joiner.skipNulls()
.
AbstractPackageSanityTests
doesn't know how to construct, the test will fail.
C
, C
is skipped for equality test.
C
or C
's subtype.
In all cases, if C
needs custom logic for testing equals()
, you can add an
explicit testEquals()
test in the corresponding CTest
class, and C
will
be excluded from the automated equals
test performed by this method.
java.lang.Exception
protected final <T> void setDefault(java.lang.Class<T> type, T value)
type
, when dummy value for a parameter of the same type
needs to be created in order to invoke a method or constructor. The default value isn't used in
testing Object.equals(java.lang.Object)
because more than one sample instances are needed for testing
inequality.protected final <T> void setDistinctValues(java.lang.Class<T> type, T value1, T value2)
type
. These values can be used for both null pointer
testing and equals testing.protected final void ignoreClasses(Predicate<? super java.lang.Class<?>> condition)
private static junit.framework.AssertionFailedError sanityError(java.lang.Class<?> cls, java.util.List<java.lang.String> explicitTestNames, java.lang.String description, java.lang.Throwable e)
java.util.List<java.lang.Class<?>> findClassesToTest(java.lang.Iterable<? extends java.lang.Class<?>> classes, java.lang.Iterable<java.lang.String> explicitTestNames)
explicitTestName
.private java.util.List<java.lang.Class<?>> loadClassesInPackage() throws java.io.IOException
java.io.IOException
private static boolean hasTest(java.lang.Class<?> testClass, java.lang.Iterable<java.lang.String> testNames)
private static boolean isEqualsDefined(java.lang.Class<?> cls)