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.ContainerConfiguration; 040 import org.codehaus.plexus.PlexusTestCase; 041 import org.eclipse.aether.DefaultRepositorySystemSession; 042 import org.eclipse.aether.RepositorySystemSession; 043 import org.eclipse.aether.collection.DependencyGraphTransformer; 044 import org.eclipse.aether.collection.DependencyManager; 045 import org.eclipse.aether.collection.DependencySelector; 046 import org.eclipse.aether.collection.DependencyTraverser; 047 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 048 import org.eclipse.aether.repository.LocalRepository; 049 import org.eclipse.aether.util.graph.manager.ClassicDependencyManager; 050 import org.eclipse.aether.util.graph.selector.AndDependencySelector; 051 import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector; 052 import org.eclipse.aether.util.graph.selector.OptionalDependencySelector; 053 import org.eclipse.aether.util.graph.selector.ScopeDependencySelector; 054 import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer; 055 import org.eclipse.aether.util.graph.transformer.ConflictResolver; 056 import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; 057 import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; 058 import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner; 059 import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; 060 import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; 061 import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser; 062 import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy; 063 064 /** 065 * @author <a href="mailto:jason@maven.org">Jason van Zyl </a> 066 */ 067 public abstract class AbstractArtifactComponentTestCase 068 extends PlexusTestCase 069 { 070 protected ArtifactFactory artifactFactory; 071 072 protected ArtifactRepositoryFactory artifactRepositoryFactory; 073 074 @Override 075 protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) 076 { 077 super.customizeContainerConfiguration( containerConfiguration ); 078 containerConfiguration.setAutoWiring( true ); 079 } 080 081 @Override 082 protected void setUp() 083 throws Exception 084 { 085 super.setUp(); 086 artifactFactory = lookup( ArtifactFactory.class); 087 artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class ); 088 089 RepositorySystemSession repoSession = initRepoSession(); 090 MavenSession session = 091 new MavenSession( getContainer(), repoSession, new DefaultMavenExecutionRequest(), 092 new DefaultMavenExecutionResult() ); 093 094 LegacySupport legacySupport = lookup(LegacySupport.class); 095 legacySupport.setSession( session ); 096 } 097 098 @Override 099 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 }