|
| 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 static junit.framework.TestCase.assertEquals; |
| 25 | +import static junit.framework.TestCase.assertFalse; |
| 26 | +import static junit.framework.TestCase.assertTrue; |
| 27 | +import static org.codehaus.plexus.PlexusTestCase.getTestFile; |
| 28 | +import org.codehaus.plexus.archiver.Archiver; |
| 29 | +import org.codehaus.plexus.archiver.BasePlexusArchiverTest; |
| 30 | +import org.codehaus.plexus.archiver.xz.XZArchiver; |
| 31 | +import org.codehaus.plexus.archiver.zip.ZipArchiver; |
| 32 | +import org.codehaus.plexus.util.FileUtils; |
| 33 | +import org.codehaus.plexus.util.IOUtil; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * @author philip.lourandos |
| 38 | + */ |
| 39 | +public class XzArchiverTest extends BasePlexusArchiverTest |
| 40 | +{ |
| 41 | + public void testCreateArchive() |
| 42 | + throws Exception |
| 43 | + { |
| 44 | + ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" ); |
| 45 | + zipArchiver.addDirectory( getTestFile( "src" ) ); |
| 46 | + zipArchiver.setDestFile( getTestFile( "target/output/archiveForxz.zip" ) ); |
| 47 | + zipArchiver.createArchive(); |
| 48 | + |
| 49 | + XZArchiver archiver = (XZArchiver) lookup( Archiver.ROLE, "xz" ); |
| 50 | + String[] inputFiles = new String[ 1 ]; |
| 51 | + inputFiles[ 0 ] = "archiveForxz.zip"; |
| 52 | + |
| 53 | + File targetOutputFile = getTestFile( "target/output/archive.xz" ); |
| 54 | + assertFalse( targetOutputFile.exists() ); |
| 55 | + |
| 56 | + archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null ); |
| 57 | + archiver.setDestFile( targetOutputFile ); |
| 58 | + archiver.createArchive(); |
| 59 | + |
| 60 | + assertTrue( targetOutputFile.exists() ); |
| 61 | + } |
| 62 | + |
| 63 | + public void testCreateResourceCollection() throws Exception |
| 64 | + { |
| 65 | + final File pomFile = new File("pom.xml"); |
| 66 | + final File xzFile = new File( "target/output/pom.xml.xz" ); |
| 67 | + XZArchiver xzArchiver = (XZArchiver) lookup( Archiver.ROLE, "xz" ); |
| 68 | + xzArchiver.setDestFile( xzFile ); |
| 69 | + xzArchiver.addFile( pomFile, "pom.xml" ); |
| 70 | + FileUtils.removePath( xzFile.getPath() ); |
| 71 | + xzArchiver.createArchive(); |
| 72 | + |
| 73 | + System.out.println( "Created: " + xzFile.getAbsolutePath() ); |
| 74 | + |
| 75 | + final File zipFile = new File( "target/output/pomxz.zip" ); |
| 76 | + ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" ); |
| 77 | + zipArchiver.setDestFile( zipFile ); |
| 78 | + zipArchiver.addArchivedFileSet( xzFile, "prfx/" ); |
| 79 | + FileUtils.removePath( zipFile.getPath() ); |
| 80 | + zipArchiver.createArchive(); |
| 81 | + |
| 82 | + final ZipFile juZipFile = new ZipFile( zipFile ); |
| 83 | + final ZipEntry zipEntry = juZipFile.getEntry( "prfx/target/output/pom.xml" ); |
| 84 | + final InputStream archivePom = juZipFile.getInputStream( zipEntry ); |
| 85 | + final InputStream pom = new FileInputStream( pomFile ); |
| 86 | + |
| 87 | + assertTrue( Arrays.equals( IOUtil.toByteArray( pom ), IOUtil.toByteArray( archivePom ) ) ); |
| 88 | + archivePom.close(); |
| 89 | + pom.close(); |
| 90 | + juZipFile.close(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests the .xz archiver is forced set to true, and after that |
| 95 | + * tests the behavior when the forced is set to false. |
| 96 | + * |
| 97 | + * @throws Exception |
| 98 | + */ |
| 99 | + public void testBz2IsForcedBehaviour() throws Exception |
| 100 | + { |
| 101 | + XZArchiver xzArchiver = (XZArchiver) createArchiver( "xz" ); |
| 102 | + |
| 103 | + assertTrue( xzArchiver.isSupportingForced() ); |
| 104 | + xzArchiver.createArchive(); |
| 105 | + |
| 106 | + final long creationTime = xzArchiver.getDestFile().lastModified(); |
| 107 | + |
| 108 | + waitUntilNewTimestamp( xzArchiver.getDestFile(), creationTime ); |
| 109 | + |
| 110 | + xzArchiver = (XZArchiver) createArchiver( "xz" ); |
| 111 | + |
| 112 | + xzArchiver.setForced( true ); |
| 113 | + xzArchiver.createArchive(); |
| 114 | + |
| 115 | + final long firstRunTime = xzArchiver.getDestFile().lastModified(); |
| 116 | + |
| 117 | + assertFalse( creationTime==firstRunTime ); |
| 118 | + |
| 119 | + xzArchiver = (XZArchiver) createArchiver( "xz" ); |
| 120 | + |
| 121 | + xzArchiver.setForced( false ); |
| 122 | + xzArchiver.createArchive(); |
| 123 | + |
| 124 | + final long secondRunTime = xzArchiver.getDestFile().lastModified(); |
| 125 | + |
| 126 | + assertEquals( firstRunTime,secondRunTime ); |
| 127 | + } |
| 128 | +} |
0 commit comments