View Javadoc
1   package org.apache.maven.plugins.dependency;
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  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.maven.plugin.LegacySupport;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
29  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
30  import org.eclipse.aether.DefaultRepositorySystemSession;
31  import org.eclipse.aether.RepositorySystem;
32  import org.eclipse.aether.repository.LocalRepository;
33  import org.eclipse.aether.repository.LocalRepositoryManager;
34  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35  
36  public abstract class AbstractDependencyMojoTestCase
37      extends AbstractMojoTestCase
38  {
39  
40      protected File testDir;
41  
42      protected DependencyArtifactStubFactory stubFactory;
43  
44      protected void setUp( String testDirStr, boolean createFiles )
45          throws Exception
46      {
47          setUp( testDirStr, createFiles, true );
48      }
49  
50      protected void setUp( String testDirStr, boolean createFiles, boolean flattenedPath )
51          throws Exception
52      {
53          // required for mojo lookups to work
54          super.setUp();
55          testDir = new File( getBasedir(), "target" + File.separatorChar + "unit-tests" + File.separatorChar + testDirStr
56              + File.separatorChar );
57          FileUtils.deleteDirectory( testDir );
58          assertFalse( testDir.exists() );
59  
60          stubFactory = new DependencyArtifactStubFactory( this.testDir, createFiles, flattenedPath );
61      }
62  
63      protected void tearDown()
64      {
65          if ( testDir != null )
66          {
67              try
68              {
69                  FileUtils.deleteDirectory( testDir );
70              }
71              catch ( IOException e )
72              {
73                  e.printStackTrace();
74                  fail( "Trying to remove directory:" + testDir + System.lineSeparator() + e.toString() );
75              }
76              assertFalse( testDir.exists() );
77          }
78      }
79  
80      protected void copyFile( AbstractDependencyMojo mojo, File artifact, File destFile )
81          throws MojoExecutionException
82      {
83          mojo.copyFile( artifact, destFile );
84      }
85      
86  
87      protected void installLocalRepository( LegacySupport legacySupport )
88          throws ComponentLookupException
89      {
90          DefaultRepositorySystemSession repoSession =
91              (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
92          RepositorySystem system = lookup( RepositorySystem.class );
93          String directory = stubFactory.getWorkingDir().toString();
94          LocalRepository localRepository = new LocalRepository( directory  );
95          LocalRepositoryManager manager = system.newLocalRepositoryManager( repoSession, localRepository );
96          repoSession.setLocalRepositoryManager( manager );
97      }
98  }