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 20 import android.net.Uri; 21 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 22 import com.google.android.libraries.mobiledatadownload.file.backends.FileUri; 23 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend; 24 import com.google.android.libraries.mobiledatadownload.file.common.testing.FileDescriptorLeakChecker; 25 import com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils; 26 import com.google.common.collect.ImmutableList; 27 import java.io.FileInputStream; 28 import java.io.RandomAccessFile; 29 import org.junit.Before; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.rules.TemporaryFolder; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 @RunWith(JUnit4.class) 37 public final class RandomAccessFileOpenerAndroidTest { 38 39 private SynchronousFileStorage storage; 40 41 @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); 42 @Rule public FileDescriptorLeakChecker leakChecker = new FileDescriptorLeakChecker(); 43 44 @Before setUpStorage()45 public void setUpStorage() throws Exception { 46 storage = new SynchronousFileStorage(ImmutableList.of(new JavaFileBackend())); 47 } 48 49 @Test succeedsWithSimplePath()50 public void succeedsWithSimplePath() throws Exception { 51 Uri uri = uriToNewTempFile().build(); 52 byte[] content = StreamUtils.makeArrayOfBytesContent(); 53 StreamUtils.createFile(storage, uri, content); 54 55 RandomAccessFileOpener opener = RandomAccessFileOpener.createForRead(); 56 RandomAccessFile file = storage.open(uri, opener); 57 assertThat(StreamUtils.readFileInBytesFromSource(new FileInputStream(file.getFD()))) 58 .isEqualTo(content); 59 60 file.close(); 61 } 62 63 @Test succeedsWithCreateForReadWrite()64 public void succeedsWithCreateForReadWrite() throws Exception { 65 Uri uri = 66 FileUri.builder() 67 .fromFile(tmpFolder.getRoot()) 68 .appendPath("this/does/not/exist/foo.pb") 69 .build(); 70 71 RandomAccessFileOpener opener = RandomAccessFileOpener.createForReadWrite(); 72 try (RandomAccessFile file = storage.open(uri, opener)) { 73 file.write(123); 74 file.seek(0); 75 assertThat(file.read()).isEqualTo(123); 76 } 77 } 78 uriToNewTempFile()79 private FileUri.Builder uriToNewTempFile() throws Exception { 80 return FileUri.builder().fromFile(tmpFolder.newFile()); 81 } 82 } 83