Skip to content

Commit 6d2ef48

Browse files
[VFS-676] : Add tests for commons compress zip provider
1 parent 6673613 commit 6d2ef48

10 files changed

Lines changed: 1073 additions & 0 deletions
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.vfs2.provider.cczip;
19+
20+
import java.io.File;
21+
22+
import org.apache.commons.vfs2.FileObject;
23+
import org.apache.commons.vfs2.FileSystemException;
24+
import org.apache.commons.vfs2.VFS;
25+
import org.apache.commons.vfs2.cache.OnCallRefreshFileObject;
26+
import org.apache.commons.vfs2.function.VfsConsumer;
27+
import org.junit.After;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
public class Jira733TestCase {
33+
34+
@After
35+
@Before
36+
public void reset() throws FileSystemException {
37+
VFS.reset();
38+
}
39+
40+
@Test
41+
public void testZipParentLayer() throws Exception {
42+
final File file = new File("src/test/resources/test-data/test.zip");
43+
final String nestedPath = "commonscompresszip:" + file.getAbsolutePath() + "!/read-tests/file1.txt";
44+
try (final FileObject fileObject = VFS.getManager().resolveFile(nestedPath);
45+
final FileObject wrappedFileObject = new OnCallRefreshFileObject(fileObject)) {
46+
// VFS.getManager().getFilesCache().close();
47+
Assert.assertNotNull("getParentLayer() 1", wrappedFileObject.getFileSystem().getParentLayer());
48+
wrappedFileObject.exists();
49+
wrappedFileObject.getContent();
50+
Assert.assertNotNull("getParentLayer() 2", wrappedFileObject.getFileSystem().getParentLayer());
51+
}
52+
}
53+
54+
private void testZipParentLayer(final VfsConsumer<FileObject> consumer) throws Exception {
55+
final File file = new File("src/test/resources/test-data/test.zip");
56+
Assert.assertTrue(file.exists());
57+
final String nestedPath = "commonscompresszip:" + file.getAbsolutePath() + "!/read-tests/file1.txt";
58+
try (final FileObject fileObject = VFS.getManager().resolveFile(nestedPath);
59+
final FileObject wrappedFileObject = new OnCallRefreshFileObject(fileObject)) {
60+
Assert.assertTrue(fileObject instanceof CommonsCompressZipFileObject);
61+
@SuppressWarnings({"unused", "resource"}) final CommonsCompressZipFileObject zipFileObject = (CommonsCompressZipFileObject) fileObject;
62+
Assert.assertNotNull("getParentLayer() 1", wrappedFileObject.getFileSystem().getParentLayer());
63+
consumer.accept(wrappedFileObject);
64+
Assert.assertNotNull("getParentLayer() 2", wrappedFileObject.getFileSystem().getParentLayer());
65+
}
66+
}
67+
68+
@Test
69+
public void testZipParentLayer_exists() throws Exception {
70+
testZipParentLayer(FileObject::exists);
71+
}
72+
73+
@Test
74+
public void testZipParentLayer_exists_getContents() throws Exception {
75+
testZipParentLayer(fileObject -> {
76+
fileObject.exists();
77+
fileObject.getContent();
78+
});
79+
}
80+
81+
@Test
82+
public void testZipParentLayer_getChildren() throws Exception {
83+
testZipParentLayer(fileObject -> fileObject.getParent().getChildren());
84+
}
85+
86+
@Test
87+
public void testZipParentLayer_getContents() throws Exception {
88+
testZipParentLayer(FileObject::getContent);
89+
}
90+
91+
@Test
92+
public void testZipParentLayer_getType() throws Exception {
93+
testZipParentLayer(FileObject::getType);
94+
}
95+
96+
@Test
97+
public void testZipParentLayer_isAttached() throws Exception {
98+
testZipParentLayer(FileObject::isAttached);
99+
}
100+
101+
@Test
102+
public void testZipParentLayer_isContentOpen() throws Exception {
103+
testZipParentLayer(FileObject::isContentOpen);
104+
}
105+
106+
@Test
107+
public void testZipParentLayer_isExecutable() throws Exception {
108+
testZipParentLayer(FileObject::isExecutable);
109+
}
110+
111+
@Test
112+
public void testZipParentLayer_isFile() throws Exception {
113+
testZipParentLayer(FileObject::isFile);
114+
}
115+
116+
@Test
117+
public void testZipParentLayer_isFolder() throws Exception {
118+
testZipParentLayer(FileObject::isFolder);
119+
}
120+
121+
@Test
122+
public void testZipParentLayer_isHidden() throws Exception {
123+
testZipParentLayer(FileObject::isHidden);
124+
}
125+
126+
@Test
127+
public void testZipParentLayer_isReadable() throws Exception {
128+
testZipParentLayer(FileObject::isReadable);
129+
}
130+
131+
@Test
132+
public void testZipParentLayer_isWriteable() throws Exception {
133+
testZipParentLayer(FileObject::isWriteable);
134+
}
135+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.vfs2.provider.cczip;
18+
19+
import java.io.File;
20+
21+
import org.apache.commons.AbstractVfsTestCase;
22+
import org.apache.commons.vfs2.FileObject;
23+
import org.apache.commons.vfs2.FileSystem;
24+
import org.apache.commons.vfs2.FileSystemManager;
25+
import org.apache.commons.vfs2.FileSystemOptions;
26+
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
27+
import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
28+
import org.apache.commons.vfs2.test.ProviderTestSuite;
29+
import org.junit.Assert;
30+
31+
import junit.framework.Test;
32+
33+
/**
34+
* Tests for the Zip file system.
35+
*/
36+
public class ZipProviderWithCharsetNullTestCase extends AbstractProviderTestConfig {
37+
/**
38+
* Creates the test suite for the zip file system.
39+
*/
40+
public static Test suite() throws Exception {
41+
return new ProviderTestSuite(new ZipProviderWithCharsetNullTestCase(), true);
42+
}
43+
44+
/**
45+
* Prepares the file system manager.
46+
*/
47+
@Override
48+
public void prepare(final DefaultFileSystemManager manager) throws Exception {
49+
manager.addProvider("commonscompresszip", new CommonsCompressZipFileProvider());
50+
manager.addExtensionMap("zip", "commonscompresszip");
51+
manager.addMimeTypeMap("application/zip", "commonscompresszip");
52+
}
53+
54+
/**
55+
* Returns the base folder for read tests.
56+
*/
57+
@Override
58+
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
59+
final FileSystemOptions opts = new FileSystemOptions();
60+
final CommonsCompressZipFileSystemConfigBuilder builder = CommonsCompressZipFileSystemConfigBuilder.getInstance();
61+
// Tests null as the default.
62+
builder.setCharset(opts, null);
63+
64+
final File zipFile = AbstractVfsTestCase.getTestResource("test.zip");
65+
final String uri = "commonscompresszip:file:" + zipFile.getAbsolutePath() + "!/";
66+
final FileObject resolvedFile = manager.resolveFile(uri, opts);
67+
final FileSystem fileSystem = resolvedFile.getFileSystem();
68+
Assert.assertTrue(fileSystem instanceof CommonsCompressZipFileSystem);
69+
final CommonsCompressZipFileSystem zipFileSystem = (CommonsCompressZipFileSystem) fileSystem;
70+
Assert.assertNull(zipFileSystem.getCharset());
71+
return resolvedFile;
72+
}
73+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.vfs2.provider.cczip;
18+
19+
import java.io.File;
20+
import java.nio.charset.StandardCharsets;
21+
22+
import org.apache.commons.AbstractVfsTestCase;
23+
import org.apache.commons.vfs2.FileObject;
24+
import org.apache.commons.vfs2.FileSystem;
25+
import org.apache.commons.vfs2.FileSystemManager;
26+
import org.apache.commons.vfs2.FileSystemOptions;
27+
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
28+
import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
29+
import org.apache.commons.vfs2.test.ProviderTestSuite;
30+
import org.junit.Assert;
31+
32+
import junit.framework.Test;
33+
34+
/**
35+
* Tests for the Zip file system.
36+
*/
37+
public class ZipProviderWithCharsetTestCase extends AbstractProviderTestConfig {
38+
/**
39+
* Creates the test suite for the zip file system.
40+
*/
41+
public static Test suite() throws Exception {
42+
return new ProviderTestSuite(new ZipProviderWithCharsetTestCase(), true);
43+
}
44+
45+
/**
46+
* Prepares the file system manager.
47+
*/
48+
@Override
49+
public void prepare(final DefaultFileSystemManager manager) throws Exception {
50+
manager.addProvider("commonscompresszip", new CommonsCompressZipFileProvider());
51+
manager.addExtensionMap("zip", "commonscompresszip");
52+
manager.addMimeTypeMap("application/zip", "commonscompresszip");
53+
}
54+
55+
/**
56+
* Returns the base folder for read tests.
57+
*/
58+
@Override
59+
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
60+
final FileSystemOptions opts = new FileSystemOptions();
61+
final CommonsCompressZipFileSystemConfigBuilder builder = CommonsCompressZipFileSystemConfigBuilder.getInstance();
62+
// Tests the same charset as the default but we exercise having a Charset set.
63+
builder.setCharset(opts, StandardCharsets.UTF_8);
64+
65+
final File zipFile = AbstractVfsTestCase.getTestResource("test.zip");
66+
final String uri = "commonscompresszip:file:" + zipFile.getAbsolutePath() + "!/";
67+
final FileObject resolvedFile = manager.resolveFile(uri, opts);
68+
final FileSystem fileSystem = resolvedFile.getFileSystem();
69+
Assert.assertTrue(fileSystem instanceof CommonsCompressZipFileSystem);
70+
final CommonsCompressZipFileSystem zipFileSystem = (CommonsCompressZipFileSystem) fileSystem;
71+
Assert.assertEquals(StandardCharsets.UTF_8, zipFileSystem.getCharset());
72+
return resolvedFile;
73+
}
74+
}

0 commit comments

Comments
 (0)