1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.tar;
18
19 import static org.junit.jupiter.api.Assertions.assertTrue;
20
21 import java.io.File;
22
23 import org.apache.commons.vfs2.FileObject;
24 import org.apache.commons.vfs2.FileSystemException;
25 import org.apache.commons.vfs2.FilesCache;
26 import org.apache.commons.vfs2.cache.WeakRefFilesCache;
27 import org.apache.commons.vfs2.impl.StandardFileSystemManager;
28 import org.junit.jupiter.api.Test;
29
30 public class TarFileSystemTest {
31
32 @Test
33 public void testTarFileUseDefaultFilesCache() throws FileSystemException {
34 testUseWeakRefFilesCache("tar", "src/test/resources/test-data/test.tar", null);
35 }
36
37 @Test
38 @SuppressWarnings("resource")
39 public void testTarFileUseWeakRefFilesCache() throws FileSystemException {
40 testUseWeakRefFilesCache("tar", "src/test/resources/test-data/test.tar", new WeakRefFilesCache());
41 }
42
43 @Test
44 public void testTbz2FileUseDefaultFilesCache() throws FileSystemException {
45 testUseWeakRefFilesCache("tbz2", "src/test/resources/test-data/test.tbz2", null);
46 }
47
48 @Test
49 @SuppressWarnings("resource")
50 public void testTbz2FileUseWeakRefFilesCache() throws FileSystemException {
51 testUseWeakRefFilesCache("tbz2", "src/test/resources/test-data/test.tbz2", new WeakRefFilesCache());
52 }
53
54 @Test
55 public void testTgzFileUseDefaultFilesCache() throws FileSystemException {
56 testUseWeakRefFilesCache("tgz", "src/test/resources/test-data/test.tgz", null);
57 }
58
59 @Test
60 @SuppressWarnings("resource")
61 public void testTgzFileUseWeakRefFilesCache() throws FileSystemException {
62 testUseWeakRefFilesCache("tgz", "src/test/resources/test-data/test.tgz", new WeakRefFilesCache());
63 }
64
65
66
67
68
69
70 private void testUseWeakRefFilesCache(final String scheme, final String filePath, final FilesCache filesCache)
71 throws FileSystemException {
72
73 final String fileUri = scheme + ":file:" + new File(filePath).getAbsolutePath();
74 FileObject fileObject = null;
75
76 try (StandardFileSystemManager manager = new StandardFileSystemManager()) {
77 if (filesCache != null) {
78 manager.setFilesCache(filesCache);
79 }
80 manager.init();
81
82 int cnt = 0;
83 while (cnt < 100_000) {
84 cnt++;
85
86
87 try {
88 fileObject = manager.resolveFile(fileUri);
89 assertTrue(fileObject.exists());
90 } finally {
91 FileObject.close(fileObject);
92 fileObject = null;
93 }
94
95
96 if (cnt % 200 == 0) {
97 System.gc();
98 }
99 }
100 }
101 }
102
103 }