1 package org.apache.maven.plugins.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 import static junit.framework.Assert.assertTrue;
38
39 public class DependencyTestUtils
40 {
41
42
43
44
45
46
47
48 public static void removeDirectory( File dir )
49 throws IOException
50 {
51 if ( dir != null )
52 {
53 Log log = new SilentLog();
54 FileSetManager fileSetManager = new FileSetManager( log, false );
55
56 FileSet fs = new FileSet();
57 fs.setDirectory( dir.getPath() );
58 fs.addInclude( "**/**" );
59 fileSetManager.delete( fs );
60
61 }
62 }
63
64 public static ArtifactFactory getArtifactFactory()
65 throws IllegalAccessException
66 {
67 ArtifactFactory artifactFactory;
68 ArtifactHandlerManager manager = new DefaultArtifactHandlerManager();
69 setVariableValueToObject( manager, "artifactHandlers", new HashMap() );
70
71 artifactFactory = new DefaultArtifactFactory();
72 setVariableValueToObject( artifactFactory, "artifactHandlerManager", manager );
73
74 return artifactFactory;
75 }
76
77
78
79
80
81
82
83
84
85 public static void setVariableValueToObject( Object object, String variable, Object value )
86 throws IllegalAccessException
87 {
88 Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( variable, object.getClass() );
89
90 field.setAccessible( true );
91
92 field.set( object, value );
93 }
94
95 public static void setFileModifiedTime( File file )
96 throws InterruptedException
97 {
98 Thread.sleep( 100 );
99
100 long time = System.currentTimeMillis();
101 time = time - ( time % 1000 );
102 assertTrue( "Updating last modification time of marker file " + file.getAbsolutePath()
103 + " failed unexpectedly.", file.setLastModified( time ) );
104
105
106
107 Thread.sleep( 1000 );
108 }
109
110 }