public void testVisibility_protected() throws Exception { assertFalse( Visibility.PROTECTED.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod"))); assertFalse( Visibility.PROTECTED.isVisible( VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod"))); assertTrue( Visibility.PROTECTED.isVisible( VisibilityMethods.class.getDeclaredMethod("protectedMethod"))); assertTrue( Visibility.PROTECTED.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod"))); }
public void verifyBarPass(Method method, TwoArg bar) { try { new NullPointerTester().testMethod(bar, method); } catch (AssertionFailedError incorrectError) { String errorMessage = rootLocaleFormat("Should not have flagged method %s for %s", method.getName(), bar); assertNull(errorMessage, incorrectError); } }
private static void shouldFail(Object instance, Visibility visibility) { try { new NullPointerTester().testInstanceMethods(instance, visibility); } catch (AssertionFailedError expected) { return; } fail("Should detect problem in " + instance.getClass().getSimpleName()); }
public void verifyBarFail(Method method, TwoArg bar) { try { new NullPointerTester().testMethod(bar, method); } catch (AssertionFailedError expected) { return; // good...we wanted a failure } String errorMessage = rootLocaleFormat("Should have flagged method %s for %s", method.getName(), bar); fail(errorMessage); }
public void testTwoArgNormalNullable() throws Exception { Method method = TwoArg.class.getMethod("normalNullable", String.class, Integer.class); for (TwoArg.Action first : TwoArg.Action.values()) { for (TwoArg.Action second : TwoArg.Action.values()) { TwoArg bar = new TwoArg(first, second); if (first.equals(TwoArg.Action.THROW_A_NPE)) { verifyBarPass(method, bar); // only pass if 1st param throws NPE } else { verifyBarFail(method, bar); } } } }
private static void shouldFail(Object instance) { shouldFail(instance, Visibility.PACKAGE); shouldFail(instance, Visibility.PROTECTED); shouldFail(instance, Visibility.PUBLIC); }
private static void shouldPass(Object instance) { shouldPass(instance, Visibility.PACKAGE); shouldPass(instance, Visibility.PROTECTED); shouldPass(instance, Visibility.PUBLIC); }
public void testNonStaticOneArgMethodsThatShouldFail() throws Exception { OneArg foo = new OneArg(); for (String methodName : NONSTATIC_ONE_ARG_METHODS_SHOULD_FAIL) { Method method = OneArg.class.getMethod(methodName, String.class); boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(foo, method, 0); } catch (AssertionFailedError expected) { foundProblem = true; } assertTrue("Should report error in method " + methodName, foundProblem); } }
public void testEqualsMethod() { shouldPass(new OverridesEquals()); shouldFail(new DoesNotOverrideEquals()); }
public void testTwoArgNullableNullable() throws Exception { Method method = TwoArg.class.getMethod("nullableNullable", String.class, Integer.class); for (TwoArg.Action first : TwoArg.Action.values()) { for (TwoArg.Action second : TwoArg.Action.values()) { TwoArg bar = new TwoArg(first, second); verifyBarPass(method, bar); // All args nullable: anything goes! } } }
public void verifyBarPass(Method method, TwoArg bar) { try { new NullPointerTester().testMethod(bar, method); } catch (AssertionFailedError incorrectError) { String errorMessage = String.format( "Should not have flagged method %s for %s", method.getName(), bar); assertNull(errorMessage, incorrectError); } }
public void testTwoArgNullableNormal() throws Exception { Method method = TwoArg.class.getMethod("nullableNormal", String.class, Integer.class); for (TwoArg.Action first : TwoArg.Action.values()) { for (TwoArg.Action second : TwoArg.Action.values()) { TwoArg bar = new TwoArg(first, second); if (second.equals(TwoArg.Action.THROW_A_NPE)) { verifyBarPass(method, bar); // only pass if 2nd param throws NPE } else { verifyBarFail(method, bar); } } } }
private static void shouldFail(Class<?> cls) { shouldFail(cls, Visibility.PACKAGE); }
public void testPassTwoNullableArgsSecondThrowsOther() { shouldPass(new PassTwoNullableArgsSecondThrowsOther()); }
public void testStaticOneArgMethodsThatShouldFail() throws Exception { for (String methodName : STATIC_ONE_ARG_METHODS_SHOULD_FAIL) { Method method = OneArg.class.getMethod(methodName, String.class); boolean foundProblem = false; try { new NullPointerTester().testMethodParameter(new OneArg(), method, 0); } catch (AssertionFailedError expected) { foundProblem = true; } assertTrue("Should report error in method " + methodName, foundProblem); } }
public void testEqualsMethod() { shouldPass(new OverridesEquals()); shouldFail(new DoesNotOverrideEquals()); }
public void verifyBarFail(Method method, TwoArg bar) { try { new NullPointerTester().testMethod(bar, method); } catch (AssertionFailedError expected) { return; // good...we wanted a failure } String errorMessage = rootLocaleFormat("Should have flagged method %s for %s", method.getName(), bar); fail(errorMessage); }
public void testTwoArgNullableNullable() throws Exception { Method method = TwoArg.class.getMethod( "nullableNullable", String.class, Integer.class); for (TwoArg.Action first : TwoArg.Action.values()) { for (TwoArg.Action second : TwoArg.Action.values()) { TwoArg bar = new TwoArg(first, second); verifyBarPass(method, bar); // All args nullable: anything goes! } } }
public void testVisibility_public() throws Exception { assertFalse( Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("privateMethod"))); assertFalse( Visibility.PUBLIC.isVisible( VisibilityMethods.class.getDeclaredMethod("packagePrivateMethod"))); assertFalse( Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("protectedMethod"))); assertTrue( Visibility.PUBLIC.isVisible(VisibilityMethods.class.getDeclaredMethod("publicMethod"))); }
public void testTwoArgNormalNormal() throws Exception { Method method = TwoArg.class.getMethod("normalNormal", String.class, Integer.class); for (TwoArg.Action first : TwoArg.Action.values()) { for (TwoArg.Action second : TwoArg.Action.values()) { TwoArg bar = new TwoArg(first, second); if (first.equals(TwoArg.Action.THROW_A_NPE) && second.equals(TwoArg.Action.THROW_A_NPE)) { verifyBarPass(method, bar); // require both params to throw NPE } else { verifyBarFail(method, bar); } } } }