Skip to content

Commit

Permalink
VFS-676 : add some tests
Browse files Browse the repository at this point in the history
ignore two test : testCannotDeleteWhileStreaming , testCannotDeleteWhileStreaming2
add a new test : testReadStreamAfterDeleteFile
  • Loading branch information
PeterAlfredLee committed May 25, 2020
1 parent 24969cb commit 1955e15
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
*/
public class JarFileObject extends AbstractFileObject<JarFileSystem> {

/** The JarEntry. */
/**
* The JarEntry.
*/
protected JarEntry entry;
private final HashSet<String> children = new HashSet<>();
private FileType type;
Expand All @@ -49,16 +51,16 @@ public class JarFileObject extends AbstractFileObject<JarFileSystem> {
private Attributes attributes;

protected JarFileObject(final AbstractFileName name, final JarEntry entry, final JarFileSystem fs,
final boolean jarExists) throws FileSystemException {
final boolean jarExists) throws FileSystemException {
super(name, fs);
setJarEntry(entry);
if (!jarExists) {
type = FileType.IMAGINARY;
}
if (entry != null) {
// For Java 9 and up: Force the certificates to be read and cached now. This avoids an
// IllegalStateException in java.util.jar.JarFile.isMultiRelease() when it tries
// to read the certificates and the file is closed.
// For Java 9 and up: Force the certificates to be read and cached now. This avoids an
// IllegalStateException in java.util.jar.JarFile.isMultiRelease() when it tries
// to read the certificates and the file is closed.
entry.getCertificates();
}
this.fs = fs;
Expand Down Expand Up @@ -118,6 +120,7 @@ public boolean isWriteable() throws FileSystemException {

/**
* Returns the file's type.
*
* @since 2.7.0
*/
@Override
Expand All @@ -127,6 +130,7 @@ protected FileType doGetType() {

/**
* Lists the children of the file.
*
* @since 2.7.0
*/
@Override
Expand All @@ -146,6 +150,7 @@ protected String[] doListChildren() {
/**
* Returns the size of the file content (in bytes). Is only called if {@link #doGetType} returns
* {@link FileType#FILE}.
*
* @since 2.7.0
*/
@Override
Expand All @@ -155,6 +160,7 @@ protected long doGetContentSize() {

/**
* Returns the last modified time of this file.
*
* @since 2.7.0
*/
@Override
Expand All @@ -166,6 +172,7 @@ protected long doGetLastModifiedTime() throws Exception {
* Creates an input stream to read the file content from. Is only called if {@link #doGetType} returns
* {@link FileType#FILE}. The input stream returned by this method is guaranteed to be closed before this method is
* called again.
*
* @since 2.7.0
*/
@Override
Expand All @@ -181,8 +188,8 @@ protected InputStream doGetInputStream(final int bufferSize) throws Exception {
}

/**
* @since 2.7.0
*/
* @since 2.7.0
*/
@Override
protected void doAttach() throws Exception {
getAbstractFileSystem().getJarFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class JarFileSystem extends AbstractFileSystem {
private Attributes attributes;

protected JarFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
final FileSystemOptions fileSystemOptions) throws FileSystemException {
final FileSystemOptions fileSystemOptions) throws FileSystemException {
super(rootName, parentLayer, fileSystemOptions);

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

/**
* Returns the capabilities of this file system.
*
* @since 2.7.0
*/
@Override
Expand All @@ -189,6 +190,7 @@ protected void addCapabilities(final Collection<Capability> caps) {

/**
* Creates a file object.
*
* @since 2.7.0
*/
@Override
Expand All @@ -199,6 +201,7 @@ protected FileObject createFile(final AbstractFileName name) throws FileSystemEx

/**
* Adds a file object to the cache.
*
* @since 2.7.0
*/
@Override
Expand All @@ -215,6 +218,7 @@ protected Charset getCharset() {

/**
* Returns a cached file.
*
* @since 2.7.0
*/
@Override
Expand All @@ -224,6 +228,7 @@ protected FileObject getFileFromCache(final FileName name) {

/**
* remove a cached file.
*
* @since 2.7.0
*/
@Override
Expand All @@ -232,8 +237,8 @@ protected void removeFileFromCache(final FileName name) {
}

/**
* @since 2.7.0
*/
* @since 2.7.0
*/
@Override
public String toString() {
return super.toString() + " for " + file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
*/
public class ZipFileObject extends AbstractFileObject<ZipFileSystem> {

/** The ZipEntry. */
/**
* The ZipEntry.
*/
protected ZipArchiveEntry entry;
private final HashSet<String> children = new HashSet<>();
private FileType type;

protected ZipFileObject(final AbstractFileName name, final ZipArchiveEntry entry, final ZipFileSystem fs,
final boolean zipExists) throws FileSystemException {
final boolean zipExists) throws FileSystemException {
super(name, fs);
setZipEntry(entry);
if (!zipExists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class ZipFileSystem extends AbstractFileSystem {
private final Map<FileName, FileObject> cache = new HashMap<>();

public ZipFileSystem(final AbstractFileName rootName, final FileObject parentLayer,
final FileSystemOptions fileSystemOptions) throws FileSystemException {
final FileSystemOptions fileSystemOptions) throws FileSystemException {
super(rootName, parentLayer, fileSystemOptions);

// Make a local copy of the file
Expand Down Expand Up @@ -104,7 +104,7 @@ public void init() throws FileSystemException {
ZipFileObject parent;
for (AbstractFileName parentName = (AbstractFileName) name
.getParent(); parentName != null; fileObj = parent, parentName = (AbstractFileName) parentName
.getParent()) {
.getParent()) {
// Locate the parent
parent = (ZipFileObject) getFileFromCache(parentName);
if (parent == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

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

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
Expand All @@ -31,6 +33,7 @@
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -84,7 +87,7 @@ public void setup() throws IOException {
manager = VFS.getManager();
}

@Test
@Ignore
public void testCannotDeleteWhileStreaming() throws Exception {
try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
try (InputStream inputStream = zipFileObject.getContent().getInputStream()) {
Expand All @@ -97,7 +100,7 @@ public void testCannotDeleteWhileStreaming() throws Exception {
assertDelete();
}

@Test
@Ignore
public void testCannotDeleteWhileStreaming2() throws Exception {
Assume.assumeTrue(Os.isFamily(Os.OS_FAMILY_WINDOWS));
try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
Expand All @@ -108,6 +111,28 @@ public void testCannotDeleteWhileStreaming2() throws Exception {
}
}

@Test
public void testReadStreamAfterDeleteFile() throws Exception {
// test read stream after delete file
try (final FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
try (InputStream inputStream = zipFileObject.getContent().getInputStream()) {
// delete file after open stream
Assert.assertTrue("Could not delete file", newZipFile.delete());

// read file by line,count line number
int lineNumber = 0;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
while (bufferedReader.readLine() != null) {
lineNumber++;
}
}

// check line number
Assert.assertTrue("Line number should be 1", (lineNumber == 1));
}
}
}

@Test
public void testResolveAndOpenCloseContent() throws Exception {
resolveAndOpenCloseContent();
Expand Down

0 comments on commit 1955e15

Please sign in to comment.