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.snappy;
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   * Tests for the snappy archiver
41   */
42  public class SnappyArchiverTest
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/archiveForSnappy.zip" ) );
52          zipArchiver.createArchive();
53          SnappyArchiver archiver = (SnappyArchiver) lookup( Archiver.ROLE, "snappy" );
54          String[] inputFiles = new String[ 1 ];
55          inputFiles[0] = "archiveForSnappy.zip";
56          archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null );
57          archiver.setDestFile( getTestFile( "target/output/archive.snappy" ) );
58          archiver.createArchive();
59      }
60  
61      public void testCreateEmptyArchive()
62          throws Exception
63      {
64          SnappyArchiver archiver = (SnappyArchiver) lookup( Archiver.ROLE, "snappy" );
65          archiver.setDestFile( getTestFile( "target/output/empty.snappy" ) );
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 snappyFile = new File( "target/output/pom.xml.snappy" );
82          SnappyArchiver SnappyArchiver = (SnappyArchiver) lookup( Archiver.ROLE, "snappy" );
83          SnappyArchiver.setDestFile( snappyFile );
84          SnappyArchiver.addFile( pomFile, "pom.xml" );
85          FileUtils.removePath( snappyFile.getPath() );
86          SnappyArchiver.createArchive();
87  
88          System.out.println( "Created: " + snappyFile.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( snappyFile, "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 Snappy 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 testsnappyIsForcedBehaviour() throws Exception
115     {
116         SnappyArchiver SnappyArchiver = (SnappyArchiver) createArchiver( "snappy" );
117 
118         assertTrue( SnappyArchiver.isSupportingForced() );
119         SnappyArchiver.createArchive();
120 
121         final long creationTime = SnappyArchiver.getDestFile().lastModified();
122 
123         waitUntilNewTimestamp( SnappyArchiver.getDestFile(), creationTime );
124 
125         SnappyArchiver = (SnappyArchiver) createArchiver( "snappy" );
126 
127         SnappyArchiver.setForced( true );
128         SnappyArchiver.createArchive();
129 
130         final long firstRunTime = SnappyArchiver.getDestFile().lastModified();
131 
132         assertFalse( creationTime == firstRunTime );
133 
134         SnappyArchiver = (SnappyArchiver) createArchiver( "snappy" );
135 
136         SnappyArchiver.setForced( false );
137         SnappyArchiver.createArchive();
138 
139         final long secondRunTime = SnappyArchiver.getDestFile().lastModified();
140 
141         assertEquals( firstRunTime, secondRunTime );
142     }
143 
144 }