View Javadoc

1   package org.apache.maven.plugin.dependency.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 java.io.File;
23  import java.io.IOException;
24  import java.lang.reflect.Field;
25  import java.util.HashMap;
26  
27  import org.apache.maven.artifact.factory.ArtifactFactory;
28  import org.apache.maven.artifact.factory.DefaultArtifactFactory;
29  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
30  import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.logging.Log;
33  import org.apache.maven.plugin.testing.SilentLog;
34  import org.apache.maven.shared.model.fileset.FileSet;
35  import org.apache.maven.shared.model.fileset.util.FileSetManager;
36  import org.codehaus.plexus.util.ReflectionUtils;
37  
38  public class DependencyTestUtils
39  {
40  
41      /**
42       * Deletes a directory and its contents.
43       * 
44       * @param dir
45       *            The base directory of the included and excluded files.
46       * @throws IOException
47       * @throws MojoExecutionException
48       *             When a directory failed to get deleted.
49       */
50      public static void removeDirectory( File dir )
51          throws IOException
52      {
53          if ( dir != null )
54          {
55              Log log = new SilentLog();
56              FileSetManager fileSetManager = new FileSetManager( log, false );
57  
58              FileSet fs = new FileSet();
59              fs.setDirectory( dir.getPath() );
60              fs.addInclude( "**/**" );
61              fileSetManager.delete( fs );
62  
63          }
64      }
65  
66      public static ArtifactFactory getArtifactFactory()
67          throws IllegalAccessException
68      {
69          ArtifactFactory artifactFactory;
70          ArtifactHandlerManager manager = new DefaultArtifactHandlerManager();
71          setVariableValueToObject( manager, "artifactHandlers", new HashMap() );
72  
73          artifactFactory = new DefaultArtifactFactory();
74          setVariableValueToObject( artifactFactory, "artifactHandlerManager", manager );
75  
76          return artifactFactory;
77      }
78  
79      /**
80       * convience method to set values to variables in objects that don't have
81       * setters
82       * 
83       * @param object
84       * @param variable
85       * @param value
86       * @throws IllegalAccessException
87       */
88      public static void setVariableValueToObject( Object object, String variable, Object value )
89          throws IllegalAccessException
90      {
91          Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( variable, object.getClass() );
92  
93          field.setAccessible( true );
94  
95          field.set( object, value );
96      }
97  
98      public static void setFileModifiedTime( File file )
99          throws InterruptedException
100     {
101         Thread.sleep( 100 );
102         // round down to the last second
103         long time = System.currentTimeMillis();
104         time = time - ( time % 1000 );
105         file.setLastModified( time );
106         // wait at least a second for filesystems that only record to the
107         // nearest second.
108         Thread.sleep( 1000 );
109     }
110 
111 }