• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.file.openers;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 
23 import android.net.Uri;
24 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
25 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri;
26 import com.google.android.libraries.mobiledatadownload.file.spi.Backend;
27 import java.io.BufferedInputStream;
28 import java.io.ByteArrayInputStream;
29 import java.io.InputStream;
30 import java.util.Arrays;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.RobolectricTestRunner;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public final class ReadStreamOpenerTest {
39 
40   private final InputStream backendStream = new ByteArrayInputStream(new byte[1]);
41   private SynchronousFileStorage storage;
42 
43   @Rule public TemporaryUri tmpUri = new TemporaryUri();
44 
45   @Before
setUpStorage()46   public void setUpStorage() throws Exception {
47     Backend mockBackend = mock(Backend.class);
48     when(mockBackend.name()).thenReturn("file");
49     when(mockBackend.openForRead(any())).thenReturn(backendStream);
50 
51     storage = new SynchronousFileStorage(Arrays.asList(mockBackend));
52   }
53 
54   @Test
open_withoutTransforms_returnsRawBackendStream()55   public void open_withoutTransforms_returnsRawBackendStream() throws Exception {
56     Uri uri = tmpUri.newUri();
57 
58     try (InputStream result = storage.open(uri, ReadStreamOpener.create())) {
59       assertThat(result).isEqualTo(backendStream);
60     }
61   }
62 
63   @Test
open_withBufferedIo_returnsBufferedInputStream()64   public void open_withBufferedIo_returnsBufferedInputStream() throws Exception {
65     Uri uri = tmpUri.newUri();
66 
67     try (InputStream result = storage.open(uri, ReadStreamOpener.create().withBufferedIo())) {
68       assertThat(result).isInstanceOf(BufferedInputStream.class);
69     }
70   }
71 }
72