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