1 package org.apache.maven.plugin.dependency.testUtils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47
48
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
81
82
83
84
85
86
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
103 long time = System.currentTimeMillis();
104 time = time - ( time % 1000 );
105 file.setLastModified( time );
106
107
108 Thread.sleep( 1000 );
109 }
110
111 }