View Javadoc
1   package org.codehaus.plexus.archiver.tar;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.nio.file.Files;
7   import java.util.Arrays;
8   import java.util.Enumeration;
9   
10  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
11  import org.codehaus.plexus.archiver.Archiver;
12  import org.codehaus.plexus.archiver.TestSupport;
13  import org.codehaus.plexus.archiver.bzip2.BZip2Compressor;
14  import org.codehaus.plexus.archiver.gzip.GZipCompressor;
15  import org.codehaus.plexus.archiver.util.Compressor;
16  import org.codehaus.plexus.util.FileUtils;
17  import org.codehaus.plexus.util.IOUtil;
18  import org.junit.jupiter.api.Test;
19  
20  import static org.codehaus.plexus.components.io.resources.ResourceFactory.createResource;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  /**
24   * Test case for {@link TarFile}.
25   */
26  class TarFileTest extends TestSupport {
27  
28      private interface TarFileCreator {
29  
30          TarFile newTarFile(File file) throws IOException;
31      }
32  
33      /**
34       * Test for the uncompressed tar file, {@link TarFile}.
35       */
36      @Test
37      void testTarFile() throws Exception {
38          testTarFile(null, null, new TarFileCreator() {
39  
40              @Override
41              public TarFile newTarFile(File file) throws IOException {
42                  return new TarFile(file);
43              }
44          });
45      }
46  
47      /**
48       * Test for the gzip compressed tar file, {@link GZipTarFile}.
49       */
50      @Test
51      void testGZipTarFile() throws Exception {
52          final GZipCompressor compressor = new GZipCompressor();
53          testTarFile(compressor, ".gz", new TarFileCreator() {
54  
55              @Override
56              public TarFile newTarFile(File file) throws IOException {
57                  return new GZipTarFile(file);
58              }
59          });
60      }
61  
62      /**
63       * Test for the bzip2 compressed tar file, {@link BZip2TarFile}.
64       */
65      @Test
66      void testBZip2TarFile() throws Exception {
67          final BZip2Compressor compressor = new BZip2Compressor();
68          testTarFile(compressor, ".bz2", new TarFileCreator() {
69  
70              @Override
71              public TarFile newTarFile(File file) throws IOException {
72                  return new BZip2TarFile(file);
73              }
74          });
75      }
76  
77      private void testTarFile(Compressor compressor, String extension, TarFileCreator tarFileCreator) throws Exception {
78          File file = new File("target/output/TarFileTest.tar");
79          final TarArchiver archiver = (TarArchiver) lookup(Archiver.class, "tar");
80          archiver.setLongfile(TarLongFileMode.posix);
81          archiver.setDestFile(file);
82          archiver.addDirectory(new File("src"));
83          FileUtils.removePath(file.getPath());
84          archiver.createArchive();
85          if (compressor != null) {
86              final File compressedFile = new File(file.getPath() + extension);
87              compressor.setSource(createResource(file, file.getName()));
88              compressor.setDestFile(compressedFile);
89              compressor.compress();
90              compressor.close();
91              file = compressedFile;
92          }
93          final TarFile tarFile = tarFileCreator.newTarFile(file);
94  
95          for (Enumeration en = tarFile.getEntries(); en.hasMoreElements(); ) {
96              final TarArchiveEntry te = (TarArchiveEntry) en.nextElement();
97              if (te.isDirectory() || te.isSymbolicLink()) {
98                  continue;
99              }
100             final File teFile = new File("src", te.getName());
101             final InputStream teStream = tarFile.getInputStream(te);
102             final InputStream fileStream = Files.newInputStream(teFile.toPath());
103             assertTrue(Arrays.equals(IOUtil.toByteArray(teStream), IOUtil.toByteArray(fileStream)));
104             teStream.close();
105             fileStream.close();
106         }
107 
108         tarFile.close();
109     }
110 }