View Javadoc

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