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  import org.codehaus.plexus.PlexusTestCase;
28  import org.codehaus.plexus.archiver.tar.TarArchiver;
29  import org.codehaus.plexus.archiver.tar.TarLongFileMode;
30  
31  /**
32   * @author Daniel Krisher
33   */
34  public class EmptyDirectoryTest
35      extends PlexusTestCase
36  {
37  
38      public void testZipArchiver()
39          throws Exception
40      {
41          testEmptyDirectory( "zip", (Archiver) lookup( Archiver.ROLE, "zip" ) );
42      }
43  
44      public void testJarArchiver()
45          throws Exception
46      {
47          // No JAR UnArchiver implementation :(
48  //        testEmptyDirectory( "jar" );
49      }
50  
51      public void testTarArchiver()
52          throws Exception
53      {
54          final TarArchiver tar = (TarArchiver) lookup( Archiver.ROLE, "tar" );
55          tar.setLongfile( TarLongFileMode.posix );
56          testEmptyDirectory( "tar", tar );
57      }
58  
59      private void testEmptyDirectory( String role, Archiver archiver )
60          throws Exception
61      {
62  
63          // Should default to true...
64          assertTrue( archiver.getIncludeEmptyDirs() );
65  
66          // create an empty directory to store in the zip archive
67          File emptyDir = getTestFile( "target/output/emptyTest/TmpEmptyDir" );
68  
69          // delete it if it exists to ensure it is actually empty
70          if ( emptyDir.exists() )
71          {
72              emptyDir.delete();
73          }
74          emptyDir.mkdirs();
75          archiver.addDirectory( emptyDir.getParentFile() );
76  
77          File archive = getTestFile( "target/output/emptyDirArchive.zip" );
78          if ( archive.exists() )
79          {
80              archive.delete();
81          }
82  
83          archiver.setDestFile( archive );
84          archiver.createArchive();
85  
86          // delete the empty dir, we will extract it from the archive
87          emptyDir.delete();
88  
89          // Check the content of the archive by extracting it
90          UnArchiver unArchiver = (UnArchiver) lookup( UnArchiver.ROLE, role );
91          unArchiver.setSourceFile( archive );
92  
93          unArchiver.setDestDirectory( getTestFile( "target/output/emptyTest" ) );
94          unArchiver.extract();
95  
96          assertTrue( emptyDir.exists() );
97          assertTrue( emptyDir.isDirectory() );
98      }
99  
100 }