Skip to content

Commit

Permalink
[VFS-676] : Add tests for commons compress zip provider
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterAlfredLee committed Jun 8, 2020
1 parent 6673613 commit 6d2ef48
Show file tree
Hide file tree
Showing 10 changed files with 1,073 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.vfs2.provider.cczip;

import java.io.File;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.cache.OnCallRefreshFileObject;
import org.apache.commons.vfs2.function.VfsConsumer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class Jira733TestCase {

@After
@Before
public void reset() throws FileSystemException {
VFS.reset();
}

@Test
public void testZipParentLayer() throws Exception {
final File file = new File("src/test/resources/test-data/test.zip");
final String nestedPath = "commonscompresszip:" + file.getAbsolutePath() + "!/read-tests/file1.txt";
try (final FileObject fileObject = VFS.getManager().resolveFile(nestedPath);
final FileObject wrappedFileObject = new OnCallRefreshFileObject(fileObject)) {
// VFS.getManager().getFilesCache().close();
Assert.assertNotNull("getParentLayer() 1", wrappedFileObject.getFileSystem().getParentLayer());
wrappedFileObject.exists();
wrappedFileObject.getContent();
Assert.assertNotNull("getParentLayer() 2", wrappedFileObject.getFileSystem().getParentLayer());
}
}

private void testZipParentLayer(final VfsConsumer<FileObject> consumer) throws Exception {
final File file = new File("src/test/resources/test-data/test.zip");
Assert.assertTrue(file.exists());
final String nestedPath = "commonscompresszip:" + file.getAbsolutePath() + "!/read-tests/file1.txt";
try (final FileObject fileObject = VFS.getManager().resolveFile(nestedPath);
final FileObject wrappedFileObject = new OnCallRefreshFileObject(fileObject)) {
Assert.assertTrue(fileObject instanceof CommonsCompressZipFileObject);
@SuppressWarnings({"unused", "resource"}) final CommonsCompressZipFileObject zipFileObject = (CommonsCompressZipFileObject) fileObject;
Assert.assertNotNull("getParentLayer() 1", wrappedFileObject.getFileSystem().getParentLayer());
consumer.accept(wrappedFileObject);
Assert.assertNotNull("getParentLayer() 2", wrappedFileObject.getFileSystem().getParentLayer());
}
}

@Test
public void testZipParentLayer_exists() throws Exception {
testZipParentLayer(FileObject::exists);
}

@Test
public void testZipParentLayer_exists_getContents() throws Exception {
testZipParentLayer(fileObject -> {
fileObject.exists();
fileObject.getContent();
});
}

@Test
public void testZipParentLayer_getChildren() throws Exception {
testZipParentLayer(fileObject -> fileObject.getParent().getChildren());
}

@Test
public void testZipParentLayer_getContents() throws Exception {
testZipParentLayer(FileObject::getContent);
}

@Test
public void testZipParentLayer_getType() throws Exception {
testZipParentLayer(FileObject::getType);
}

@Test
public void testZipParentLayer_isAttached() throws Exception {
testZipParentLayer(FileObject::isAttached);
}

@Test
public void testZipParentLayer_isContentOpen() throws Exception {
testZipParentLayer(FileObject::isContentOpen);
}

@Test
public void testZipParentLayer_isExecutable() throws Exception {
testZipParentLayer(FileObject::isExecutable);
}

@Test
public void testZipParentLayer_isFile() throws Exception {
testZipParentLayer(FileObject::isFile);
}

@Test
public void testZipParentLayer_isFolder() throws Exception {
testZipParentLayer(FileObject::isFolder);
}

@Test
public void testZipParentLayer_isHidden() throws Exception {
testZipParentLayer(FileObject::isHidden);
}

@Test
public void testZipParentLayer_isReadable() throws Exception {
testZipParentLayer(FileObject::isReadable);
}

@Test
public void testZipParentLayer_isWriteable() throws Exception {
testZipParentLayer(FileObject::isWriteable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs2.provider.cczip;

import java.io.File;

import org.apache.commons.AbstractVfsTestCase;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
import org.apache.commons.vfs2.test.ProviderTestSuite;
import org.junit.Assert;

import junit.framework.Test;

/**
* Tests for the Zip file system.
*/
public class ZipProviderWithCharsetNullTestCase extends AbstractProviderTestConfig {
/**
* Creates the test suite for the zip file system.
*/
public static Test suite() throws Exception {
return new ProviderTestSuite(new ZipProviderWithCharsetNullTestCase(), true);
}

/**
* Prepares the file system manager.
*/
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
manager.addProvider("commonscompresszip", new CommonsCompressZipFileProvider());
manager.addExtensionMap("zip", "commonscompresszip");
manager.addMimeTypeMap("application/zip", "commonscompresszip");
}

/**
* Returns the base folder for read tests.
*/
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
final FileSystemOptions opts = new FileSystemOptions();
final CommonsCompressZipFileSystemConfigBuilder builder = CommonsCompressZipFileSystemConfigBuilder.getInstance();
// Tests null as the default.
builder.setCharset(opts, null);

final File zipFile = AbstractVfsTestCase.getTestResource("test.zip");
final String uri = "commonscompresszip:file:" + zipFile.getAbsolutePath() + "!/";
final FileObject resolvedFile = manager.resolveFile(uri, opts);
final FileSystem fileSystem = resolvedFile.getFileSystem();
Assert.assertTrue(fileSystem instanceof CommonsCompressZipFileSystem);
final CommonsCompressZipFileSystem zipFileSystem = (CommonsCompressZipFileSystem) fileSystem;
Assert.assertNull(zipFileSystem.getCharset());
return resolvedFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs2.provider.cczip;

import java.io.File;
import java.nio.charset.StandardCharsets;

import org.apache.commons.AbstractVfsTestCase;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
import org.apache.commons.vfs2.test.ProviderTestSuite;
import org.junit.Assert;

import junit.framework.Test;

/**
* Tests for the Zip file system.
*/
public class ZipProviderWithCharsetTestCase extends AbstractProviderTestConfig {
/**
* Creates the test suite for the zip file system.
*/
public static Test suite() throws Exception {
return new ProviderTestSuite(new ZipProviderWithCharsetTestCase(), true);
}

/**
* Prepares the file system manager.
*/
@Override
public void prepare(final DefaultFileSystemManager manager) throws Exception {
manager.addProvider("commonscompresszip", new CommonsCompressZipFileProvider());
manager.addExtensionMap("zip", "commonscompresszip");
manager.addMimeTypeMap("application/zip", "commonscompresszip");
}

/**
* Returns the base folder for read tests.
*/
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
final FileSystemOptions opts = new FileSystemOptions();
final CommonsCompressZipFileSystemConfigBuilder builder = CommonsCompressZipFileSystemConfigBuilder.getInstance();
// Tests the same charset as the default but we exercise having a Charset set.
builder.setCharset(opts, StandardCharsets.UTF_8);

final File zipFile = AbstractVfsTestCase.getTestResource("test.zip");
final String uri = "commonscompresszip:file:" + zipFile.getAbsolutePath() + "!/";
final FileObject resolvedFile = manager.resolveFile(uri, opts);
final FileSystem fileSystem = resolvedFile.getFileSystem();
Assert.assertTrue(fileSystem instanceof CommonsCompressZipFileSystem);
final CommonsCompressZipFileSystem zipFileSystem = (CommonsCompressZipFileSystem) fileSystem;
Assert.assertEquals(StandardCharsets.UTF_8, zipFileSystem.getCharset());
return resolvedFile;
}
}
Loading

0 comments on commit 6d2ef48

Please sign in to comment.