/** * Asserts that {@code runnable} throws an exception of type {@code expectedThrowable} when * executed. If it does, the exception object is returned. If it does not throw an exception, an * {@link AssertionError} is thrown. If it throws the wrong type of exception, an {@code * AssertionError} is thrown describing the mismatch; the exception that was actually thrown can * be obtained by calling {@link AssertionError#getCause}. * * @param expectedThrowable the expected type of the exception * @param runnable a function that is expected to throw an exception when executed * @return the exception thrown by {@code runnable} * @since 4.13 */ public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) { return assertThrows(null, expectedThrowable, runnable); }
/** * Adds a failure to the table if {@code runnable} does not throw an * exception of type {@code expectedThrowable} when executed. * Execution continues, but the test will fail at the end if the runnable * does not throw an exception, or if it throws a different exception. * * @param expectedThrowable the expected type of the exception * @param runnable a function that is expected to throw an exception when executed * @since 4.13 */ public void checkThrows(Class<? extends Throwable> expectedThrowable, ThrowingRunnable runnable) { try { assertThrows(expectedThrowable, runnable); } catch (AssertionError e) { addError(e); } }
@Test public void write_throwsIfLengthAndOffsetsIsGreaterThanLength() { assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { os.write(new byte[1], /*initialOffset=*/ 1, /*length=*/ 1); } }); }
@Test public void write_throwsIfLengthIsLessThanZero() { assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { os.write(new byte[0], /*initialOffset=*/ 0, /*length=*/ -1); } }); }
@Test public void write_throwsIfOffsetIsGreaterThanLength() { assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { os.write(new byte[0], /*initialOffset=*/ 1, /*length=*/ 0); } }); }
@Test public void write_throwsIfOffsetIsLessThanZero() { assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { os.write(new byte[0], /*initialOffset=*/ -1, /*length=*/ 0); } }); }
@Test public void write_throwsIfLengthsIsGreaterThanLength() { assertThrows(IndexOutOfBoundsException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { os.write(new byte[0], /*initialOffset=*/ 0, /*length=*/ 1); } }); }
@Test public void load_withShapeDrawableResourceId_asDrawable_withTransformation_sizeOriginal_fails() throws ExecutionException, InterruptedException { assertThrows( ExecutionException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { Glide.with(context) .load(ResourceIds.drawable.shape_drawable) .apply(centerCropTransform()) .submit() .get(); } }); }
@Test public void load_withShapeDrawableResourceId_asBitmap_withSizeOriginal_fails() throws ExecutionException, InterruptedException { assertThrows( ExecutionException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { Glide.with(context) .asBitmap() .load(ResourceIds.drawable.shape_drawable) .submit() .get(); } }); }
@Test public void load_withColorDrawable_sizeOriginal_requiredTransform_fails() throws ExecutionException, InterruptedException { final Drawable colorDrawable = new ColorDrawable(Color.RED); assertThrows( ExecutionException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { Glide.with(context) .load(colorDrawable) .apply(new RequestOptions() .centerCrop()) .submit() .get(); } }); }
@Test public void testBuild_withModelAndDataClasses_excludesModelLoadersForOtherDataClasses() { multiFactory.append(String.class, String.class, firstFactory); assertThrows( NoModelLoaderAvailableException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { multiFactory.build(String.class, Integer.class); } }); }
@Test public void testBuild_withModelAndDataClass_doesNotMatchSubclassesOfModelClass() { appendFactoryFor(String.class, Object.class); assertThrows( NoModelLoaderAvailableException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { multiFactory.build(Object.class, Object.class); } }); }
@Test public void testBuild_withModelAndDataClass_doesNotMatchSubclassesOfDataClass() { appendFactoryFor(Object.class, String.class); assertThrows( NoModelLoaderAvailableException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { multiFactory.build(Object.class, Object.class); } }); }
@Test public void testBuild_withModelAndDataClasses_excludesModelLoadersForOtherModelClasses() { multiFactory.append(String.class, String.class, firstFactory); assertThrows( NoModelLoaderAvailableException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { multiFactory.build(Integer.class, String.class); } }); }
assertThrows( RuntimeException.class, new ThrowingRunnable() {
@Test public void updateDiskCacheKey_throwsException() throws NoSuchAlgorithmException { // If this test fails, update testEqualsAndHashcode to use KeyTester including regression tests. final EngineKey key = new EngineKey( "id", new ObjectKey("signature"), 100, 100, Collections.<Class<?>, Transformation<?>>emptyMap(), Object.class, Object.class, new Options()); assertThrows( UnsupportedOperationException.class, new ThrowingRunnable() { @Override public void run() throws NoSuchAlgorithmException { key.updateDiskCacheKey(MessageDigest.getInstance("SHA-1")); } }); }
@Test public void fitCenter_withHugeRectangle_throwsOOM() throws ExecutionException, InterruptedException { float multiplier = Integer.MAX_VALUE / (canonical.getWidth() * canonical.getHeight() * 2); final int overrideWidth = (int) multiplier * canonical.getWidth(); final int overrideHeight = (int) multiplier * canonical.getHeight(); assertThrows( ExecutionException.class, new ThrowingRunnable() { @Override public void run() throws Throwable { GlideApp .with(context) .asBitmap() .load(canonical.getBitmap()) .fitCenter() .override(overrideWidth, overrideHeight) .submit() .get(); } }); } }
@Test public void testConstructorNullCredential() { assertThrows( NullPointerException.class, () -> new RetryHttpInitializer(null, "foo-user-agent")); }
@Test public void exceptionIsThrownForNoServiceAccountEmail() throws IOException, GeneralSecurityException { // No email set, keyfile doesn't exist, but that's OK. configuration.setServiceAccountKeyFile("aFile"); assertThrows(IllegalStateException.class, () -> configuration.getCredential(TEST_SCOPES)); }
@Test public void testValidCallHasNoRetries() throws Exception { IllegalArgumentException thrown = assertThrows( IllegalArgumentException.class, () -> new RetryBoundedBackOff(-7, new BackOffTester())); assertThat(thrown) .hasMessageThat() .contains("Maximum number of retries must not be less than 0."); }