Skip to content

Commit e0fc2f2

Browse files
committed
re #VFS-834: Added test cases which test for the bug.
1 parent 4157876 commit e0fc2f2

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

commons-vfs2/src/test/java/org/apache/commons/vfs2/AbstractProviderTestCase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,22 @@ protected void assertSameContent(final String expected, final FileObject file) t
114114
* are encoded using UTF-8.
115115
*/
116116
protected void assertSameURLContent(final String expected, final URLConnection connection) throws Exception {
117+
assertSameURLContent(expected, connection.getInputStream(), connection);
118+
}
119+
120+
/**
121+
* Asserts that the content of a file is the same as expected. Checks the length reported by getContentLength() is
122+
* correct, then reads the content as a byte stream and compares the result with the expected content. Assumes files
123+
* are encoded using UTF-8.
124+
*/
125+
protected void assertSameURLContent(final String expected, final InputStream instr, final URLConnection connection) throws Exception {
117126
// Get file content as a binary stream
118127
final byte[] expectedBin = expected.getBytes(StandardCharsets.UTF_8);
119128

120129
// Check lengths
121130
assertEquals("same content length", expectedBin.length, connection.getContentLength());
122131

123132
// Read content into byte array
124-
final InputStream instr = connection.getInputStream();
125133
final ByteArrayOutputStream outstr;
126134
try {
127135
outstr = new ByteArrayOutputStream();

commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/VfsClassLoaderTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectoryFile;
2020

2121
import java.io.File;
22+
import java.io.InputStream;
2223
import java.io.PrintWriter;
2324
import java.net.URL;
2425
import java.net.URLConnection;
@@ -196,11 +197,21 @@ public void testLoadClass() throws Exception {
196197
public void testLoadResource() throws Exception {
197198
final VFSClassLoader loader = createClassLoader();
198199

199-
final URL resource = loader.getResource("read-tests/file1.txt");
200+
final URL resource1 = loader.getResource("read-tests/file1.txt");
201+
assertNotNull(resource1);
202+
final URLConnection urlCon1 = resource1.openConnection();
203+
final InputStream instr1 = urlCon1.getInputStream();
200204

201-
assertNotNull(resource);
202-
final URLConnection urlCon = resource.openConnection();
203-
assertSameURLContent(FILE1_CONTENT, urlCon);
205+
// VFS-834: testing that getting the resource again does not close out the previous input stream.
206+
final URL resource2 = loader.getResource("read-tests/file1.txt");
207+
208+
assertSameURLContent(FILE1_CONTENT, instr1, urlCon1);
209+
210+
assertNotNull(resource2);
211+
final URLConnection urlCon2 = resource2.openConnection();
212+
final InputStream instr2 = urlCon1.getInputStream();
213+
214+
assertSameURLContent(FILE1_CONTENT, instr2, urlCon2);
204215
}
205216

206217
/**

0 commit comments

Comments
 (0)