-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathPlexusIoZipFileResourceCollectionTest.java
128 lines (117 loc) · 5.05 KB
/
PlexusIoZipFileResourceCollectionTest.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
package org.codehaus.plexus.archiver.zip;
import org.apache.commons.io.IOUtils;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
import org.codehaus.plexus.components.io.resources.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class PlexusIoZipFileResourceCollectionTest
extends PlexusTestCase
{
public void testNamelessRootFolder()
throws Exception
{
PlexusIoZipFileResourceCollection resourceCollection = new PlexusIoZipFileResourceCollection();
resourceCollection.setFile( getTestFile( "src/test/jars/namelessrootfolder.jar" ) );
Iterator iterator = resourceCollection.getResources();
PlexusIoURLResource entry = (PlexusIoURLResource) iterator.next();
assertEquals( "/dummy.txt", entry.getName() );
final URL url = entry.getURL();
BufferedReader d = new BufferedReader( new InputStreamReader( entry.getContents() ) );
assertEquals( "dummy content", d.readLine() );
}
public void testDescriptionForError()
throws Exception
{
PlexusIoZipFileResourceCollection resourceCollection = new PlexusIoZipFileResourceCollection();
resourceCollection.setFile( getTestFile( "src/test/jars/namelessrootfolder.jar" ) );
Iterator<PlexusIoResource> iterator = resourceCollection.getResources();
PlexusIoURLResource entry = (PlexusIoURLResource) iterator.next();
final URL url = entry.getURL();
String descriptionForError = entry.getDescriptionForError( url );
assertTrue( descriptionForError.endsWith( "namelessrootfolder.jar!//dummy.txt" ) );
}
public void testFilesWithIllegalHtmlChars()
throws Exception
{
File testZip = new File( getBasedir(), "src/test/resources/bogusManifest.zip" );
PlexusIoZipFileResourceCollection
prc = new PlexusIoZipFileResourceCollection();
prc.setFile( testZip );
final Iterator<PlexusIoResource> entries = prc.getEntries();
while ( entries.hasNext() )
{
final PlexusIoResource next = entries.next();
if ( next.getName().endsWith( "MANIFEST.MF" ) )
{
final InputStream contents1 = next.getContents();
final String manifest = IOUtils.toString( contents1, "UTF-8" );
assertTrue( manifest.contains( "bogs=fus" ) );
contents1.close();
}
}
}
public void testFilesThatAreNotThere()
throws Exception
{
File testZip = new File( getBasedir(), "src/test/resources/archiveWithIllegalHtmlFileName.zip" );
Set<String> seen = new HashSet<String>();
seen.add( "AFileThatNeedsHtmlEsc%3F>" );
seen.add( "Afile<Yo>.txt" );
seen.add( "File With Space.txt" );
seen.add( "FileWith%.txt" );
PlexusIoZipFileResourceCollection
prc = new PlexusIoZipFileResourceCollection();
prc.setFile( testZip );
final Iterator<PlexusIoResource> entries = prc.getEntries();
while ( entries.hasNext() )
{
final PlexusIoResource next = entries.next();
assertTrue( next.getName() + "was not present", seen.remove( next.getName() ) );
final URL url = next.getURL();
final InputStream contents = next.getContents();
contents.close();
}
}
public void testSymlinkEntries()
throws Exception
{
File testZip = new File( getBasedir(), "src/test/resources/symlinks/symlinks.zip" );
Map<String, String> symLinks = new HashMap<String, String>();
symLinks.put( "symDir", "targetDir/" );
symLinks.put( "symLinkToDirOnTheOutside", "../dirOnTheOutside/" );
symLinks.put( "symLinkToTheOutside", "../onTheOutside.txt" );
symLinks.put( "symR", "fileR.txt" );
symLinks.put( "symW", "fileW.txt" );
symLinks.put( "symX", "fileX.txt" );
PlexusIoZipFileResourceCollection
prc = new PlexusIoZipFileResourceCollection();
prc.setFile( testZip );
final Iterator<PlexusIoResource> entries = prc.getEntries();
while ( entries.hasNext() )
{
final PlexusIoResource next = entries.next();
String symLinkTarget = symLinks.remove( next.getName() );
if ( symLinkTarget != null )
{
assertTrue( next.getName() + " must be symlink", next.isSymbolicLink() );
assertTrue( next instanceof SymlinkDestinationSupplier );
assertEquals( symLinkTarget,
( (SymlinkDestinationSupplier) next ).getSymlinkDestination() );
}
else
{
assertFalse( next.getName() + " must not be symlink", next.isSymbolicLink() );
}
}
assertTrue( symLinks.isEmpty() );
}
}