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