use of org.apache.maven.project.MavenProject in project maven-plugins by apache.
the class ShadeMojoTest method testShadeWithFilter.
/**
* Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
*
* @throws Exception
*/
public void testShadeWithFilter() throws Exception {
ShadeMojo mojo = new ShadeMojo();
// set createSourcesJar = true
Field createSourcesJar = ShadeMojo.class.getDeclaredField("createSourcesJar");
createSourcesJar.setAccessible(true);
createSourcesJar.set(mojo, Boolean.TRUE);
// configure artifactResolver (mocked) for mojo
ArtifactResolver mockArtifactResolver = new ArtifactResolver() {
@Override
public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final Artifact art) throws ArtifactResolverException {
return new ArtifactResult() {
@Override
public Artifact getArtifact() {
art.setResolved(true);
String fileName = art.getArtifactId() + "-" + art.getVersion() + (art.getClassifier() != null ? "-" + art.getClassifier() : "") + ".jar";
art.setFile(new File(fileName));
return art;
}
};
}
@Override
public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final ArtifactCoordinate coordinate) throws ArtifactResolverException {
return new ArtifactResult() {
@Override
public Artifact getArtifact() {
Artifact art = mock(Artifact.class);
when(art.getGroupId()).thenReturn(coordinate.getGroupId());
when(art.getArtifactId()).thenReturn(coordinate.getArtifactId());
when(art.getType()).thenReturn(coordinate.getExtension());
when(art.getClassifier()).thenReturn(coordinate.getClassifier());
when(art.isResolved()).thenReturn(true);
String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() + (coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "") + ".jar";
when(art.getFile()).thenReturn(new File(fileName));
return art;
}
};
}
};
Field artifactResolverField = ShadeMojo.class.getDeclaredField("artifactResolver");
artifactResolverField.setAccessible(true);
artifactResolverField.set(mojo, mockArtifactResolver);
// create and configure MavenProject
MavenProject project = new MavenProject();
ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE);
Artifact artifact = new DefaultArtifact("org.apache.myfaces.core", "myfaces-impl", VersionRange.createFromVersion("2.0.1-SNAPSHOT"), "compile", "jar", null, artifactHandler);
// setFile and setResolved
artifact = mockArtifactResolver.resolveArtifact(null, artifact).getArtifact();
project.setArtifact(artifact);
Field projectField = ShadeMojo.class.getDeclaredField("project");
projectField.setAccessible(true);
projectField.set(mojo, project);
// create and configure the ArchiveFilter
ArchiveFilter archiveFilter = new ArchiveFilter();
Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField("artifact");
archiveFilterArtifact.setAccessible(true);
archiveFilterArtifact.set(archiveFilter, "org.apache.myfaces.core:myfaces-impl");
// add ArchiveFilter to mojo
Field filtersField = ShadeMojo.class.getDeclaredField("filters");
filtersField.setAccessible(true);
filtersField.set(mojo, new ArchiveFilter[] { archiveFilter });
Field sessionField = ShadeMojo.class.getDeclaredField("session");
sessionField.setAccessible(true);
sessionField.set(mojo, mock(MavenSession.class));
// invoke getFilters()
Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", new Class[0]);
getFilters.setAccessible(true);
List<Filter> filters = (List<Filter>) getFilters.invoke(mojo);
// assertions - there must be one filter
assertEquals(1, filters.size());
// the filter must be able to filter the binary and the sources jar
Filter filter = filters.get(0);
// binary jar
assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT.jar")));
// sources jar
assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT-sources.jar")));
}
use of org.apache.maven.project.MavenProject in project maven-plugins by apache.
the class AddDependencySetsTaskTest method testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude.
public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude() throws ArchiveCreationException, InvalidAssemblerConfigurationException {
final MavenProject project = new MavenProject(new Model());
final Set<Artifact> artifacts = new HashSet<Artifact>();
final ArtifactMock am = new ArtifactMock(mockManager, "group", "artifact", "1.0", "jar", false);
am.setDependencyTrail(Collections.singletonList(project.getId()));
artifacts.add(am.getArtifact());
final ArtifactMock am2 = new ArtifactMock(mockManager, "group2", "artifact2", "1.0", "jar", false);
am2.setDependencyTrail(Collections.singletonList(project.getId()));
artifacts.add(am2.getArtifact());
final DependencySet dependencySet = new DependencySet();
dependencySet.addInclude("group:artifact");
dependencySet.setUseTransitiveFiltering(true);
final Logger logger = new ConsoleLogger(Logger.LEVEL_DEBUG, "test");
mockManager.replayAll();
final AddDependencySetsTask task = new AddDependencySetsTask(Collections.singletonList(dependencySet), artifacts, project, null, logger);
final Set<Artifact> result = task.resolveDependencyArtifacts(dependencySet);
assertNotNull(result);
assertEquals(1, result.size());
assertSame(am.getArtifact(), result.iterator().next());
mockManager.verifyAll();
}
use of org.apache.maven.project.MavenProject in project maven-plugins by apache.
the class AddFileSetsTaskTest method testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent.
public void testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent() throws ArchiveCreationException, AssemblyFormattingException {
final FileSet fs = new FileSet();
final String dirname = "dir";
fs.setDirectory(dirname);
final File archiveBaseDir = fileManager.createTempDir();
macTask.expectGetFinalName("finalName");
//macTask.expectGetProject( null );
expect(macTask.archiver.getOverrideDirectoryMode()).andReturn(-1);
expect(macTask.archiver.getOverrideFileMode()).andReturn(-1);
final MavenProject project = new MavenProject(new Model());
DefaultAssemblyArchiverTest.setupInterpolators(macTask.configSource);
mockManager.replayAll();
final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<FileSet>());
task.setLogger(new ConsoleLogger(Logger.LEVEL_DEBUG, "test"));
task.setProject(project);
task.addFileSet(fs, macTask.archiver, macTask.configSource, archiveBaseDir);
mockManager.verifyAll();
}
use of org.apache.maven.project.MavenProject in project maven-plugins by apache.
the class DefaultAssemblyReaderTest method testGetAssemblyFromDescriptorFile_ShouldReadAssembly.
public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly() throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException {
final Assembly assembly = new Assembly();
assembly.setId("test");
final FileSet fs = new FileSet();
fs.setDirectory("/dir");
assembly.addFileSet(fs);
final File assemblyFile = fileManager.createTempFile();
final File basedir = assemblyFile.getParentFile();
expect(configSource.getBasedir()).andReturn(basedir).anyTimes();
expect(configSource.getProject()).andReturn(new MavenProject(new Model())).anyTimes();
DefaultAssemblyArchiverTest.setupInterpolators(configSource);
Writer writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(assemblyFile), "UTF-8");
new AssemblyXpp3Writer().write(writer, assembly);
writer.close();
writer = null;
} finally {
IOUtil.close(writer);
}
mockManager.replayAll();
final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile(assemblyFile, configSource);
assertEquals(assembly.getId(), result.getId());
mockManager.verifyAll();
}
use of org.apache.maven.project.MavenProject in project maven-plugins by apache.
the class DefaultAssemblyReaderTest method performReadAssemblies.
private List<Assembly> performReadAssemblies(final File basedir, final String[] descriptors, final String[] descriptorRefs, final File descriptorDir, final boolean ignoreMissing) throws AssemblyReadException, InvalidAssemblerConfigurationException {
expect(configSource.getDescriptorReferences()).andReturn(descriptorRefs);
expect(configSource.getDescriptors()).andReturn(descriptors);
expect(configSource.getDescriptorSourceDirectory()).andReturn(descriptorDir);
expect(configSource.getBasedir()).andReturn(basedir).anyTimes();
expect(configSource.getProject()).andReturn(new MavenProject(new Model())).anyTimes();
expect(configSource.isIgnoreMissingDescriptor()).andReturn(ignoreMissing).anyTimes();
DefaultAssemblyArchiverTest.setupInterpolators(configSource);
mockManager.replayAll();
final List<Assembly> assemblies = new DefaultAssemblyReader().readAssemblies(configSource);
mockManager.verifyAll();
return assemblies;
}
Aggregations