View Javadoc
1   /*
2    * The MIT License
3    *
4    * Copyright (c) 2004, The Codehaus
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.codehaus.plexus.archiver;
25  
26  import java.io.File;
27  
28  import org.codehaus.plexus.archiver.tar.TarArchiver;
29  import org.codehaus.plexus.archiver.tar.TarLongFileMode;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  /**
35   * @author Daniel Krisher
36   */
37  class EmptyDirectoryTest extends TestSupport {
38  
39      @Test
40      void testZipArchiver() throws Exception {
41          testEmptyDirectory("zip", lookup(Archiver.class, "zip"));
42      }
43  
44      @Test
45      void testJarArchiver() throws Exception {
46          // No JAR UnArchiver implementation :(
47          //        testEmptyDirectory( "jar" );
48      }
49  
50      @Test
51      void testTarArchiver() throws Exception {
52          final TarArchiver tar = (TarArchiver) lookup(Archiver.class, "tar");
53          tar.setLongfile(TarLongFileMode.posix);
54          testEmptyDirectory("tar", tar);
55      }
56  
57      private void testEmptyDirectory(String role, Archiver archiver) throws Exception {
58  
59          // Should default to true...
60          assertTrue(archiver.getIncludeEmptyDirs());
61  
62          // create an empty directory to store in the zip archive
63          File emptyDir = getTestFile("target/output/emptyTest/TmpEmptyDir");
64  
65          // delete it if it exists to ensure it is actually empty
66          if (emptyDir.exists()) {
67              emptyDir.delete();
68          }
69          emptyDir.mkdirs();
70          archiver.addDirectory(emptyDir.getParentFile());
71  
72          File archive = getTestFile("target/output/emptyDirArchive.zip");
73          if (archive.exists()) {
74              archive.delete();
75          }
76  
77          archiver.setDestFile(archive);
78          archiver.createArchive();
79  
80          // delete the empty dir, we will extract it from the archive
81          emptyDir.delete();
82  
83          // Check the content of the archive by extracting it
84          UnArchiver unArchiver = lookup(UnArchiver.class, role);
85          unArchiver.setSourceFile(archive);
86  
87          unArchiver.setDestDirectory(getTestFile("target/output/emptyTest"));
88          unArchiver.extract();
89  
90          assertTrue(emptyDir.exists());
91          assertTrue(emptyDir.isDirectory());
92      }
93  }