1   package org.apache.maven.artifact;
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.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.maven.artifact.factory.ArtifactFactory;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.plugin.LegacySupport;
33  import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
34  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
35  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
36  import org.apache.maven.execution.DefaultMavenExecutionRequest;
37  import org.apache.maven.execution.DefaultMavenExecutionResult;
38  import org.apache.maven.execution.MavenSession;
39  import org.codehaus.plexus.PlexusTestCase;
40  import org.sonatype.aether.RepositorySystemSession;
41  import org.sonatype.aether.collection.DependencyGraphTransformer;
42  import org.sonatype.aether.collection.DependencyManager;
43  import org.sonatype.aether.collection.DependencySelector;
44  import org.sonatype.aether.collection.DependencyTraverser;
45  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
46  import org.sonatype.aether.repository.LocalRepository;
47  import org.sonatype.aether.util.DefaultRepositorySystemSession;
48  import org.sonatype.aether.util.graph.manager.ClassicDependencyManager;
49  import org.sonatype.aether.util.graph.selector.AndDependencySelector;
50  import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
51  import org.sonatype.aether.util.graph.selector.OptionalDependencySelector;
52  import org.sonatype.aether.util.graph.selector.ScopeDependencySelector;
53  import org.sonatype.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
54  import org.sonatype.aether.util.graph.transformer.NearestVersionConflictResolver;
55  import org.sonatype.aether.util.graph.transformer.ConflictMarker;
56  import org.sonatype.aether.util.graph.transformer.JavaDependencyContextRefiner;
57  import org.sonatype.aether.util.graph.transformer.JavaEffectiveScopeCalculator;
58  import org.sonatype.aether.util.graph.traverser.FatArtifactTraverser;
59  
60  /**
61   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
62   * @version $Id: AbstractArtifactComponentTestCase.java 988749 2010-08-24 22:46:07Z bentmann $
63   */
64  public abstract class AbstractArtifactComponentTestCase
65      extends PlexusTestCase
66  {
67      protected ArtifactFactory artifactFactory;
68  
69      protected ArtifactRepositoryFactory artifactRepositoryFactory;
70      
71      @Override
72      protected void setUp()
73          throws Exception
74      {
75          super.setUp();
76          artifactFactory = lookup( ArtifactFactory.class);        
77          artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class );
78  
79          RepositorySystemSession repoSession = initRepoSession();
80          MavenSession session =
81              new MavenSession( getContainer(), repoSession, new DefaultMavenExecutionRequest(),
82                                new DefaultMavenExecutionResult() );
83  
84          LegacySupport legacySupport = lookup(LegacySupport.class);
85          legacySupport.setSession( session );
86      }
87      
88      @Override
89      protected void tearDown()
90          throws Exception
91      {
92          release( artifactFactory );
93          
94          super.tearDown();
95      }
96  
97      protected abstract String component();
98  
99      /**
100      * Return an existing file, not a directory - causes creation to fail.
101      * 
102      * @throws Exception
103      */
104     protected ArtifactRepository badLocalRepository()
105         throws Exception
106     {
107         String path = "target/test-repositories/" + component() + "/bad-local-repository";
108 
109         File f = new File( getBasedir(), path );
110 
111         f.createNewFile();
112 
113         ArtifactRepositoryLayout repoLayout =
114             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
115 
116         return artifactRepositoryFactory.createArtifactRepository( "test", "file://" + f.getPath(), repoLayout, null, null );
117     }
118 
119     protected String getRepositoryLayout()
120     {
121         return "default";
122     }
123 
124     protected ArtifactRepository localRepository()
125         throws Exception
126     {
127         String path = "target/test-repositories/" + component() + "/local-repository";
128 
129         File f = new File( getBasedir(), path );
130 
131         ArtifactRepositoryLayout repoLayout =
132             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
133 
134         return artifactRepositoryFactory.createArtifactRepository( "local", "file://" + f.getPath(), repoLayout, null, null );
135     }
136 
137     protected ArtifactRepository remoteRepository()
138         throws Exception
139     {
140         String path = "target/test-repositories/" + component() + "/remote-repository";
141 
142         File f = new File( getBasedir(), path );
143 
144         ArtifactRepositoryLayout repoLayout =
145             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
146 
147         return artifactRepositoryFactory.createArtifactRepository( "test", "file://" + f.getPath(), repoLayout,
148                                               new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
149     }
150 
151     protected ArtifactRepository badRemoteRepository()
152         throws Exception
153     {
154         ArtifactRepositoryLayout repoLayout =
155             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
156 
157         return artifactRepositoryFactory.createArtifactRepository( "test", "http://foo.bar/repository", repoLayout, null, null );
158     }
159 
160     protected void assertRemoteArtifactPresent( Artifact artifact )
161         throws Exception
162     {
163         ArtifactRepository remoteRepo = remoteRepository();
164 
165         String path = remoteRepo.pathOf( artifact );
166 
167         File file = new File( remoteRepo.getBasedir(), path );
168 
169         if ( !file.exists() )
170         {
171             fail( "Remote artifact " + file + " should be present." );
172         }
173     }
174 
175     protected void assertLocalArtifactPresent( Artifact artifact )
176         throws Exception
177     {
178         ArtifactRepository localRepo = localRepository();
179 
180         String path = localRepo.pathOf( artifact );
181 
182         File file = new File( localRepo.getBasedir(), path );
183 
184         if ( !file.exists() )
185         {
186             fail( "Local artifact " + file + " should be present." );
187         }
188     }
189 
190     protected void assertRemoteArtifactNotPresent( Artifact artifact )
191         throws Exception
192     {
193         ArtifactRepository remoteRepo = remoteRepository();
194 
195         String path = remoteRepo.pathOf( artifact );
196 
197         File file = new File( remoteRepo.getBasedir(), path );
198 
199         if ( file.exists() )
200         {
201             fail( "Remote artifact " + file + " should not be present." );
202         }
203     }
204 
205     protected void assertLocalArtifactNotPresent( Artifact artifact )
206         throws Exception
207     {
208         ArtifactRepository localRepo = localRepository();
209 
210         String path = localRepo.pathOf( artifact );
211 
212         File file = new File( localRepo.getBasedir(), path );
213 
214         if ( file.exists() )
215         {
216             fail( "Local artifact " + file + " should not be present." );
217         }
218     }
219 
220     // ----------------------------------------------------------------------
221     //
222     // ----------------------------------------------------------------------
223 
224     protected List<ArtifactRepository> remoteRepositories()
225         throws Exception
226     {
227         List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
228 
229         remoteRepositories.add( remoteRepository() );
230 
231         return remoteRepositories;
232     }
233 
234     // ----------------------------------------------------------------------
235     // Test artifact generation for unit tests
236     // ----------------------------------------------------------------------
237 
238     protected Artifact createLocalArtifact( String artifactId, String version )
239         throws Exception
240     {
241         Artifact artifact = createArtifact( artifactId, version );
242 
243         createArtifact( artifact, localRepository() );
244 
245         return artifact;
246     }
247 
248     protected Artifact createRemoteArtifact( String artifactId, String version )
249         throws Exception
250     {
251         Artifact artifact = createArtifact( artifactId, version );
252 
253         createArtifact( artifact, remoteRepository() );
254 
255         return artifact;
256     }
257 
258     protected void createLocalArtifact( Artifact artifact )
259         throws Exception
260     {
261         createArtifact( artifact, localRepository() );
262     }
263 
264     protected void createRemoteArtifact( Artifact artifact )
265         throws Exception
266     {
267         createArtifact( artifact, remoteRepository() );
268     }
269 
270     protected void createArtifact( Artifact artifact, ArtifactRepository repository )
271         throws Exception
272     {
273         String path = repository.pathOf( artifact );
274 
275         File artifactFile = new File( repository.getBasedir(), path );
276 
277         if ( !artifactFile.getParentFile().exists() )
278         {
279             artifactFile.getParentFile().mkdirs();
280         }
281 
282         Writer writer = new OutputStreamWriter( new FileOutputStream( artifactFile ), "ISO-8859-1" );
283 
284         writer.write( artifact.getId() );
285 
286         writer.close();
287     }
288 
289     protected Artifact createArtifact( String artifactId, String version )
290         throws Exception
291     {
292         return createArtifact( artifactId, version, "jar" );
293     }
294 
295     protected Artifact createArtifact( String artifactId, String version, String type )
296         throws Exception
297     {
298         return createArtifact( "org.apache.maven", artifactId, version, type );
299     }
300 
301     protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
302         throws Exception
303     {
304         Artifact a = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
305                 
306         return a;
307     }
308 
309     protected void deleteLocalArtifact( Artifact artifact )
310         throws Exception
311     {
312         deleteArtifact( artifact, localRepository() );
313     }
314 
315     protected void deleteArtifact( Artifact artifact, ArtifactRepository repository )
316         throws Exception
317     {
318         String path = repository.pathOf( artifact );
319 
320         File artifactFile = new File( repository.getBasedir(), path );
321 
322         if ( artifactFile.exists() )
323         {
324             if ( !artifactFile.delete() )
325             {
326                 throw new IOException( "Failure while attempting to delete artifact " + artifactFile );
327             }
328         }
329     }
330 
331     protected RepositorySystemSession initRepoSession()
332         throws Exception
333     {
334         DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
335         session.setIgnoreMissingArtifactDescriptor( true );
336         session.setIgnoreInvalidArtifactDescriptor( true );
337         DependencyTraverser depTraverser = new FatArtifactTraverser();
338         session.setDependencyTraverser( depTraverser );
339 
340         DependencyManager depManager = new ClassicDependencyManager();
341         session.setDependencyManager( depManager );
342 
343         DependencySelector depFilter =
344             new AndDependencySelector( new ScopeDependencySelector( "test", "provided" ),
345                                        new OptionalDependencySelector(), new ExclusionDependencySelector() );
346         session.setDependencySelector( depFilter );
347 
348         DependencyGraphTransformer transformer =
349             new ChainedDependencyGraphTransformer( new ConflictMarker(), new JavaEffectiveScopeCalculator(),
350                                                    new NearestVersionConflictResolver(),
351                                                    new JavaDependencyContextRefiner() );
352         session.setDependencyGraphTransformer( transformer );
353 
354         session.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepository().getBasedir() ) );
355 
356         return session;
357     }
358 
359 }