Skip to content

Commit 1955e15

Browse files
VFS-676 : add some tests
ignore two test : testCannotDeleteWhileStreaming , testCannotDeleteWhileStreaming2 add a new test : testReadStreamAfterDeleteFile
1 parent 24969cb commit 1955e15

5 files changed

Lines changed: 55 additions & 16 deletions

File tree

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/jar/JarFileObject.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
*/
4040
public class JarFileObject extends AbstractFileObject<JarFileSystem> {
4141

42-
/** The JarEntry. */
42+
/**
43+
* The JarEntry.
44+
*/
4345
protected JarEntry entry;
4446
private final HashSet<String> children = new HashSet<>();
4547
private FileType type;
@@ -49,16 +51,16 @@ public class JarFileObject extends AbstractFileObject<JarFileSystem> {
4951
private Attributes attributes;
5052

5153
protected JarFileObject(final AbstractFileName name, final JarEntry entry, final JarFileSystem fs,
52-
final boolean jarExists) throws FileSystemException {
54+
final boolean jarExists) throws FileSystemException {
5355
super(name, fs);
5456
setJarEntry(entry);
5557
if (!jarExists) {
5658
type = FileType.IMAGINARY;
5759
}
5860
if (entry != null) {
59-
// For Java 9 and up: Force the certificates to be read and cached now. This avoids an
60-
// IllegalStateException in java.util.jar.JarFile.isMultiRelease() when it tries
61-
// to read the certificates and the file is closed.
61+
// For Java 9 and up: Force the certificates to be read and cached now. This avoids an
62+
// IllegalStateException in java.util.jar.JarFile.isMultiRelease() when it tries
63+
// to read the certificates and the file is closed.
6264
entry.getCertificates();
6365
}
6466
this.fs = fs;
@@ -118,6 +120,7 @@ public boolean isWriteable() throws FileSystemException {
118120

119121
/**
120122
* Returns the file's type.
123+
*
121124
* @since 2.7.0
122125
*/
123126
@Override
@@ -127,6 +130,7 @@ protected FileType doGetType() {
127130

128131
/**
129132
* Lists the children of the file.
133+
*
130134
* @since 2.7.0
131135
*/
132136
@Override
@@ -146,6 +150,7 @@ protected String[] doListChildren() {
146150
/**
147151
* Returns the size of the file content (in bytes). Is only called if {@link #doGetType} returns
148152
* {@link FileType#FILE}.
153+
*
149154
* @since 2.7.0
150155
*/
151156
@Override
@@ -155,6 +160,7 @@ protected long doGetContentSize() {
155160

156161
/**
157162
* Returns the last modified time of this file.
163+
*
158164
* @since 2.7.0
159165
*/
160166
@Override
@@ -166,6 +172,7 @@ protected long doGetLastModifiedTime() throws Exception {
166172
* Creates an input stream to read the file content from. Is only called if {@link #doGetType} returns
167173
* {@link FileType#FILE}. The input stream returned by this method is guaranteed to be closed before this method is
168174
* called again.
175+
*
169176
* @since 2.7.0
170177
*/
171178
@Override
@@ -181,8 +188,8 @@ protected InputStream doGetInputStream(final int bufferSize) throws Exception {
181188
}
182189

183190
/**
184-
* @since 2.7.0
185-
*/
191+
* @since 2.7.0
192+
*/
186193
@Override
187194
protected void doAttach() throws Exception {
188195
getAbstractFileSystem().getJarFile();

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/jar/JarFileSystem.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class JarFileSystem extends AbstractFileSystem {
6464
private Attributes attributes;
6565

6666
protected JarFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
67-
final FileSystemOptions fileSystemOptions) throws FileSystemException {
67+
final FileSystemOptions fileSystemOptions) throws FileSystemException {
6868
super(rootName, parentLayer, fileSystemOptions);
6969

7070
// Make a local copy of the file
@@ -180,6 +180,7 @@ protected void doCloseCommunicationLink() {
180180

181181
/**
182182
* Returns the capabilities of this file system.
183+
*
183184
* @since 2.7.0
184185
*/
185186
@Override
@@ -189,6 +190,7 @@ protected void addCapabilities(final Collection<Capability> caps) {
189190

190191
/**
191192
* Creates a file object.
193+
*
192194
* @since 2.7.0
193195
*/
194196
@Override
@@ -199,6 +201,7 @@ protected FileObject createFile(final AbstractFileName name) throws FileSystemEx
199201

200202
/**
201203
* Adds a file object to the cache.
204+
*
202205
* @since 2.7.0
203206
*/
204207
@Override
@@ -215,6 +218,7 @@ protected Charset getCharset() {
215218

216219
/**
217220
* Returns a cached file.
221+
*
218222
* @since 2.7.0
219223
*/
220224
@Override
@@ -224,6 +228,7 @@ protected FileObject getFileFromCache(final FileName name) {
224228

225229
/**
226230
* remove a cached file.
231+
*
227232
* @since 2.7.0
228233
*/
229234
@Override
@@ -232,8 +237,8 @@ protected void removeFileFromCache(final FileName name) {
232237
}
233238

234239
/**
235-
* @since 2.7.0
236-
*/
240+
* @since 2.7.0
241+
*/
237242
@Override
238243
public String toString() {
239244
return super.toString() + " for " + file;

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
*/
3232
public class ZipFileObject extends AbstractFileObject<ZipFileSystem> {
3333

34-
/** The ZipEntry. */
34+
/**
35+
* The ZipEntry.
36+
*/
3537
protected ZipArchiveEntry entry;
3638
private final HashSet<String> children = new HashSet<>();
3739
private FileType type;
3840

3941
protected ZipFileObject(final AbstractFileName name, final ZipArchiveEntry entry, final ZipFileSystem fs,
40-
final boolean zipExists) throws FileSystemException {
42+
final boolean zipExists) throws FileSystemException {
4143
super(name, fs);
4244
setZipEntry(entry);
4345
if (!zipExists) {

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class ZipFileSystem extends AbstractFileSystem {
5858
private final Map<FileName, FileObject> cache = new HashMap<>();
5959

6060
public ZipFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
61-
final FileSystemOptions fileSystemOptions) throws FileSystemException {
61+
final FileSystemOptions fileSystemOptions) throws FileSystemException {
6262
super(rootName, parentLayer, fileSystemOptions);
6363

6464
// Make a local copy of the file
@@ -104,7 +104,7 @@ public void init() throws FileSystemException {
104104
ZipFileObject parent;
105105
for (AbstractFileName parentName = (AbstractFileName) name
106106
.getParent(); parentName != null; fileObj = parent, parentName = (AbstractFileName) parentName
107-
.getParent()) {
107+
.getParent()) {
108108
// Locate the parent
109109
parent = (ZipFileObject) getFileFromCache(parentName);
110110
if (parent == null) {

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/test/FileLockTestCase.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.apache.commons.vfs2.provider.zip.test;
1919

20+
import java.io.BufferedReader;
2021
import java.io.File;
2122
import java.io.IOException;
2223
import java.io.InputStream;
24+
import java.io.InputStreamReader;
2325

2426
import org.apache.commons.io.FileUtils;
2527
import org.apache.commons.io.IOUtils;
@@ -31,6 +33,7 @@
3133
import org.junit.Assert;
3234
import org.junit.Assume;
3335
import org.junit.Before;
36+
import org.junit.Ignore;
3437
import org.junit.Test;
3538

3639
/**
@@ -84,7 +87,7 @@ public void setup() throws IOException {
8487
manager = VFS.getManager();
8588
}
8689

87-
@Test
90+
@Ignore
8891
public void testCannotDeleteWhileStreaming() throws Exception {
8992
try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
9093
try (InputStream inputStream = zipFileObject.getContent().getInputStream()) {
@@ -97,7 +100,7 @@ public void testCannotDeleteWhileStreaming() throws Exception {
97100
assertDelete();
98101
}
99102

100-
@Test
103+
@Ignore
101104
public void testCannotDeleteWhileStreaming2() throws Exception {
102105
Assume.assumeTrue(Os.isFamily(Os.OS_FAMILY_WINDOWS));
103106
try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
@@ -108,6 +111,28 @@ public void testCannotDeleteWhileStreaming2() throws Exception {
108111
}
109112
}
110113

114+
@Test
115+
public void testReadStreamAfterDeleteFile() throws Exception {
116+
// test read stream after delete file
117+
try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
118+
try (InputStream inputStream = zipFileObject.getContent().getInputStream()) {
119+
// delete file after open stream
120+
Assert.assertTrue("Could not delete file", newZipFile.delete());
121+
122+
// read file by line,count line number
123+
int lineNumber = 0;
124+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
125+
while (bufferedReader.readLine() != null) {
126+
lineNumber++;
127+
}
128+
}
129+
130+
// check line number
131+
Assert.assertTrue("Line number should be 1", (lineNumber == 1));
132+
}
133+
}
134+
}
135+
111136
@Test
112137
public void testResolveAndOpenCloseContent() throws Exception {
113138
resolveAndOpenCloseContent();

0 commit comments

Comments
 (0)