View Javadoc
1   package org.codehaus.plexus.archiver.zip;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.net.URL;
8   import java.util.HashMap;
9   import java.util.HashSet;
10  import java.util.Iterator;
11  import java.util.Map;
12  import java.util.Set;
13  import org.apache.commons.io.IOUtils;
14  import org.codehaus.plexus.PlexusTestCase;
15  import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
16  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
17  import org.codehaus.plexus.components.io.resources.PlexusIoURLResource;
18  
19  public class PlexusIoZipFileResourceCollectionTest
20      extends PlexusTestCase
21  {
22  
23      public void testNamelessRootFolder()
24          throws Exception
25      {
26          PlexusIoZipFileResourceCollection resourceCollection = new PlexusIoZipFileResourceCollection();
27          resourceCollection.setFile( getTestFile( "src/test/jars/namelessrootfolder.jar" ) );
28          Iterator iterator = resourceCollection.getResources();
29          PlexusIoURLResource entry = (PlexusIoURLResource) iterator.next();
30          assertEquals( "/dummy.txt", entry.getName() );
31          final URL url = entry.getURL();
32          BufferedReader d = new BufferedReader( new InputStreamReader( entry.getContents() ) );
33          assertEquals( "dummy content", d.readLine() );
34      }
35  
36      public void testDescriptionForError()
37          throws Exception
38      {
39          PlexusIoZipFileResourceCollection resourceCollection = new PlexusIoZipFileResourceCollection();
40          resourceCollection.setFile( getTestFile( "src/test/jars/namelessrootfolder.jar" ) );
41          Iterator<PlexusIoResource> iterator = resourceCollection.getResources();
42          PlexusIoURLResource entry = (PlexusIoURLResource) iterator.next();
43          final URL url = entry.getURL();
44          String descriptionForError = entry.getDescriptionForError( url );
45          assertTrue( descriptionForError.endsWith( "namelessrootfolder.jar!//dummy.txt" ) );
46      }
47  
48      public void testFilesWithIllegalHtmlChars()
49          throws Exception
50      {
51          File testZip = new File( getBasedir(), "src/test/resources/bogusManifest.zip" );
52          PlexusIoZipFileResourceCollection prc = new PlexusIoZipFileResourceCollection();
53          prc.setFile( testZip );
54          final Iterator<PlexusIoResource> entries = prc.getEntries();
55          while ( entries.hasNext() )
56          {
57              final PlexusIoResource next = entries.next();
58              if ( next.getName().endsWith( "MANIFEST.MF" ) )
59              {
60                  final InputStream contents1 = next.getContents();
61                  final String manifest = IOUtils.toString( contents1, "UTF-8" );
62                  assertTrue( manifest.contains( "bogs=fus" ) );
63                  contents1.close();
64              }
65  
66          }
67      }
68  
69      public void testFilesThatAreNotThere()
70          throws Exception
71      {
72          File testZip = new File( getBasedir(), "src/test/resources/archiveWithIllegalHtmlFileName.zip" );
73          Set<String> seen = new HashSet<String>();
74          seen.add( "AFileThatNeedsHtmlEsc%3F&gt" );
75          seen.add( "Afile&lt;Yo&gt;.txt" );
76          seen.add( "File With Space.txt" );
77          seen.add( "FileWith%.txt" );
78          PlexusIoZipFileResourceCollection prc = new PlexusIoZipFileResourceCollection();
79          prc.setFile( testZip );
80          final Iterator<PlexusIoResource> entries = prc.getEntries();
81          while ( entries.hasNext() )
82          {
83              final PlexusIoResource next = entries.next();
84              assertTrue( next.getName() + "was not present", seen.remove( next.getName() ) );
85              final URL url = next.getURL();
86              final InputStream contents = next.getContents();
87              contents.close();
88          }
89      }
90  
91      public void testSymlinkEntries()
92          throws Exception
93      {
94          File testZip = new File( getBasedir(), "src/test/resources/symlinks/symlinks.zip" );
95          Map<String, String> symLinks = new HashMap<String, String>();
96          symLinks.put( "symDir", "targetDir/" );
97          symLinks.put( "symLinkToDirOnTheOutside", "../dirOnTheOutside/" );
98          symLinks.put( "symLinkToTheOutside", "../onTheOutside.txt" );
99          symLinks.put( "symR", "fileR.txt" );
100         symLinks.put( "symW", "fileW.txt" );
101         symLinks.put( "symX", "fileX.txt" );
102         PlexusIoZipFileResourceCollection prc = new PlexusIoZipFileResourceCollection();
103         prc.setFile( testZip );
104         final Iterator<PlexusIoResource> entries = prc.getEntries();
105         while ( entries.hasNext() )
106         {
107             final PlexusIoResource next = entries.next();
108             String symLinkTarget = symLinks.remove( next.getName() );
109             if ( symLinkTarget != null )
110             {
111                 assertTrue( next.getName() + " must be symlink", next.isSymbolicLink() );
112                 assertTrue( next instanceof SymlinkDestinationSupplier );
113                 assertEquals( symLinkTarget,
114                               ( (SymlinkDestinationSupplier) next ).getSymlinkDestination() );
115             }
116             else
117             {
118                 assertFalse( next.getName() + " must not be symlink", next.isSymbolicLink() );
119             }
120         }
121 
122         assertTrue( symLinks.isEmpty() );
123     }
124 
125 }