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.backends; 17 18 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.makeArrayOfBytesContent; 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.net.Uri; 22 import com.google.android.libraries.mobiledatadownload.file.common.FileChannelConvertible; 23 import com.google.android.libraries.mobiledatadownload.file.common.LockScope; 24 import com.google.android.libraries.mobiledatadownload.file.common.Sizable; 25 import com.google.android.libraries.mobiledatadownload.file.common.testing.BackendTestBase; 26 import com.google.android.libraries.mobiledatadownload.file.spi.Backend; 27 import com.google.common.collect.ImmutableList; 28 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.io.OutputStream; 32 import java.util.List; 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.rules.TemporaryFolder; 36 import org.junit.runner.RunWith; 37 38 @RunWith(GoogleRobolectricTestRunner.class) 39 public final class JavaFileBackendTest extends BackendTestBase { 40 41 private Backend backend = new JavaFileBackend(); 42 43 @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); 44 45 @Test openForRead_shouldImplementFileChannelConvertible()46 public void openForRead_shouldImplementFileChannelConvertible() throws IOException { 47 backend.openForWrite(uriForTestMethod()).close(); 48 try (InputStream inputStream = backend.openForRead(uriForTestMethod())) { 49 assertThat(inputStream).isInstanceOf(FileChannelConvertible.class); 50 assertThat(((FileChannelConvertible) inputStream).toFileChannel().position()).isEqualTo(0); 51 } 52 } 53 54 @Test openForWrite_shouldImplementFileChannelConvertible()55 public void openForWrite_shouldImplementFileChannelConvertible() throws IOException { 56 try (OutputStream outputStream = backend.openForWrite(uriForTestMethod())) { 57 assertThat(outputStream).isInstanceOf(FileChannelConvertible.class); 58 assertThat(((FileChannelConvertible) outputStream).toFileChannel().position()).isEqualTo(0); 59 } 60 } 61 62 @Test openForAppend_shouldImplementFileChannelConvertible()63 public void openForAppend_shouldImplementFileChannelConvertible() throws IOException { 64 try (OutputStream outputStream = backend.openForAppend(uriForTestMethod())) { 65 assertThat(outputStream).isInstanceOf(FileChannelConvertible.class); 66 assertThat(((FileChannelConvertible) outputStream).toFileChannel().position()).isEqualTo(0); 67 } 68 } 69 70 @Test openForRead_shouldImplementSizable()71 public void openForRead_shouldImplementSizable() throws IOException { 72 byte[] content = makeArrayOfBytesContent(); 73 try (OutputStream out = backend.openForWrite(uriForTestMethod())) { 74 out.write(content); 75 } 76 77 try (InputStream inputStream = backend.openForRead(uriForTestMethod())) { 78 assertThat(inputStream).isInstanceOf(Sizable.class); 79 assertThat(((Sizable) inputStream).size()).isEqualTo(content.length); 80 } 81 } 82 83 @Test lockScope_returnsNonNullLockScope()84 public void lockScope_returnsNonNullLockScope() throws IOException { 85 assertThat(backend.lockScope()).isNotNull(); 86 } 87 88 @Test lockScope_canBeOverridden()89 public void lockScope_canBeOverridden() throws IOException { 90 LockScope lockScope = new LockScope(); 91 backend = new JavaFileBackend(lockScope); 92 assertThat(backend.lockScope()).isSameInstanceAs(lockScope); 93 } 94 95 @Override backend()96 protected Backend backend() { 97 return backend; 98 } 99 100 @Override legalUriBase()101 protected Uri legalUriBase() { 102 return FileUri.fromFile(tmpFolder.getRoot()); 103 } 104 105 @Override illegalUrisToRead()106 protected List<Uri> illegalUrisToRead() { 107 String root = legalUriBase().getPath(); 108 return ImmutableList.of( 109 Uri.parse("file://<internal>@authority:123/" + root + "/uriWithAuthority"), 110 Uri.parse("file:///" + root + "/uriWithQuery?q=a")); 111 } 112 113 @Override illegalUrisToWrite()114 protected List<Uri> illegalUrisToWrite() { 115 return ImmutableList.of(); 116 } 117 118 @Override illegalUrisToAppend()119 protected List<Uri> illegalUrisToAppend() { 120 return illegalUrisToWrite(); 121 } 122 } 123