View Javadoc
1   package org.apache.maven.plugins.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  import static junit.framework.Assert.assertTrue;
38  
39  public class DependencyTestUtils
40  {
41  
42      /**
43       * Deletes a directory and its contents.
44       *
45       * @param dir {@link File} The base directory of the included and excluded files.
46       * @throws IOException in case of an error. When a directory failed to get deleted.
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       * convenience method to set values to variables in objects that don't have setters
79       *
80       * @param object {@link Object}
81       * @param variable the field name.
82       * @param value The value to be set.
83       * @throws IllegalAccessException in case of an error.
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          // round down to the last second
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         // wait at least a second for filesystems that only record to the
106         // nearest second.
107         Thread.sleep( 1000 );
108     }
109 
110 }