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