View Javadoc

1   package org.apache.maven.plugin.assembly.testutils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
23  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
24  import org.codehaus.plexus.archiver.ArchivedFileSet;
25  import org.codehaus.plexus.archiver.Archiver;
26  import org.codehaus.plexus.archiver.ArchiverException;
27  import org.codehaus.plexus.archiver.FileSet;
28  import org.codehaus.plexus.archiver.ResourceIterator;
29  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
30  import org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.logging.console.ConsoleLogger;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  public class TrackingArchiverStub
43      implements Archiver
44  {
45  
46      private static final Logger logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "test" );
47  
48      public boolean forced;
49  
50      public File destFile;
51  
52      public final List<Addition> added = new ArrayList<Addition>();
53  
54      public boolean created;
55  
56      private boolean useJvmChmod;
57  
58      private boolean ignorePermissions;
59  
60      public void createArchive()
61          throws ArchiverException, IOException
62      {
63          created = true;
64      }
65  
66      public void addDirectory( final File directory )
67          throws ArchiverException
68      {
69          added.add( new Addition( directory, null, null, null, -1 ) );
70      }
71  
72      public void addDirectory( final File directory, final String prefix )
73          throws ArchiverException
74      {
75          added.add( new Addition( directory, prefix, null, null, -1 ) );
76      }
77  
78      public void addDirectory( final File directory, final String[] includes, final String[] excludes )
79          throws ArchiverException
80      {
81          added.add( new Addition( directory, null, includes, excludes, -1 ) );
82      }
83  
84      public void addDirectory( final File directory, final String prefix, final String[] includes,
85                                final String[] excludes )
86          throws ArchiverException
87      {
88          added.add( new Addition( directory, prefix, includes, excludes, -1 ) );
89      }
90  
91      public void addFileSet( final FileSet fileSet )
92          throws ArchiverException
93      {
94          added.add( new Addition( fileSet, null, null, null, -1 ) );
95      }
96  
97      public void addFile( final File inputFile, final String destFileName )
98          throws ArchiverException
99      {
100         added.add( new Addition( inputFile, destFileName, null, null, -1 ) );
101     }
102 
103     public void addFile( final File inputFile, final String destFileName, final int permissions )
104         throws ArchiverException
105     {
106         added.add( new Addition( inputFile, destFileName, null, null, permissions ) );
107     }
108 
109     public void addArchivedFileSet( final File archiveFile )
110         throws ArchiverException
111     {
112         added.add( new Addition( archiveFile, null, null, null, -1 ) );
113     }
114 
115     public void addArchivedFileSet( final File archiveFile, final String prefix )
116         throws ArchiverException
117     {
118         added.add( new Addition( archiveFile, prefix, null, null, -1 ) );
119     }
120 
121     public void addArchivedFileSet( final File archiveFile, final String[] includes, final String[] excludes )
122         throws ArchiverException
123     {
124         added.add( new Addition( archiveFile, null, includes, excludes, -1 ) );
125     }
126 
127     public void addArchivedFileSet( final File archiveFile, final String prefix, final String[] includes,
128                                     final String[] excludes )
129         throws ArchiverException
130     {
131         added.add( new Addition( archiveFile, prefix, includes, excludes, -1 ) );
132     }
133 
134     public void addArchivedFileSet( final ArchivedFileSet fileSet )
135         throws ArchiverException
136     {
137         added.add( new Addition( fileSet, null, null, null, -1 ) );
138     }
139 
140     public void addResource( final PlexusIoResource resource, final String destFileName, final int permissions )
141         throws ArchiverException
142     {
143         added.add( new Addition( resource, destFileName, null, null, permissions ) );
144     }
145 
146     public void addResources( final PlexusIoResourceCollection resources )
147         throws ArchiverException
148     {
149         added.add( new Addition( resources, null, null, null, -1 ) );
150     }
151 
152     public File getDestFile()
153     {
154         return destFile;
155     }
156 
157     public void setDestFile( final File destFile )
158     {
159         this.destFile = destFile;
160     }
161 
162     public void setFileMode( final int mode )
163     {
164     }
165 
166     public int getFileMode()
167     {
168         try
169         {
170             return TypeConversionUtils.modeToInt( "0644", logger );
171         }
172         catch ( final AssemblyFormattingException e )
173         {
174             throw new IllegalStateException( "Failed to parse mode 0644", e );
175         }
176     }
177 
178     public int getOverrideFileMode()
179     {
180         try
181         {
182             return TypeConversionUtils.modeToInt( "0644", logger );
183         }
184         catch ( final AssemblyFormattingException e )
185         {
186             throw new IllegalStateException( "Failed to parse mode 0644", e );
187         }
188     }
189 
190     public void setDefaultFileMode( final int mode )
191     {
192     }
193 
194     public int getDefaultFileMode()
195     {
196         try
197         {
198             return TypeConversionUtils.modeToInt( "0644", logger );
199         }
200         catch ( final AssemblyFormattingException e )
201         {
202             throw new IllegalStateException( "Failed to parse mode 0644", e );
203         }
204     }
205 
206     public void setDirectoryMode( final int mode )
207     {
208     }
209 
210     public int getDirectoryMode()
211     {
212         try
213         {
214             return TypeConversionUtils.modeToInt( "0755", logger );
215         }
216         catch ( final AssemblyFormattingException e )
217         {
218             throw new IllegalStateException( "Failed to parse mode 0755", e );
219         }
220     }
221 
222     public int getOverrideDirectoryMode()
223     {
224         try
225         {
226             return TypeConversionUtils.modeToInt( "0755", logger );
227         }
228         catch ( final AssemblyFormattingException e )
229         {
230             throw new IllegalStateException( "Failed to parse mode 0755", e );
231         }
232     }
233 
234     public void setDefaultDirectoryMode( final int mode )
235     {
236     }
237 
238     public int getDefaultDirectoryMode()
239     {
240         try
241         {
242             return TypeConversionUtils.modeToInt( "0755", logger );
243         }
244         catch ( final AssemblyFormattingException e )
245         {
246             throw new IllegalStateException( "Failed to parse mode 0755", e );
247         }
248     }
249 
250     public boolean getIncludeEmptyDirs()
251     {
252         return false;
253     }
254 
255     public void setIncludeEmptyDirs( final boolean includeEmptyDirs )
256     {
257     }
258 
259     public void setDotFileDirectory( final File dotFileDirectory )
260     {
261     }
262 
263     public ResourceIterator getResources()
264         throws ArchiverException
265     {
266         return null;
267     }
268 
269     @SuppressWarnings( "rawtypes" )
270     public Map getFiles()
271     {
272         return new HashMap();
273     }
274 
275     public boolean isForced()
276     {
277         return false;
278     }
279 
280     public void setForced( final boolean forced )
281     {
282     }
283 
284     public boolean isSupportingForced()
285     {
286         return true;
287     }
288 
289     public String getDuplicateBehavior()
290     {
291         return null;
292     }
293 
294     public void setDuplicateBehavior( final String duplicate )
295     {
296     }
297 
298     public class Addition
299     {
300         /**
301          * {@inheritDoc}
302          * 
303          * @see java.lang.Object#toString()
304          */
305         @Override
306         public String toString()
307         {
308             final StringBuilder builder = new StringBuilder();
309             builder.append( "Addition (\n    resource= " );
310             builder.append( resource );
311             builder.append( "\n    directory= " );
312             builder.append( directory );
313             builder.append( "\n    destination= " );
314             builder.append( destination );
315             builder.append( "\n    permissions= " );
316             builder.append( permissions );
317             builder.append( "\n    includes= " );
318             builder.append( includes == null ? "-none-" : StringUtils.join( includes, ", " ) );
319             builder.append( "\n    excludes= " );
320             builder.append( excludes == null ? "-none-" : StringUtils.join( excludes, ", " ) );
321             builder.append( "\n)" );
322             return builder.toString();
323         }
324 
325         public final Object resource;
326 
327         public final File directory;
328 
329         public final String destination;
330 
331         public final int permissions;
332 
333         public final String[] includes;
334 
335         public final String[] excludes;
336 
337         public Addition( final Object resource, final String destination, final String[] includes,
338                          final String[] excludes, final int permissions )
339         {
340             this.resource = resource;
341             if ( resource instanceof FileSet )
342             {
343                 final FileSet fs = (FileSet) resource;
344                 directory = fs.getDirectory();
345                 this.destination = fs.getPrefix();
346                 this.includes = fs.getIncludes();
347                 this.excludes = fs.getExcludes();
348                 this.permissions = permissions;
349             }
350             else
351             {
352                 if ( resource instanceof File && ( (File) resource ).isDirectory() )
353                 {
354                     directory = (File) resource;
355                 }
356                 else
357                 {
358                     directory = null;
359                 }
360 
361                 this.destination = destination;
362                 this.includes = includes;
363                 this.excludes = excludes;
364                 this.permissions = permissions;
365             }
366         }
367     }
368 
369     public boolean isUseJvmChmod()
370     {
371         return useJvmChmod;
372     }
373 
374     public void setUseJvmChmod( final boolean useJvmChmod )
375     {
376         this.useJvmChmod = useJvmChmod;
377     }
378 
379     public boolean isIgnorePermissions()
380     {
381         return ignorePermissions;
382     }
383 
384     public void setIgnorePermissions( final boolean ignorePermissions )
385     {
386         this.ignorePermissions = ignorePermissions;
387     }
388 }