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.bzip2;
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 BZip2ArchiverTest
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/archiveForbz2.zip" ) );
52          zipArchiver.createArchive();
53          BZip2Archiver archiver = (BZip2Archiver) lookup( Archiver.ROLE, "bzip2" );
54          String[] inputFiles = new String[ 1 ];
55          inputFiles[0] = "archiveForbz2.zip";
56          archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null );
57          archiver.setDestFile( getTestFile( "target/output/archive.bz2" ) );
58          archiver.createArchive();
59      }
60  
61      public void testCreateEmptyArchive()
62          throws Exception
63      {
64          BZip2Archiver archiver = (BZip2Archiver) lookup( Archiver.ROLE, "bzip2" );
65          archiver.setDestFile( getTestFile( "target/output/empty.bz2" ) );
66          try
67          {
68              archiver.createArchive();
69  
70              fail( "Creating empty archive should throw EmptyArchiveException" );
71          }
72          catch ( EmptyArchiveException ignore )
73          {
74          }
75      }
76  
77      public void testCreateResourceCollection()
78          throws Exception
79      {
80          final File pomFile = new File( "pom.xml" );
81          final File bz2File = new File( "target/output/pom.xml.bz2" );
82          BZip2Archiver bzip2Archiver = (BZip2Archiver) lookup( Archiver.ROLE, "bzip2" );
83          bzip2Archiver.setDestFile( bz2File );
84          bzip2Archiver.addFile( pomFile, "pom.xml" );
85          FileUtils.removePath( bz2File.getPath() );
86          bzip2Archiver.createArchive();
87  
88          System.out.println( "Created: " + bz2File.getAbsolutePath() );
89  
90          final File zipFile = new File( "target/output/pom.zip" );
91          ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
92          zipArchiver.setDestFile( zipFile );
93          zipArchiver.addArchivedFileSet( bz2File, "prfx/" );
94          FileUtils.removePath( zipFile.getPath() );
95          zipArchiver.createArchive();
96  
97          final ZipFile juZipFile = new ZipFile( zipFile );
98          final ZipEntry zipEntry = juZipFile.getEntry( "prfx/target/output/pom.xml" );
99          final InputStream archivePom = juZipFile.getInputStream( zipEntry );
100         final InputStream pom = new FileInputStream( pomFile );
101 
102         assertTrue( Arrays.equals( IOUtil.toByteArray( pom ), IOUtil.toByteArray( archivePom ) ) );
103         archivePom.close();
104         pom.close();
105         juZipFile.close();
106     }
107 
108     /**
109      * Tests the .bzip2 archiver is forced set to true, and after that
110      * tests the behavior when the forced is set to false.
111      *
112      * @throws Exception
113      */
114     public void testBz2IsForcedBehaviour() throws Exception
115     {
116         BZip2Archiver bZip2Archiver = (BZip2Archiver) createArchiver( "bzip2" );
117 
118         assertTrue( bZip2Archiver.isSupportingForced() );
119         bZip2Archiver.createArchive();
120 
121         final long creationTime = bZip2Archiver.getDestFile().lastModified();
122 
123         waitUntilNewTimestamp( bZip2Archiver.getDestFile(), creationTime );
124 
125         bZip2Archiver = (BZip2Archiver) createArchiver( "bzip2" );
126 
127         bZip2Archiver.setForced( true );
128         bZip2Archiver.createArchive();
129 
130         final long firstRunTime = bZip2Archiver.getDestFile().lastModified();
131 
132         assertFalse( creationTime == firstRunTime );
133 
134         bZip2Archiver = (BZip2Archiver) createArchiver( "bzip2" );
135 
136         bZip2Archiver.setForced( false );
137         bZip2Archiver.createArchive();
138 
139         final long secondRunTime = bZip2Archiver.getDestFile().lastModified();
140 
141         assertEquals( firstRunTime, secondRunTime );
142     }
143 
144 }