@GwtIncompatible("Reflection") public void testAllOverloads_checkArgument() throws Exception { for (ImmutableList<Class<?>> sig : allSignatures(boolean.class)) { Method checkArgumentMethod = Preconditions.class.getMethod("checkArgument", sig.toArray(new Class<?>[] {})); checkArgumentMethod.invoke(null /* static method */, getParametersForSignature(true, sig)); Object[] failingParams = getParametersForSignature(false, sig); try { checkArgumentMethod.invoke(null /* static method */, failingParams); fail(); } catch (InvocationTargetException ite) { assertFailureCause(ite.getCause(), IllegalArgumentException.class, failingParams); } } }
public void testCheckPositionIndex_ok() { assertEquals(0, Preconditions.checkPositionIndex(0, 0)); assertEquals(0, Preconditions.checkPositionIndex(0, 1)); assertEquals(1, Preconditions.checkPositionIndex(1, 1)); }
public void testCheckNotNull_simple_success() { String result = Preconditions.checkNotNull(NON_NULL_STRING); assertSame(NON_NULL_STRING, result); }
public void testCheckArgument_complexMessage_failure() { try { Preconditions.checkArgument(false, FORMAT, 5); fail("no exception thrown"); } catch (IllegalArgumentException expected) { verifyComplexMessage(expected); } }
public void testCheckNotNull_simpleMessage_failure() { try { Preconditions.checkNotNull(null, new Message()); fail("no exception thrown"); } catch (NullPointerException expected) { verifySimpleMessage(expected); } }
public void testCheckState_simple_failure() { try { Preconditions.checkState(false); fail("no exception thrown"); } catch (IllegalStateException expected) { } }
public void testCheckArgument_simple_failure() { try { Preconditions.checkArgument(false); fail("no exception thrown"); } catch (IllegalArgumentException expected) { } }
public void testCheckState_simpleMessage_failure() { try { Preconditions.checkState(false, new Message()); fail("no exception thrown"); } catch (IllegalStateException expected) { verifySimpleMessage(expected); } }
public void testCheckState_complexMessage_failure() { try { Preconditions.checkState(false, FORMAT, 5); fail("no exception thrown"); } catch (IllegalStateException expected) { verifyComplexMessage(expected); } }
@GwtIncompatible("Reflection") public void testAllOverloads_checkState() throws Exception { for (ImmutableList<Class<?>> sig : allSignatures(boolean.class)) { Method checkArgumentMethod = Preconditions.class.getMethod("checkState", sig.toArray(new Class<?>[] {})); checkArgumentMethod.invoke(null /* static method */, getParametersForSignature(true, sig)); Object[] failingParams = getParametersForSignature(false, sig); try { checkArgumentMethod.invoke(null /* static method */, failingParams); fail(); } catch (InvocationTargetException ite) { assertFailureCause(ite.getCause(), IllegalStateException.class, failingParams); } } }
public void testCheckNotNull_simple_failure() { try { Preconditions.checkNotNull(null); fail("no exception thrown"); } catch (NullPointerException expected) { } }
public void testCheckArgument_simpleMessage_failure() { try { Preconditions.checkArgument(false, new Message()); fail("no exception thrown"); } catch (IllegalArgumentException expected) { verifySimpleMessage(expected); } }
public void testCheckNotNull_complexMessage_failure() { try { Preconditions.checkNotNull(null, FORMAT, 5); fail("no exception thrown"); } catch (NullPointerException expected) { verifyComplexMessage(expected); } }
public void testCheckNotNull_simpleMessage_success() { String result = Preconditions.checkNotNull(NON_NULL_STRING, IGNORE_ME); assertSame(NON_NULL_STRING, result); }
public void testCheckElementIndex_ok() { assertEquals(0, Preconditions.checkElementIndex(0, 1)); assertEquals(0, Preconditions.checkElementIndex(0, 2)); assertEquals(1, Preconditions.checkElementIndex(1, 2)); }
@GwtIncompatible("Reflection") public void testAllOverloads_checkNotNull() throws Exception { for (ImmutableList<Class<?>> sig : allSignatures(Object.class)) { Method checkArgumentMethod = Preconditions.class.getMethod("checkNotNull", sig.toArray(new Class<?>[] {})); checkArgumentMethod.invoke( null /* static method */, getParametersForSignature(new Object(), sig)); Object[] failingParams = getParametersForSignature(null, sig); try { checkArgumentMethod.invoke(null /* static method */, failingParams); fail(); } catch (InvocationTargetException ite) { assertFailureCause(ite.getCause(), NullPointerException.class, failingParams); } } }
public void testCheckPositionIndex_badSize() { try { Preconditions.checkPositionIndex(1, -1); fail(); } catch (IllegalArgumentException expected) { // don't care what the message text is, as this is an invalid usage of // the Preconditions class, unlike all the other exceptions it throws } }
public void testCheckState_simpleMessage_failure() { try { Preconditions.checkState(false, new Message()); fail("no exception thrown"); } catch (IllegalStateException expected) { verifySimpleMessage(expected); } }
public void testCheckArgument_complexMessage_failure() { try { Preconditions.checkArgument(false, FORMAT, 5); fail("no exception thrown"); } catch (IllegalArgumentException expected) { verifyComplexMessage(expected); } }
public void testCheckNotNull_complexMessage_success() { String result = Preconditions.checkNotNull(NON_NULL_STRING, "%s", IGNORE_ME); assertSame(NON_NULL_STRING, result); }