-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSnappyArchiverTest.java
144 lines (121 loc) · 5.38 KB
/
SnappyArchiverTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* The MIT License
*
* Copyright (c) 2004, The Codehaus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.codehaus.plexus.archiver.snappy;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.BasePlexusArchiverTest;
import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
/**
* Tests for the snappy archiver
*/
public class SnappyArchiverTest
extends BasePlexusArchiverTest
{
public void testCreateArchive()
throws Exception
{
ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
zipArchiver.addDirectory( getTestFile( "src" ) );
zipArchiver.setDestFile( getTestFile( "target/output/archiveForSnappy.zip" ) );
zipArchiver.createArchive();
SnappyArchiver archiver = (SnappyArchiver) lookup( Archiver.ROLE, "snappy" );
String[] inputFiles = new String[ 1 ];
inputFiles[0] = "archiveForSnappy.zip";
archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null );
archiver.setDestFile( getTestFile( "target/output/archive.snappy" ) );
archiver.createArchive();
}
public void testCreateEmptyArchive()
throws Exception
{
SnappyArchiver archiver = (SnappyArchiver) lookup( Archiver.ROLE, "snappy" );
archiver.setDestFile( getTestFile( "target/output/empty.snappy" ) );
try
{
archiver.createArchive();
fail( "Creating empty archive should throw EmptyArchiveException" );
}
catch ( EmptyArchiveException ignore )
{
}
}
public void testCreateResourceCollection()
throws Exception
{
final File pomFile = new File( "pom.xml" );
final File snappyFile = new File( "target/output/pom.xml.snappy" );
SnappyArchiver SnappyArchiver = (SnappyArchiver) lookup( Archiver.ROLE, "snappy" );
SnappyArchiver.setDestFile( snappyFile );
SnappyArchiver.addFile( pomFile, "pom.xml" );
FileUtils.removePath( snappyFile.getPath() );
SnappyArchiver.createArchive();
System.out.println( "Created: " + snappyFile.getAbsolutePath() );
final File zipFile = new File( "target/output/pom.zip" );
ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" );
zipArchiver.setDestFile( zipFile );
zipArchiver.addArchivedFileSet( snappyFile, "prfx/" );
FileUtils.removePath( zipFile.getPath() );
zipArchiver.createArchive();
final ZipFile juZipFile = new ZipFile( zipFile );
final ZipEntry zipEntry = juZipFile.getEntry( "prfx/target/output/pom.xml" );
final InputStream archivePom = juZipFile.getInputStream( zipEntry );
final InputStream pom = new FileInputStream( pomFile );
assertTrue( Arrays.equals( IOUtil.toByteArray( pom ), IOUtil.toByteArray( archivePom ) ) );
archivePom.close();
pom.close();
juZipFile.close();
}
/**
* Tests the Snappy archiver is forced set to true, and after that
* tests the behavior when the forced is set to false.
*
* @throws Exception
*/
public void testsnappyIsForcedBehaviour() throws Exception
{
SnappyArchiver SnappyArchiver = (SnappyArchiver) createArchiver( "snappy" );
assertTrue( SnappyArchiver.isSupportingForced() );
SnappyArchiver.createArchive();
final long creationTime = SnappyArchiver.getDestFile().lastModified();
waitUntilNewTimestamp( SnappyArchiver.getDestFile(), creationTime );
SnappyArchiver = (SnappyArchiver) createArchiver( "snappy" );
SnappyArchiver.setForced( true );
SnappyArchiver.createArchive();
final long firstRunTime = SnappyArchiver.getDestFile().lastModified();
assertFalse( creationTime == firstRunTime );
SnappyArchiver = (SnappyArchiver) createArchiver( "snappy" );
SnappyArchiver.setForced( false );
SnappyArchiver.createArchive();
final long secondRunTime = SnappyArchiver.getDestFile().lastModified();
assertEquals( firstRunTime, secondRunTime );
}
}