001package org.apache.maven.artifact;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.FileOutputStream;
024import java.io.IOException;
025import java.io.OutputStreamWriter;
026import java.io.Writer;
027import java.util.ArrayList;
028import java.util.List;
029
030import org.apache.maven.artifact.factory.ArtifactFactory;
031import org.apache.maven.artifact.repository.ArtifactRepository;
032import org.apache.maven.plugin.LegacySupport;
033import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
034import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
035import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
036import org.apache.maven.execution.DefaultMavenExecutionRequest;
037import org.apache.maven.execution.DefaultMavenExecutionResult;
038import org.apache.maven.execution.MavenSession;
039import org.codehaus.plexus.ContainerConfiguration;
040import org.codehaus.plexus.PlexusConstants;
041import org.codehaus.plexus.PlexusTestCase;
042import org.eclipse.aether.DefaultRepositorySystemSession;
043import org.eclipse.aether.RepositorySystemSession;
044import org.eclipse.aether.collection.DependencyGraphTransformer;
045import org.eclipse.aether.collection.DependencyManager;
046import org.eclipse.aether.collection.DependencySelector;
047import org.eclipse.aether.collection.DependencyTraverser;
048import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
049import org.eclipse.aether.repository.LocalRepository;
050import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
051import org.eclipse.aether.util.graph.selector.AndDependencySelector;
052import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
053import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
054import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
055import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
056import org.eclipse.aether.util.graph.transformer.ConflictResolver;
057import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
058import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
059import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
060import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
061import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
062import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
063import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
064
065/**
066 * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
067 */
068public abstract class AbstractArtifactComponentTestCase
069    extends PlexusTestCase
070{
071    protected ArtifactFactory artifactFactory;
072
073    protected ArtifactRepositoryFactory artifactRepositoryFactory;
074
075    @Override
076    protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
077    {
078        super.customizeContainerConfiguration( containerConfiguration );
079        containerConfiguration.setAutoWiring( true );
080        containerConfiguration.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
081    }
082
083    @Override
084    protected void setUp()
085        throws Exception
086    {
087        super.setUp();
088        artifactFactory = lookup( ArtifactFactory.class);
089        artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class );
090
091        RepositorySystemSession repoSession = initRepoSession();
092        MavenSession session =
093            new MavenSession( getContainer(), repoSession, new DefaultMavenExecutionRequest(),
094                              new DefaultMavenExecutionResult() );
095
096        LegacySupport legacySupport = lookup(LegacySupport.class);
097        legacySupport.setSession( session );
098    }
099
100    @Override
101    protected void tearDown()
102        throws Exception
103    {
104        release( artifactFactory );
105
106        super.tearDown();
107    }
108
109    protected abstract String component();
110
111    /**
112     * Return an existing file, not a directory - causes creation to fail.
113     *
114     * @throws Exception
115     */
116    protected ArtifactRepository badLocalRepository()
117        throws Exception
118    {
119        String path = "target/test-repositories/" + component() + "/bad-local-repository";
120
121        File f = new File( getBasedir(), path );
122
123        f.createNewFile();
124
125        ArtifactRepositoryLayout repoLayout =
126            (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
127
128        return artifactRepositoryFactory.createArtifactRepository( "test", "file://" + f.getPath(), repoLayout, null, null );
129    }
130
131    protected String getRepositoryLayout()
132    {
133        return "default";
134    }
135
136    protected ArtifactRepository localRepository()
137        throws Exception
138    {
139        String path = "target/test-repositories/" + component() + "/local-repository";
140
141        File f = new File( getBasedir(), path );
142
143        ArtifactRepositoryLayout repoLayout =
144            (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
145
146        return artifactRepositoryFactory.createArtifactRepository( "local", "file://" + f.getPath(), repoLayout, null, null );
147    }
148
149    protected ArtifactRepository remoteRepository()
150        throws Exception
151    {
152        String path = "target/test-repositories/" + component() + "/remote-repository";
153
154        File f = new File( getBasedir(), path );
155
156        ArtifactRepositoryLayout repoLayout =
157            (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
158
159        return artifactRepositoryFactory.createArtifactRepository( "test", "file://" + f.getPath(), repoLayout,
160                                              new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
161    }
162
163    protected ArtifactRepository badRemoteRepository()
164        throws Exception
165    {
166        ArtifactRepositoryLayout repoLayout =
167            (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
168
169        return artifactRepositoryFactory.createArtifactRepository( "test", "http://foo.bar/repository", repoLayout, null, null );
170    }
171
172    protected void assertRemoteArtifactPresent( Artifact artifact )
173        throws Exception
174    {
175        ArtifactRepository remoteRepo = remoteRepository();
176
177        String path = remoteRepo.pathOf( artifact );
178
179        File file = new File( remoteRepo.getBasedir(), path );
180
181        if ( !file.exists() )
182        {
183            fail( "Remote artifact " + file + " should be present." );
184        }
185    }
186
187    protected void assertLocalArtifactPresent( Artifact artifact )
188        throws Exception
189    {
190        ArtifactRepository localRepo = localRepository();
191
192        String path = localRepo.pathOf( artifact );
193
194        File file = new File( localRepo.getBasedir(), path );
195
196        if ( !file.exists() )
197        {
198            fail( "Local artifact " + file + " should be present." );
199        }
200    }
201
202    protected void assertRemoteArtifactNotPresent( Artifact artifact )
203        throws Exception
204    {
205        ArtifactRepository remoteRepo = remoteRepository();
206
207        String path = remoteRepo.pathOf( artifact );
208
209        File file = new File( remoteRepo.getBasedir(), path );
210
211        if ( file.exists() )
212        {
213            fail( "Remote artifact " + file + " should not be present." );
214        }
215    }
216
217    protected void assertLocalArtifactNotPresent( Artifact artifact )
218        throws Exception
219    {
220        ArtifactRepository localRepo = localRepository();
221
222        String path = localRepo.pathOf( artifact );
223
224        File file = new File( localRepo.getBasedir(), path );
225
226        if ( file.exists() )
227        {
228            fail( "Local artifact " + file + " should not be present." );
229        }
230    }
231
232    // ----------------------------------------------------------------------
233    //
234    // ----------------------------------------------------------------------
235
236    protected List<ArtifactRepository> remoteRepositories()
237        throws Exception
238    {
239        List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
240
241        remoteRepositories.add( remoteRepository() );
242
243        return remoteRepositories;
244    }
245
246    // ----------------------------------------------------------------------
247    // Test artifact generation for unit tests
248    // ----------------------------------------------------------------------
249
250    protected Artifact createLocalArtifact( String artifactId, String version )
251        throws Exception
252    {
253        Artifact artifact = createArtifact( artifactId, version );
254
255        createArtifact( artifact, localRepository() );
256
257        return artifact;
258    }
259
260    protected Artifact createRemoteArtifact( String artifactId, String version )
261        throws Exception
262    {
263        Artifact artifact = createArtifact( artifactId, version );
264
265        createArtifact( artifact, remoteRepository() );
266
267        return artifact;
268    }
269
270    protected void createLocalArtifact( Artifact artifact )
271        throws Exception
272    {
273        createArtifact( artifact, localRepository() );
274    }
275
276    protected void createRemoteArtifact( Artifact artifact )
277        throws Exception
278    {
279        createArtifact( artifact, remoteRepository() );
280    }
281
282    protected void createArtifact( Artifact artifact, ArtifactRepository repository )
283        throws Exception
284    {
285        String path = repository.pathOf( artifact );
286
287        File artifactFile = new File( repository.getBasedir(), path );
288
289        if ( !artifactFile.getParentFile().exists() )
290        {
291            artifactFile.getParentFile().mkdirs();
292        }
293
294        Writer writer = new OutputStreamWriter( new FileOutputStream( artifactFile ), "ISO-8859-1" );
295
296        writer.write( artifact.getId() );
297
298        writer.close();
299    }
300
301    protected Artifact createArtifact( String artifactId, String version )
302        throws Exception
303    {
304        return createArtifact( artifactId, version, "jar" );
305    }
306
307    protected Artifact createArtifact( String artifactId, String version, String type )
308        throws Exception
309    {
310        return createArtifact( "org.apache.maven", artifactId, version, type );
311    }
312
313    protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
314        throws Exception
315    {
316        Artifact a = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
317
318        return a;
319    }
320
321    protected void deleteLocalArtifact( Artifact artifact )
322        throws Exception
323    {
324        deleteArtifact( artifact, localRepository() );
325    }
326
327    protected void deleteArtifact( Artifact artifact, ArtifactRepository repository )
328        throws Exception
329    {
330        String path = repository.pathOf( artifact );
331
332        File artifactFile = new File( repository.getBasedir(), path );
333
334        if ( artifactFile.exists() )
335        {
336            if ( !artifactFile.delete() )
337            {
338                throw new IOException( "Failure while attempting to delete artifact " + artifactFile );
339            }
340        }
341    }
342
343    protected RepositorySystemSession initRepoSession()
344        throws Exception
345    {
346        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
347        session.setArtifactDescriptorPolicy( new SimpleArtifactDescriptorPolicy( true, true ) );
348        DependencyTraverser depTraverser = new FatArtifactTraverser();
349        session.setDependencyTraverser( depTraverser );
350
351        DependencyManager depManager = new ClassicDependencyManager();
352        session.setDependencyManager( depManager );
353
354        DependencySelector depFilter =
355            new AndDependencySelector( new ScopeDependencySelector( "test", "provided" ),
356                                       new OptionalDependencySelector(), new ExclusionDependencySelector() );
357        session.setDependencySelector( depFilter );
358
359        DependencyGraphTransformer transformer =
360            new ConflictResolver( new NearestVersionSelector(), new JavaScopeSelector(),
361                                  new SimpleOptionalitySelector(), new JavaScopeDeriver() );
362        new ChainedDependencyGraphTransformer( transformer, new JavaDependencyContextRefiner() );
363        session.setDependencyGraphTransformer( transformer );
364
365        LocalRepository localRepo = new LocalRepository( localRepository().getBasedir() );
366        session.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( session, localRepo ) );
367
368        return session;
369    }
370
371}