View Javadoc
1   /*
2    * Copyright 2016 Codehaus.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.archiver.xz;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.InputStream;
21  import java.util.Arrays;
22  import java.util.zip.ZipEntry;
23  import java.util.zip.ZipFile;
24  import org.codehaus.plexus.archiver.Archiver;
25  import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
26  import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
27  import org.codehaus.plexus.archiver.zip.ZipArchiver;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.codehaus.plexus.util.IOUtil;
30  import static junit.framework.TestCase.assertEquals;
31  import static junit.framework.TestCase.assertFalse;
32  import static junit.framework.TestCase.assertTrue;
33  import static org.codehaus.plexus.PlexusTestCase.getTestFile;
34  
35  /**
36   * @author philip.lourandos
37   * @since 3.3
38   */
39  public class XzArchiverTest extends BasePlexusArchiverTest
40  {
41  
42      public void testCreateArchive()
43          throws Exception
44      {
45          ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
46          zipArchiver.addDirectory( getTestFile( "src" ) );
47          zipArchiver.setDestFile( getTestFile( "target/output/archiveForxz.zip" ) );
48          zipArchiver.createArchive();
49  
50          XZArchiver archiver = (XZArchiver) lookup( Archiver.ROLE, "xz" );
51          String[] inputFiles = new String[ 1 ];
52          inputFiles[0] = "archiveForxz.zip";
53  
54          File targetOutputFile = getTestFile( "target/output/archive.xz" );
55          if ( targetOutputFile.exists() )
56          {
57              FileUtils.fileDelete( targetOutputFile.getPath() );
58          }
59          assertFalse( targetOutputFile.exists() );
60  
61          archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null );
62          archiver.setDestFile( targetOutputFile );
63          archiver.createArchive();
64  
65          assertTrue( targetOutputFile.exists() );
66      }
67  
68      public void testCreateEmptyArchive()
69          throws Exception
70      {
71          XZArchiver archiver = (XZArchiver) lookup( Archiver.ROLE, "xz" );
72          archiver.setDestFile( getTestFile( "target/output/empty.xz" ) );
73          try
74          {
75              archiver.createArchive();
76  
77              fail( "Creating empty archive should throw EmptyArchiveException" );
78          }
79          catch ( EmptyArchiveException ignore )
80          {
81          }
82      }
83  
84      public void testCreateResourceCollection() throws Exception
85      {
86          final File pomFile = new File( "pom.xml" );
87          final File xzFile = new File( "target/output/pom.xml.xz" );
88          XZArchiver xzArchiver = (XZArchiver) lookup( Archiver.ROLE, "xz" );
89          xzArchiver.setDestFile( xzFile );
90          xzArchiver.addFile( pomFile, "pom.xml" );
91          FileUtils.removePath( xzFile.getPath() );
92          xzArchiver.createArchive();
93  
94          System.out.println( "Created: " + xzFile.getAbsolutePath() );
95  
96          final File zipFile = new File( "target/output/pomxz.zip" );
97          ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
98          zipArchiver.setDestFile( zipFile );
99          zipArchiver.addArchivedFileSet( xzFile, "prfx/" );
100         FileUtils.removePath( zipFile.getPath() );
101         zipArchiver.createArchive();
102 
103         final ZipFile juZipFile = new ZipFile( zipFile );
104         final ZipEntry zipEntry = juZipFile.getEntry( "prfx/target/output/pom.xml" );
105         final InputStream archivePom = juZipFile.getInputStream( zipEntry );
106         final InputStream pom = new FileInputStream( pomFile );
107 
108         assertTrue( Arrays.equals( IOUtil.toByteArray( pom ), IOUtil.toByteArray( archivePom ) ) );
109         archivePom.close();
110         pom.close();
111         juZipFile.close();
112     }
113 
114     /**
115      * Tests the .xz archiver is forced set to true, and after that
116      * tests the behavior when the forced is set to false.
117      *
118      * @throws Exception
119      */
120     public void testXzIsForcedBehaviour() throws Exception
121     {
122         XZArchiver xzArchiver = (XZArchiver) createArchiver( "xz" );
123 
124         assertTrue( xzArchiver.isSupportingForced() );
125         xzArchiver.createArchive();
126 
127         final long creationTime = xzArchiver.getDestFile().lastModified();
128 
129         waitUntilNewTimestamp( xzArchiver.getDestFile(), creationTime );
130 
131         xzArchiver = (XZArchiver) createArchiver( "xz" );
132 
133         xzArchiver.setForced( true );
134         xzArchiver.createArchive();
135 
136         final long firstRunTime = xzArchiver.getDestFile().lastModified();
137 
138         assertFalse( creationTime == firstRunTime );
139 
140         xzArchiver = (XZArchiver) createArchiver( "xz" );
141 
142         xzArchiver.setForced( false );
143         xzArchiver.createArchive();
144 
145         final long secondRunTime = xzArchiver.getDestFile().lastModified();
146 
147         assertEquals( firstRunTime, secondRunTime );
148     }
149 
150 }