|
19 | 19 | import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectory; |
20 | 20 |
|
21 | 21 | import java.io.File; |
| 22 | +import java.io.InputStream; |
22 | 23 | import java.time.Duration; |
23 | 24 | import java.util.concurrent.TimeUnit; |
24 | 25 |
|
25 | 26 | import org.apache.commons.vfs2.AbstractProviderTestConfig; |
| 27 | +import org.apache.commons.vfs2.CacheStrategy; |
| 28 | +import org.apache.commons.vfs2.FileContent; |
26 | 29 | import org.apache.commons.vfs2.FileNotFolderException; |
27 | 30 | import org.apache.commons.vfs2.FileObject; |
28 | 31 | import org.apache.commons.vfs2.FileSystemException; |
29 | 32 | import org.apache.commons.vfs2.FileSystemManager; |
30 | 33 | import org.apache.commons.vfs2.FileSystemOptions; |
31 | 34 | import org.apache.commons.vfs2.ProviderTestSuite; |
32 | 35 | import org.apache.commons.vfs2.VFS; |
| 36 | +import org.apache.commons.vfs2.cache.SoftRefFilesCache; |
33 | 37 | import org.apache.commons.vfs2.impl.DefaultFileSystemManager; |
34 | 38 | import org.apache.commons.vfs2.util.NHttpFileServer; |
35 | 39 | import org.junit.Test; |
@@ -191,6 +195,36 @@ public void testHttpTimeoutConfig() { |
191 | 195 | assertEquals("foo/bar", builder.getUserAgent(opts)); |
192 | 196 | } |
193 | 197 |
|
| 198 | + @Test |
| 199 | + public void testReadFileOperations() throws Exception { |
| 200 | + try (DefaultFileSystemManager manager = new DefaultFileSystemManager(); |
| 201 | + Http4FileProvider provider = new Http4FileProvider(); |
| 202 | + SoftRefFilesCache filesCache = new SoftRefFilesCache();) { |
| 203 | + manager.addProvider("http4", provider); |
| 204 | + manager.setFilesCache(filesCache); |
| 205 | + manager.setCacheStrategy(CacheStrategy.ON_RESOLVE); |
| 206 | + final String nonExistentFileUri = connectionUri + "/read-tests/nonexistent.txt"; |
| 207 | + try (FileObject nonExistentFileObject = manager.resolveFile(nonExistentFileUri); FileContent content = nonExistentFileObject.getContent();) { |
| 208 | + // Attempt to read from the stream |
| 209 | + final FileSystemException e = Assertions.assertThrows(FileSystemException.class, content::getInputStream); |
| 210 | + Assertions.assertTrue(e.getCode().contains("read-not-file.error"), |
| 211 | + "Expected HTTP 404 Not Found error, but got: " + e.getMessage() + " " + e.getCode()); |
| 212 | + } |
| 213 | + final String existentFileUri = connectionUri + "/read-tests/file1.txt"; |
| 214 | + try (FileObject existentFileObject = manager.resolveFile(existentFileUri)) { |
| 215 | + Assertions.assertTrue(existentFileObject.exists(), "File should exist"); |
| 216 | + try (FileContent content = existentFileObject.getContent(); InputStream inputStream = content.getInputStream()) { |
| 217 | + Assertions.assertNotNull(inputStream, "InputStream should not be null"); |
| 218 | + final int available = inputStream.available(); |
| 219 | + Assertions.assertTrue(available > 0, "InputStream should have content available"); |
| 220 | + final byte[] buffer = new byte[1024]; |
| 221 | + final int bytesRead = inputStream.read(buffer); |
| 222 | + Assertions.assertTrue(bytesRead > 0, "InputStream should read non-empty content"); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
194 | 228 | private void testResolveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException { |
195 | 229 | VFS.getManager().getFilesCache().close(); |
196 | 230 | final FileSystemOptions opts = new FileSystemOptions(); |
|
0 commit comments