View Javadoc
1   package org.codehaus.plexus.components.io.filemappers;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Closeable;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URLConnection;
26  import java.util.Iterator;
27  import java.util.zip.ZipEntry;
28  import java.util.zip.ZipOutputStream;
29  
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
32  import org.codehaus.plexus.components.io.resources.PlexusIoFileResource;
33  import org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection;
34  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
35  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
36  import org.codehaus.plexus.util.FileUtils;
37  
38  
39  /**
40   * Test case for resource collections.
41   */
42  public class ResourcesTest extends PlexusTestCase
43  {
44      private static final String X_PATH = "x";
45      private static final String A_PATH = X_PATH + "/a";
46      private static final String B_PATH = X_PATH + "/b";
47      private static final String Y_PATH = "y";
48  
49      private File getTestDir()
50      {
51          final String testDirPath = System.getProperty( "plexus.io.testDirPath" );
52          return new File( testDirPath == null ? "target/plexus.io.testDir" : testDirPath );
53      }
54  
55      private File getFilesDir()
56      {
57          return new File( getTestDir(), "files" );
58      }
59  
60      private void createFiles()
61          throws IOException
62      {
63          final File baseDir = getFilesDir();
64          FileUtils.deleteDirectory( baseDir );
65          FileUtils.mkdir( baseDir.getPath() );
66          final File aFile = new File( baseDir, A_PATH );
67          FileUtils.mkdir( aFile.getParentFile().getPath() );
68          FileOutputStream fos = new FileOutputStream( aFile );
69          fos.write( "0123456789".getBytes( "US-ASCII" ) );
70          fos.close();
71          final File bFile = new File( baseDir, B_PATH );
72          fos = new FileOutputStream( bFile );
73          fos.write( "abcdefghijklmnopqrstuvwxyz".getBytes( "US-ASCII" ) );
74          fos.close();
75          final File yDir = new File( baseDir, Y_PATH );
76          FileUtils.mkdir( yDir.getPath() );
77      }
78  
79      private void addDirToZipFile( ZipOutputStream zos, File dir, String path )
80          throws IOException
81      {
82          final String prefix = path == null ? "" : ( path + "/" );
83          File[] files = dir.listFiles();
84          for ( File f : files )
85          {
86              final String entryName = prefix + f.getName();
87              ZipEntry ze = new ZipEntry( entryName );
88              if ( f.isFile() )
89              {
90                  ze.setSize( f.length() );
91                  zos.putNextEntry( ze );
92                  FileInputStream fis = new FileInputStream( f );
93                  byte[] buffer = new byte[1024];
94                  for ( ;; )
95                  {
96                      int res = fis.read( buffer );
97                      if ( res == -1 )
98                      {
99                          break;
100                     }
101                     if ( res > 0 )
102                     {
103                         zos.write( buffer, 0, res );
104                     }
105                 }
106                 fis.close();
107                 ze.setTime( f.lastModified() );
108                 zos.closeEntry();
109             }
110             else
111             {
112                 addDirToZipFile( zos, f, entryName );
113             }
114         }
115     }
116 
117     private void createZipFile( File dest, File dir ) throws IOException
118     {
119         FileOutputStream fos = new FileOutputStream( dest );
120         ZipOutputStream zos = new ZipOutputStream( fos );
121         addDirToZipFile( zos, dir, null );
122         zos.close();
123     }
124 
125     private void compare( InputStream in, File file )
126         throws IOException
127     {
128         try( InputStream fIn = new FileInputStream( file ) )
129         {
130             for ( ;; )
131             {
132                 int i1 = in.read();
133                 int i2 = fIn.read();
134                 assertEquals( i2, i1 );
135                 if ( i1 == -1 )
136                 {
137                     break;
138                 }
139             }
140         }
141     }
142 
143     private void compare( PlexusIoResource res, File file )
144         throws IOException
145     {
146         assertTrue( res.getLastModified() != PlexusIoResource.UNKNOWN_MODIFICATION_DATE );
147         if ( res instanceof PlexusIoFileResource )
148         {
149             assertEquals( res.getLastModified() / 1000, file.lastModified() / 1000 );
150         }
151         assertTrue( res.getSize() != PlexusIoResource.UNKNOWN_RESOURCE_SIZE );
152         assertEquals( res.getSize(), file.length() );
153         InputStream in = res.getContents();
154         compare( in, file );
155         in.close();
156         URLConnection uc = res.getURL().openConnection();
157         uc.setUseCaches( false );
158         in = uc.getInputStream();
159         compare( in, file );
160         in.close();
161     }
162 
163     private void testPlexusIoResourceCollection( PlexusIoResourceCollection plexusIoResourceCollection )
164         throws IOException
165     {
166         boolean xPathSeen = false;
167         boolean yPathSeen = false;
168         boolean aFileSeen = false;
169         boolean bFileSeen = false;
170         Iterator iter = plexusIoResourceCollection.getResources();
171         while ( iter.hasNext() )
172         {
173             PlexusIoResource res = (PlexusIoResource) iter.next();
174             final String resName = res.getName().replace( File.separatorChar, '/' );
175             if ( res.isDirectory() )
176             {
177                 assertFalse( "The directory " + resName + " is a file.", res.isFile() );
178                 if ( X_PATH.equals( resName ) )
179                 {
180                     xPathSeen = true;
181                 }
182                 else if ( Y_PATH.equals( resName ) )
183                 {
184                     yPathSeen = true;
185                 }
186                 else if ( "".equals( resName ) || ".".equals( resName ) )
187                 {
188                     // Ignore me
189                 }
190                 else
191                 {
192                     fail( "Unexpected directory entry: " + resName );
193                 }
194                 final File dir = new File( getFilesDir(), resName );
195                 assertTrue( "The directory " + dir + " doesn't exist.", dir.isDirectory() );
196             }
197             else
198             {
199                 assertTrue( "The file " + resName + " isn't reported to be a file.", res.isFile() );
200                 assertTrue( "The file " + resName + " doesn't exist.", res.isExisting() );
201                 final File f = new File( getFilesDir(), resName );
202                 assertTrue( "A file " + f + " doesn't exist.", f.isFile() && f.exists() );
203                 if ( A_PATH.equals( resName ) )
204                 {
205                     aFileSeen = true;
206                 }
207                 else if ( B_PATH.equals( resName ) )
208                 {
209                     bFileSeen = true;
210                 }
211                 else
212                 {
213                     fail( "Unexpected file entry: " + resName );
214                 }
215                 compare( res, f );
216             }
217         }
218 
219         assertTrue( aFileSeen );
220         assertTrue( bFileSeen );
221         if ( iter instanceof Closeable )
222         {
223             ( (Closeable) iter ).close();
224         }
225     }
226 
227     private void testFileResourceCollection( PlexusIoFileResourceCollection resourceCollection )
228         throws IOException
229     {
230         resourceCollection.setBaseDir( getFilesDir() );
231         testPlexusIoResourceCollection( resourceCollection );
232     }
233 
234     public void testFileCollection() throws Exception
235     {
236         createFiles();
237         testFileResourceCollection( (PlexusIoFileResourceCollection) lookup( PlexusIoResourceCollection.ROLE,
238                                                                              PlexusIoResourceCollection.DEFAULT_ROLE_HINT ) );
239         testFileResourceCollection( (PlexusIoFileResourceCollection) lookup( PlexusIoResourceCollection.ROLE,
240                                                                              PlexusIoFileResourceCollection.ROLE_HINT ) );
241     }
242 
243     private void testZipFileCollection( AbstractPlexusIoArchiveResourceCollection resourceCollection, File zipFile )
244         throws IOException
245     {
246         resourceCollection.setFile( zipFile );
247         testPlexusIoResourceCollection( resourceCollection );
248     }
249 }