Skip to content

Commit 46b1037

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Make PlatformTestStorage throw FileNotFoundException instead of IOException.
This was done largely to have consistency with its TestStorage implementation, and to have a more precise API. PiperOrigin-RevId: 604714337
1 parent 70805a4 commit 46b1037

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

runner/android_junit_runner/javatests/androidx/test/internal/runner/RunnerArgsTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.BufferedWriter;
4242
import java.io.ByteArrayInputStream;
4343
import java.io.File;
44+
import java.io.FileNotFoundException;
4445
import java.io.FileWriter;
4546
import java.io.IOException;
4647
import java.io.InputStream;
@@ -704,11 +705,11 @@ private static class FakeTestStorage implements PlatformTestStorage {
704705
private final Map<String, InputStream> inputFileMap = new HashMap<>();
705706

706707
@Override
707-
public InputStream openInputFile(String pathname) throws IOException {
708+
public InputStream openInputFile(String pathname) throws FileNotFoundException {
708709

709710
InputStream is = inputFileMap.get(pathname);
710711
if (is == null) {
711-
throw new IOException();
712+
throw new FileNotFoundException();
712713
}
713714
return is;
714715
}
@@ -728,12 +729,12 @@ public Map<String, String> getInputArgs() {
728729
}
729730

730731
@Override
731-
public OutputStream openOutputFile(String pathname) throws IOException {
732+
public OutputStream openOutputFile(String pathname) {
732733
throw new UnsupportedOperationException();
733734
}
734735

735736
@Override
736-
public OutputStream openOutputFile(String pathname, boolean append) throws IOException {
737+
public OutputStream openOutputFile(String pathname, boolean append) {
737738
throw new UnsupportedOperationException();
738739
}
739740

@@ -748,12 +749,12 @@ public Map<String, Serializable> getOutputProperties() {
748749
}
749750

750751
@Override
751-
public InputStream openInternalInputFile(String pathname) throws IOException {
752+
public InputStream openInternalInputFile(String pathname) {
752753
throw new UnsupportedOperationException();
753754
}
754755

755756
@Override
756-
public OutputStream openInternalOutputFile(String pathname) throws IOException {
757+
public OutputStream openInternalOutputFile(String pathname) {
757758
throw new UnsupportedOperationException();
758759
}
759760

runner/monitor/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* Make DeviceController an public API from ExperimentalTestApi
1515
* Upstream TestStorage.isTestStoragePath to PlatformTestStorage
1616
* Upstream TestStorage.getInputFileUri and getOutputFileUri to PlatformTestStorage
17+
* Change PlatformTestStorage methods to throw FileNotFoundException instead of
18+
IOException
1719

1820
**Breaking API Changes**
1921

runner/monitor/java/androidx/test/platform/io/FileTestStorage.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import androidx.test.platform.app.InstrumentationRegistry;
2424
import java.io.File;
2525
import java.io.FileInputStream;
26+
import java.io.FileNotFoundException;
2627
import java.io.FileOutputStream;
2728
import java.io.IOException;
2829
import java.io.InputStream;
@@ -55,12 +56,19 @@ public FileTestStorage() {
5556
* apk's asset directory
5657
*/
5758
@Override
58-
public InputStream openInputFile(String pathname) throws IOException {
59+
public InputStream openInputFile(String pathname) throws FileNotFoundException {
5960
File inputFile = new File(pathname);
6061
if (inputFile.isAbsolute()) {
6162
return new FileInputStream(inputFile);
6263
}
63-
return InstrumentationRegistry.getInstrumentation().getContext().getAssets().open(pathname);
64+
try {
65+
return InstrumentationRegistry.getInstrumentation().getContext().getAssets().open(pathname);
66+
} catch (IOException e) {
67+
FileNotFoundException fe =
68+
new FileNotFoundException(String.format("failed to open %s from apk assets", pathname));
69+
fe.initCause(e);
70+
throw fe;
71+
}
6472
}
6573

6674
/**
@@ -71,12 +79,12 @@ public InputStream openInputFile(String pathname) throws IOException {
7179
* writable output dir based on API level.
7280
*/
7381
@Override
74-
public OutputStream openOutputFile(String pathname) throws IOException {
82+
public OutputStream openOutputFile(String pathname) throws FileNotFoundException {
7583
return openOutputFile(pathname, false);
7684
}
7785

7886
@Override
79-
public OutputStream openOutputFile(String pathname, boolean append) throws IOException {
87+
public OutputStream openOutputFile(String pathname, boolean append) throws FileNotFoundException {
8088
File outputFile = new File(pathname);
8189
if (!outputFile.isAbsolute()) {
8290
outputFile = new File(outputDirCalculator.getOutputDir(), pathname);
@@ -127,7 +135,7 @@ public Map<String, Serializable> getOutputProperties() {
127135
* file path is readable.
128136
*/
129137
@Override
130-
public InputStream openInternalInputFile(String pathname) throws IOException {
138+
public InputStream openInternalInputFile(String pathname) throws FileNotFoundException {
131139
return openInputFile(pathname);
132140
}
133141

@@ -139,7 +147,7 @@ public InputStream openInternalInputFile(String pathname) throws IOException {
139147
* apk's asset directory
140148
*/
141149
@Override
142-
public OutputStream openInternalOutputFile(String pathname) throws IOException {
150+
public OutputStream openInternalOutputFile(String pathname) throws FileNotFoundException {
143151
return openOutputFile(pathname);
144152
}
145153

runner/monitor/java/androidx/test/platform/io/PlatformTestStorage.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import android.net.Uri;
1919
import androidx.annotation.NonNull;
2020
import androidx.test.annotation.ExperimentalTestApi;
21-
import java.io.IOException;
21+
import java.io.FileNotFoundException;
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
2424
import java.io.Serializable;
@@ -44,8 +44,9 @@ public interface PlatformTestStorage {
4444
*
4545
* @param pathname path to the test file dependency. Should not be null.
4646
* @return an InputStream to the given test file.
47+
* @throws FileNotFoundException if pathname does not exist
4748
*/
48-
InputStream openInputFile(String pathname) throws IOException;
49+
InputStream openInputFile(String pathname) throws FileNotFoundException;
4950

5051
/**
5152
* Returns the value of a given argument name.
@@ -64,8 +65,9 @@ public interface PlatformTestStorage {
6465
*
6566
* @param pathname path to the test output file. Should not be null.
6667
* @return an OutputStream to the given output file.
68+
* @throws FileNotFoundException if pathname does not exist
6769
*/
68-
OutputStream openOutputFile(String pathname) throws IOException;
70+
OutputStream openOutputFile(String pathname) throws FileNotFoundException;
6971

7072
/**
7173
* Provides an OutputStream to a test output file.
@@ -74,8 +76,9 @@ public interface PlatformTestStorage {
7476
* @param append if true, then the lines will be added to the end of the file rather than
7577
* overwriting.
7678
* @return an OutputStream to the given output file.
79+
* @throws FileNotFoundException if pathname does not exist
7780
*/
78-
OutputStream openOutputFile(String pathname, boolean append) throws IOException;
81+
OutputStream openOutputFile(String pathname, boolean append) throws FileNotFoundException;
7982

8083
/**
8184
* Adds the given properties.
@@ -96,16 +99,18 @@ public interface PlatformTestStorage {
9699
*
97100
* @param pathname path to the internal file. Should not be null.
98101
* @return an InputStream to the given test file.
102+
* @throws FileNotFoundException if pathname does not exist
99103
*/
100-
InputStream openInternalInputFile(String pathname) throws IOException;
104+
InputStream openInternalInputFile(String pathname) throws FileNotFoundException;
101105

102106
/**
103107
* Provides an OutputStream to an internal file used by the testing infrastructure.
104108
*
105109
* @param pathname path to the internal file. Should not be null.
106110
* @return an OutputStream to the given output file.
111+
* @throws FileNotFoundException if pathname does not exist
107112
*/
108-
OutputStream openInternalOutputFile(String pathname) throws IOException;
113+
OutputStream openInternalOutputFile(String pathname) throws FileNotFoundException;
109114

110115
/**
111116
* Provides a Uri to a test file dependency.

runner/monitor/java/androidx/test/platform/io/PlatformTestStorageRegistry.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import androidx.annotation.NonNull;
2222
import androidx.test.annotation.ExperimentalTestApi;
2323
import androidx.test.internal.platform.ServiceLoaderWrapper;
24-
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.io.OutputStream;
2726
import java.io.Serializable;
@@ -109,12 +108,12 @@ public Map<String, Serializable> getOutputProperties() {
109108
}
110109

111110
@Override
112-
public InputStream openInternalInputFile(String pathname) throws IOException {
111+
public InputStream openInternalInputFile(String pathname) {
113112
return new NullInputStream();
114113
}
115114

116115
@Override
117-
public OutputStream openInternalOutputFile(String pathname) throws IOException {
116+
public OutputStream openInternalOutputFile(String pathname) {
118117
return new NullOutputStream();
119118
}
120119

services/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
* Upstream TestStorage.isTestStoragePath to PlatformTestStorage
1616
* Upstream TestStorage.getInputFileUri and getOutputFileUri to PlatformTestStorage
17+
* Change openInternal* methods to throw FileNotFoundException instead of
18+
IOException for consistency
1719

1820
**Breaking API Changes**
1921

services/storage/java/androidx/test/services/storage/TestStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public Map<String, Serializable> getOutputProperties() {
338338
*/
339339
@RestrictTo(Scope.LIBRARY)
340340
@Override
341-
public InputStream openInternalInputFile(String pathname) throws IOException {
341+
public InputStream openInternalInputFile(String pathname) throws FileNotFoundException {
342342
checkNotNull(pathname);
343343
Uri outputUri = HostedFile.buildUri(FileHost.INTERNAL_USE_ONLY, pathname);
344344
return TestStorageUtil.getInputStream(outputUri, contentResolver);
@@ -356,7 +356,7 @@ public InputStream openInternalInputFile(String pathname) throws IOException {
356356
*/
357357
@RestrictTo(Scope.LIBRARY)
358358
@Override
359-
public OutputStream openInternalOutputFile(String pathname) throws IOException {
359+
public OutputStream openInternalOutputFile(String pathname) throws FileNotFoundException {
360360
checkNotNull(pathname);
361361
Uri outputUri = HostedFile.buildUri(FileHost.INTERNAL_USE_ONLY, pathname);
362362
return TestStorageUtil.getOutputStream(outputUri, contentResolver);

0 commit comments

Comments
 (0)