public void testNulls() throws Exception { new ClassSanityTester() .setDefault(Method.class, FreshValueGeneratorTest.class.getDeclaredMethod("testNulls")) .testNulls(FreshValueGenerator.class); }
private static ClassSanityTester.FactoryMethodReturnValueTester sanityTester() { return new ClassSanityTester() .setDefault(byte[].class, new byte[] {1, 2, 3, 4}) .setDistinctValues(byte[].class, new byte[] {1, 2, 3, 4}, new byte[] {5, 6, 7, 8}) .setDistinctValues(String.class, "7f8005ff0e", "7f8005ff0f") .forAllPublicStaticMethods(HashCode.class); }
doTestEquals(cls); } catch (Exception e) { throwIfUnchecked(e);
/** * Returns dummy factory arguments that are equal to {@code args} but may be different instances, * to be used to construct a second instance of the same equality group. */ private List<Object> generateEqualFactoryArguments( Invokable<?, ?> factory, List<Parameter> params, List<Object> args) throws ParameterNotInstantiableException, FactoryMethodReturnsNullException, InvocationTargetException, IllegalAccessException { List<Object> equalArgs = Lists.newArrayList(args); for (int i = 0; i < args.size(); i++) { Parameter param = params.get(i); Object arg = args.get(i); // Use new fresh value generator because 'args' were populated with new fresh generator each. // Two newFreshValueGenerator() instances should normally generate equal value sequence. Object shouldBeEqualArg = generateDummyArg(param, newFreshValueGenerator()); if (arg != shouldBeEqualArg && Objects.equal(arg, shouldBeEqualArg) && hashCodeInsensitiveToArgReference(factory, args, i, shouldBeEqualArg) && hashCodeInsensitiveToArgReference( factory, args, i, generateDummyArg(param, newFreshValueGenerator()))) { // If the implementation uses identityHashCode(), referential equality is // probably intended. So no point in using an equal-but-different factory argument. // We check twice to avoid confusion caused by accidental hash collision. equalArgs.set(i, shouldBeEqualArg); } } return equalArgs; }
public void testReturnValuesFromAnotherPackageIgnoredForNullTests() throws Exception { new ClassSanityTester().forAllPublicStaticMethods(JdkObjectFactory.class).testNulls(); }
public void testExecutors_nullCheck() throws Exception { new ClassSanityTester() .setDefault(RateLimiter.class, RateLimiter.create(1.0)) .forAllPublicStaticMethods(MoreExecutors.class) .thatReturn(Executor.class) .testNulls(); }
@GwtIncompatible // reflection public void testEquals() throws Exception { new ClassSanityTester() .setDistinctValues(Class.class, Color.class, Gender.class) .setDistinctValues(Enum.class, Color.BLUE, Color.RED) .forAllPublicStaticMethods(EnumMultisetFactory.class) .testEquals(); }
public void testInstantiate_setDefault() throws Exception { NotInstantiable x = new NotInstantiable(); tester.setDefault(NotInstantiable.class, x); assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class)); }
public void testInstantiate_setDistinctValues() throws Exception { NotInstantiable x = new NotInstantiable(); NotInstantiable y = new NotInstantiable(); tester.setDistinctValues(NotInstantiable.class, x, y); assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class)); tester.testEquals(ConstructorParameterMapOfNotInstantiable.class); }
public void testInstantiate_constructorThrows() throws Exception { try { tester.instantiate(ConstructorThrows.class); fail(); } catch (InvocationTargetException expected) { } }
public void testEquals_setOfNonInstantiable() throws Exception { try { new ClassSanityTester().doTestEquals(SetWrapper.class); fail(); } catch (ParameterNotInstantiableException expected) { } }
List<Object> args = Lists.newArrayListWithCapacity(params.size()); for (Parameter param : params) { FreshValueGenerator generator = newFreshValueGenerator(); argGenerators.add(generator); args.add(generateDummyArg(param, generator)); Object instance = createInstance(factory, args); List<Object> equalArgs = generateEqualFactoryArguments(factory, params, args); tester.addEqualityGroup(instance, createInstance(factory, equalArgs)); for (int i = 0; i < params.size(); i++) { List<Object> newArgs = Lists.newArrayList(args); tester.addEqualityGroup(createInstance(factory, newArgs)); argGroups.add(ImmutableList.of(newArgs));
/** * Sets two distinct values for {@code type}. These values can be used for both null pointer * testing and equals testing. * * @since 17.0 */ protected final <T> void setDistinctValues(Class<T> type, T value1, T value2) { tester.setDistinctValues(type, value1, value2); }
public void testEqualsAndSerializableForReturnValues_factoryReturnsNullAndAnnotated() throws Exception { tester .forAllPublicStaticMethods(FactoryThatReturnsNullAndAnnotated.class) .testEqualsAndSerializable(); }
public void testNoNullCheckNeededDespitNotInstantiable() throws Exception { tester.doTestNulls(NoNullCheckNeededDespitNotInstantiable.class, Visibility.PACKAGE); }
/** * Sets the default value for {@code 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 {@link Object#equals} because more than one sample instances are needed for testing * inequality. */ protected final <T> void setDefault(Class<T> type, T value) { tester.setDefault(type, value); }
public void testInstantiate_setDistinctValues() throws Exception { NotInstantiable x = new NotInstantiable(); NotInstantiable y = new NotInstantiable(); tester.setDistinctValues(NotInstantiable.class, x, y); assertNotNull(tester.instantiate(ConstructorParameterNotInstantiable.class)); }
void doTestNulls(Class<?> cls, Visibility visibility) throws ParameterNotInstantiableException, IllegalAccessException, InvocationTargetException, FactoryMethodReturnsNullException { if (!Modifier.isAbstract(cls.getModifiers())) { nullPointerTester.testConstructors(cls, visibility); } nullPointerTester.testStaticMethods(cls, visibility); if (hasInstanceMethodToTestNulls(cls, visibility)) { Object instance = instantiate(cls); if (instance != null) { nullPointerTester.testInstanceMethods(instance, visibility); } } }
private static boolean hashCodeInsensitiveToArgReference( Invokable<?, ?> factory, List<Object> args, int i, Object alternateArg) throws FactoryMethodReturnsNullException, InvocationTargetException, IllegalAccessException { List<Object> tentativeArgs = Lists.newArrayList(args); tentativeArgs.set(i, alternateArg); return createInstance(factory, tentativeArgs).hashCode() == createInstance(factory, args).hashCode(); }
public void testAdapters_nullChecks() throws Exception { new ClassSanityTester() .forAllPublicStaticMethods(JdkFutureAdapters.class) .thatReturn(Future.class) .testNulls(); } }