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.gzip;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.InputStream;
29  import java.util.Arrays;
30  import java.util.zip.ZipEntry;
31  import java.util.zip.ZipFile;
32  import org.codehaus.plexus.archiver.Archiver;
33  import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
34  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
35  import org.codehaus.plexus.archiver.zip.ZipArchiver;
36  import org.codehaus.plexus.util.FileUtils;
37  import org.codehaus.plexus.util.IOUtil;
38  
39  /**
40   * @author Emmanuel Venisse
41   */
42  public class GZipArchiverTest
43      extends BasePlexusArchiverTest
44  {
45  
46      public void testCreateArchive()
47          throws Exception
48      {
49          ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
50          zipArchiver.addDirectory( getTestFile( "src" ) );
51          zipArchiver.setDestFile( getTestFile( "target/output/archiveForGzip.zip" ) );
52          zipArchiver.createArchive();
53          GZipArchiver archiver = (GZipArchiver) lookup( Archiver.ROLE, "gzip" );
54          String[] inputFiles = new String[ 1 ];
55          inputFiles[0] = "archiveForGzip.zip";
56          archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null );
57          archiver.setDestFile( getTestFile( "target/output/archive.gzip" ) );
58          archiver.createArchive();
59      }
60  
61  
62      public void testCreateEmptyArchive()
63          throws Exception
64      {
65          GZipArchiver archiver = (GZipArchiver) lookup( Archiver.ROLE, "gzip" );
66          archiver.setDestFile( getTestFile( "target/output/empty.gz" ) );
67          try
68          {
69              archiver.createArchive();
70  
71              fail( "Creating empty archive should throw EmptyArchiveException" );
72          }
73          catch ( EmptyArchiveException ignore )
74          {
75          }
76      }
77  
78      public void testCreateResourceCollection()
79          throws Exception
80      {
81          final File pomFile = new File( "pom.xml" );
82          final File gzFile = new File( "target/output/pom.xml.gz" );
83          GZipArchiver gzipArchiver = (GZipArchiver) lookup( Archiver.ROLE, "gzip" );
84          gzipArchiver.setDestFile( gzFile );
85          gzipArchiver.addFile( pomFile, "pom.xml" );
86          FileUtils.removePath( gzFile.getPath() );
87          gzipArchiver.createArchive();
88  
89          final File zipFile = new File( "target/output/pom.zip" );
90          ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
91          zipArchiver.setDestFile( zipFile );
92          zipArchiver.addArchivedFileSet( gzFile, "prfx/" );
93          FileUtils.removePath( zipFile.getPath() );
94          zipArchiver.createArchive();
95  
96          final ZipFile juZipFile = new ZipFile( zipFile );
97          final ZipEntry zipEntry = juZipFile.getEntry( "prfx/target/output/pom.xml" );
98          final InputStream archivePom = juZipFile.getInputStream( zipEntry );
99          final InputStream pom = new FileInputStream( pomFile );
100         assertTrue( Arrays.equals( IOUtil.toByteArray( pom ), IOUtil.toByteArray( archivePom ) ) );
101         archivePom.close();
102         pom.close();
103         juZipFile.close();
104     }
105 
106     /**
107      * Tests the .gzip archiver is forced set to true, and after that
108      * tests the behavior when the forced is set to false.
109      *
110      * @throws Exception
111      */
112     public void testTarGzIsForcedBehaviour() throws Exception
113     {
114         GZipArchiver gZipArchiver = (GZipArchiver) createArchiver( "gzip" );
115 
116         assertTrue( gZipArchiver.isSupportingForced() );
117         gZipArchiver.createArchive();
118 
119         final long creationTime = gZipArchiver.getDestFile().lastModified();
120 
121         waitUntilNewTimestamp( gZipArchiver.getDestFile(), creationTime );
122 
123         gZipArchiver = (GZipArchiver) createArchiver( "gzip" );
124 
125         gZipArchiver.setForced( true );
126         gZipArchiver.createArchive();
127 
128         final long firstRunTime = gZipArchiver.getDestFile().lastModified();
129 
130         assertFalse( creationTime == firstRunTime );
131 
132         gZipArchiver = (GZipArchiver) createArchiver( "gzip" );
133 
134         gZipArchiver.setForced( false );
135         gZipArchiver.createArchive();
136 
137         final long secondRunTime = gZipArchiver.getDestFile().lastModified();
138 
139         assertEquals( firstRunTime, secondRunTime );
140     }
141 
142 }