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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.File;
24  import java.io.OutputStream;
25  import java.io.PipedInputStream;
26  import java.io.PipedOutputStream;
27  import java.nio.file.Files;
28  import java.util.Arrays;
29  import java.util.List;
30  
31  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
32  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
33  import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
34  import org.apache.commons.compress.compressors.CompressorStreamFactory;
35  import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
36  import org.apache.commons.compress.utils.IOUtils;
37  import org.apache.commons.vfs2.CacheStrategy;
38  import org.apache.commons.vfs2.FileObject;
39  import org.apache.commons.vfs2.cache.SoftRefFilesCache;
40  import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
41  import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider;
42  import org.junit.jupiter.api.BeforeEach;
43  import org.junit.jupiter.api.Test;
44  
45  //@SuppressWarnings("nls")
46  public class LargeTarTest {
47  
48      private final static String baseDir = "target/test-classes/test-data/";
49  
50      private final static String largeFilePath = baseDir;
51      private final static String largeFileName = "largefile";
52      private DefaultFileSystemManager manager;
53  
54      // @SuppressWarnings("unused")
55      protected void createLargeFile(final String path, final String name) throws Exception {
56          final long _1K = 1024;
57          final long _1M = 1024 * _1K;
58          // long _256M = 256 * _1M;
59          // long _512M = 512 * _1M;
60          final long _1G = 1024 * _1M;
61  
62          // File size of 3 GB
63          final long fileSize = 3 * _1G;
64  
65          final File tarGzFile = new File(path + name + ".tar.gz");
66  
67          if (!tarGzFile.exists()) {
68              System.out.println(
69                      "This test is a bit slow. It needs to write 3GB of data as a compressed file (approx. 3MB) to your hard drive");
70  
71              final PipedOutputStream outTarFileStream = new PipedOutputStream();
72              final PipedInputStream inTarFileStream = new PipedInputStream(outTarFileStream);
73  
74              final Thread source = new Thread(() -> {
75                  final byte[] ba_1k = new byte[(int) _1K];
76                  Arrays.fill(ba_1k, (byte) 'a');
77                  try {
78                      final TarArchiveOutputStream outTarStream = (TarArchiveOutputStream) new ArchiveStreamFactory()
79                              .createArchiveOutputStream(ArchiveStreamFactory.TAR, outTarFileStream);
80                      // Create archive contents
81                      final TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name + ".txt");
82                      tarArchiveEntry.setSize(fileSize);
83  
84                      outTarStream.putArchiveEntry(tarArchiveEntry);
85                      for (long i = 0; i < fileSize; i += ba_1k.length) {
86                          outTarStream.write(ba_1k);
87                      }
88                      outTarStream.closeArchiveEntry();
89                      outTarStream.close();
90                      outTarFileStream.close();
91                  } catch (final Exception e) {
92                      e.printStackTrace();
93                  }
94              });
95              source.start();
96  
97              final File gzFile = new File(path + name + ".tar.gz");
98              // Create compressed archive
99              final OutputStream outGzipFileStream = Files.newOutputStream(gzFile.toPath());
100 
101             final GzipCompressorOutputStream outGzipStream = (GzipCompressorOutputStream) new CompressorStreamFactory()
102                     .createCompressorOutputStream(CompressorStreamFactory.GZIP, outGzipFileStream);
103 
104             IOUtils.copy(inTarFileStream, outGzipStream);
105             inTarFileStream.close();
106 
107             outGzipStream.close();
108             outGzipFileStream.close();
109 
110         }
111     }
112 
113     protected boolean endsWith(final String testString, final String[] testList) {
114         for (final String string : testList) {
115             if (testString.endsWith(string)) {
116                 return true;
117             }
118         }
119         return false;
120     }
121 
122     /*
123      * public void testFileCheck() throws Exception { String[] expectedFiles = { "plugins.tsv", "languages.tsv",
124      * "browser_type.tsv", "timezones.tsv", "color_depth.tsv", "resolution.tsv", "connection_type.tsv",
125      * "search_engines.tsv", "javascript_version.tsv", "operating_systems.tsv", "country.tsv", "browser.tsv" };
126      *
127      * fileCheck(expectedFiles, "tar:file://c:/temp/data/data/data-small.tar"); }
128      */
129 
130     protected void fileCheck(final String[] expectedFiles, final String tarFile) throws Exception {
131         assertNotNull(manager);
132         final FileObject file = manager.resolveFile(tarFile);
133 
134         assertNotNull(file);
135         final List<FileObject> files = Arrays.asList(file.getChildren());
136 
137         assertNotNull(files);
138         for (final String expectedFile : expectedFiles) {
139             assertTrue(fileExists(expectedFile, files), () -> "Expected file not found: " + expectedFile);
140         }
141     }
142 
143     /**
144      * Search for the expected file in a given list, without using the full path.
145      *
146      * @param expectedFile the expected file.
147      * @param files a list of files to search.
148      * @return {@code true} if {@code expectedFile} is in {@code files}.
149      */
150     protected boolean fileExists(final String expectedFile, final List<FileObject> files) {
151         for (final FileObject file : files) {
152             if (file.getName().getBaseName().equals(expectedFile)) {
153                 return true;
154             }
155         }
156         return false;
157     }
158 
159     @BeforeEach
160     public void setUp() throws Exception {
161         manager = new DefaultFileSystemManager();
162 
163         manager.setFilesCache(new SoftRefFilesCache());
164         manager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
165 
166         manager.addProvider("file", new DefaultLocalFileProvider());
167         manager.addProvider("tgz", new TarFileProvider());
168         manager.addProvider("tar", new TarFileProvider());
169 
170         new File(baseDir).mkdir(); // if test is run standalone
171         createLargeFile(largeFilePath, largeFileName);
172     }
173 
174     @Test
175     public void testLargeFile() throws Exception {
176         final File realFile = new File(largeFilePath + largeFileName + ".tar.gz");
177 
178         final FileObject file = manager.resolveFile("tgz:file://" + realFile.getCanonicalPath() + "!/");
179 
180         assertNotNull(file);
181         final List<FileObject> files = Arrays.asList(file.getChildren());
182 
183         assertNotNull(files);
184         assertEquals(1, files.size());
185         final FileObject f = files.get(0);
186 
187         assertEquals(f.getName().getBaseName(), largeFileName + ".txt", () -> "Expected file not found: " + largeFileName + ".txt");
188     }
189 
190 }