1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.libraries.mobiledatadownload; 17 18 import static com.google.common.labs.truth.FutureSubject.assertThat; 19 import static com.google.common.util.concurrent.Futures.immediateFailedFuture; 20 import static com.google.common.util.concurrent.Futures.immediateFuture; 21 22 import com.google.android.libraries.mobiledatadownload.DownloadException.DownloadResultCode; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import java.io.IOException; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.robolectric.RobolectricTestRunner; 28 29 /** Tests for {@link DownloadException}. */ 30 @RunWith(RobolectricTestRunner.class) 31 public final class DownloadExceptionTest { 32 33 @Test wrapIfFailed_futureFails_wrapsFailedFutureWithDownloadException()34 public void wrapIfFailed_futureFails_wrapsFailedFutureWithDownloadException() throws Exception { 35 ListenableFuture<Void> future = immediateFailedFuture(new IOException("cause")); 36 ListenableFuture<Void> wrappedFuture = 37 DownloadException.wrapIfFailed( 38 future, DownloadResultCode.ANDROID_DOWNLOADER_HTTP_ERROR, "failed"); 39 assertThat(wrappedFuture).whenDone().isFailedWith(DownloadException.class); 40 assertThat(wrappedFuture).whenDone().hasFailureThat().hasMessageThat().isEqualTo("failed"); 41 assertThat(wrappedFuture) 42 .whenDone() 43 .hasFailureThat() 44 .hasCauseThat() 45 .isInstanceOf(IOException.class); 46 assertThat(wrappedFuture) 47 .whenDone() 48 .hasFailureThat() 49 .hasCauseThat() 50 .hasMessageThat() 51 .isEqualTo("cause"); 52 } 53 54 @Test wrapIfFailed_futureSucceeds_noop()55 public void wrapIfFailed_futureSucceeds_noop() throws Exception { 56 ListenableFuture<Integer> future = immediateFuture(7); 57 ListenableFuture<Integer> wrappedFuture = 58 DownloadException.wrapIfFailed( 59 future, DownloadResultCode.ANDROID_DOWNLOADER_HTTP_ERROR, "failed"); 60 assertThat(wrappedFuture).whenDone().hasValue(7); 61 } 62 } 63