Skip to content

Instantly share code, notes, and snippets.

@khmarbaise
Last active September 24, 2016 09:14
Show Gist options
  • Save khmarbaise/1ed421983480e3fef624bf6c8f4844b6 to your computer and use it in GitHub Desktop.
Save khmarbaise/1ed421983480e3fef624bf6c8f4844b6 to your computer and use it in GitHub Desktop.
Test for writing a Unit Test for maven-artifact-transfer comopnent.
package org.apache.maven.shared.project.install;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.internal.MavenRepositorySystemSession;
import org.codehaus.plexus.PlexusTestCase;
import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
public class ProjectInstallerTest
extends PlexusTestCase
{
private final File localRepo = new File( "target/tests/projects-local-repo" );
private ProjectInstaller installer;
@Override
public void setUp()
throws Exception
{
super.setUp();
installer = (ProjectInstaller) lookup( ProjectInstaller.class );
}
private MavenProject createProject( String groupId, String artifactId, String version, DefaultArtifact artifact )
{
MavenProject mp = new MavenProject();
mp.setGroupId( groupId );
mp.setArtifactId( artifactId );
mp.setVersion( version );
mp.setArtifact( artifact );
return mp;
}
private DefaultArtifact createArtifact( DefaultArtifactHandler artifactHandler, File file )
{
DefaultArtifact artifact =
new DefaultArtifact( "org.groupId", "mp-first", "1.0-SNAPSHOT", "compile", "jar", null, artifactHandler );
artifact.setFile( file );
return artifact;
}
private DefaultArtifact createArtifactWithClassifier( DefaultArtifactHandler artifactHandler, File file )
{
DefaultArtifact artifact =
new DefaultArtifact( "GROUPID", "ARTIFACTID", "VERSION", "compile", "jar", "CLASSIFIER", artifactHandler );
artifact.setFile( file );
return artifact;
}
public void testShouldInstallSingleArtifact()
throws Exception
{
DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
buildingRequest.setRepositorySession( repositorySession );
DefaultArtifactHandler artifactHandler = new DefaultArtifactHandler();
artifactHandler.setExtension( "jar" );
File artifactsDirectory = new File( "target/tests/install-projects-artifacts" );
artifactsDirectory.mkdirs();
File tmpFile = File.createTempFile( "install-single-artifact", ".jar", artifactsDirectory );
// tmpFile.deleteOnExit();
DefaultArtifact artifact = createArtifact( artifactHandler, tmpFile );
MavenProject mp = createProject( "org.groupId", "mp-first", "1.0-SNAPSHOT", artifact );
// DefaultArtifact artifactWithClassifier = createArtifactWithClassifier( artifactHandler, tmpFile );
// mp.addAttachedArtifact( artifactWithClassifier );
ProjectInstallerRequest pir = new ProjectInstallerRequest().setCreateChecksum( true )
.setProject( mp );
// ArtifactRepository ar = lookup( ArtifactRepository.class );
// ar.setId( "first" );
// ar.setLayout( );
installer.install( buildingRequest, pir, buildingRequest.getLocalRepository() );
// What we need to Test:
//
}
// public void testShouldInstallProjectArtifactAndArtifactWithClassifier()
// throws Exception
// {
// DefaultArtifactHandler artifactHandler = new DefaultArtifactHandler();
// artifactHandler.setExtension( "EXTENSION" );
//
// File artifactsDirectory = new File( "target/tests/artifacts" );
// artifactsDirectory.mkdirs();
// File tmpFile = File.createTempFile( "test-install", ".jar", artifactsDirectory );
//
// DefaultArtifact artifact = createArtifact( artifactHandler, tmpFile );
// MavenProject mp = createProject( "org.groupId", "mp-first", "1.0-SNAPSHOT", artifact );
//
// DefaultArtifact artifactWithClassifier = createArtifactWithClassifier( artifactHandler, tmpFile );
// mp.addAttachedArtifact( artifactWithClassifier );
//
// DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
// ProjectInstallerRequest pir = new ProjectInstallerRequest().setCreateChecksum( false )
// .setProject( mp );
// ArtifactRepository ar = lookup( ArtifactRepository.class );
// // ar.set
//
// MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
// repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepo ) );
//
// // installer.installProject( buildingRequest, pir, repositorySession.getLocalRepository());
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment