View Javadoc
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.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") // Managed by the file system, no need to close.
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") // Managed by the file system, no need to close.
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") // Managed by the file system, no need to close.
61      public void testTgzFileUseWeakRefFilesCache() throws FileSystemException {
62          testUseWeakRefFilesCache("tgz", "src/test/resources/test-data/test.tgz", new WeakRefFilesCache());
63      }
64  
65      /**
66       * Sets a file system's file cache to use WeakReference, and test resolve file after GC.
67       *
68       * @param filesCache TODO
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                  // resolve file, assert fileObject exist. clear fileObject to null and wait GC.
87                  try {
88                      fileObject = manager.resolveFile(fileUri);
89                      assertTrue(fileObject.exists());
90                  } finally {
91                      FileObject.close(fileObject);
92                      fileObject = null;
93                  }
94  
95                  // every 200 times suggest one gc
96                  if (cnt % 200 == 0) {
97                      System.gc();
98                  }
99              }
100         }
101     }
102 
103 }