Skip to content

Commit

Permalink
[ISSUE 606] Added transformer-maven-plugin integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
jluehe committed Aug 18, 2024
1 parent 2ccce67 commit 94e34c8
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class TransformerRunMojo extends AbstractMojo {
@Parameter(defaultValue = "true", property = "transformer-plugin.overwrite", required = true)
private boolean overwrite;

@Parameter(defaultValue = "false", property = "transformer-plugin.strip-signatures", required = false)
private boolean stripSignatures;

@Parameter(defaultValue = "true", property = "transformer-plugin.attach", required = true)
private boolean attach;

Expand Down Expand Up @@ -131,6 +134,8 @@ public boolean hasOption(AppOption option) {
return overwrite;
case INVERT :
return invert;
case STRIP_SIGNATURES:
return stripSignatures;
default :
return TransformOptions.super.hasOption(option);
}
Expand Down Expand Up @@ -257,4 +262,7 @@ void setAttach(boolean attach) {
this.attach = attach;
}

void setStripSignatures(boolean stripSignatures) {
this.stripSignatures = stripSignatures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@

package org.eclipse.transformer.maven;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
Expand All @@ -28,8 +33,12 @@
import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.eclipse.transformer.action.ElementAction;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.FileAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -66,7 +75,7 @@ public void testProjectArtifactTransformerPlugin() throws Exception {

final MavenProject mavenProject = createMavenProject(modelDirectory, pom, "war", "rest-sample");
mavenProject.getArtifact()
.setFile(createService());
.setFile(createService("war", targetDirectory));

mojo.setProject(mavenProject);
mojo.setClassifier("transformed");
Expand Down Expand Up @@ -113,11 +122,11 @@ public void testMultipleArtifactTransformerPlugin() throws Exception {
mojo.setOutputDirectory(tmp.newFolder(name.getMethodName()));

mojo.getProjectHelper()
.attachArtifact(mavenProject, "zip", "test1", createService());
.attachArtifact(mavenProject, "zip", "test1", createService("war", targetDirectory));
mojo.getProjectHelper()
.attachArtifact(mavenProject, "zip", "test2", createService());
.attachArtifact(mavenProject, "zip", "test2", createService("war", targetDirectory));
mojo.getProjectHelper()
.attachArtifact(mavenProject, "zip", "test3", createService());
.attachArtifact(mavenProject, "zip", "test3", createService("war", targetDirectory));

final Artifact[] sourceArtifacts = mojo.getSourceArtifacts();
assertEquals(3, sourceArtifacts.length);
Expand Down Expand Up @@ -171,7 +180,7 @@ public void testProjectArtifactTransformerPluginNoAttach() throws Exception {

final MavenProject mavenProject = createMavenProject(modelDirectory, pom, "war", "rest-sample");
mavenProject.getArtifact()
.setFile(createService());
.setFile(createService("war", targetDirectory));

mojo.setProject(mavenProject);
mojo.setClassifier("transformed");
Expand All @@ -190,6 +199,53 @@ public void testProjectArtifactTransformerPluginNoAttach() throws Exception {
.size());
}

@Test
public void testProjectArtifactTransformerPluginStripSignatureFiles() throws Exception {
final TransformerRunMojo mojo = new TransformerRunMojo();
mojo.setProjectHelper(this.rule.lookup(MavenProjectHelper.class));
mojo.setOverwrite(true);
mojo.setOutputDirectory(tmp.newFolder(name.getMethodName()));
mojo.setAttach(true);
mojo.setStripSignatures(true);

This comment has been minimized.

Copy link
@jluehe

jluehe Aug 18, 2024

Author Contributor

Instruct the plugin to strip signature files


assertNotNull(mojo);

final File targetDirectory = this.resources.getBasedir("transform-build-jar-artifact");
final File modelDirectory = new File(targetDirectory, "target/model");
final File pom = new File(targetDirectory, "pom.xml");

final MavenProject mavenProject = createMavenProject(modelDirectory, pom, "jar", "rest-sample");
mavenProject.getArtifact()
.setFile(createService("jar", targetDirectory));

mojo.setProject(mavenProject);
mojo.setClassifier("transformed");

final Artifact[] sourceArtifacts = mojo.getSourceArtifacts();
assertEquals(1, sourceArtifacts.length);
final Artifact sourceArtifact = sourceArtifacts[0];
assertEquals("org.superbiz.rest", sourceArtifact.getGroupId());
assertEquals("rest-sample", sourceArtifact.getArtifactId());
assertEquals("1.0-SNAPSHOT", sourceArtifact.getVersion());
assertEquals("jar", sourceArtifact.getType());
assertNull(sourceArtifact.getClassifier());
assertThat(getSignatureFileEntries(sourceArtifact.getFile())).containsExactly("META-INF/MYKEY.SF", "META-INF/MYKEY.DSA");

This comment has been minimized.

Copy link
@jluehe

jluehe Aug 18, 2024

Author Contributor

Assert that signature files are present in the source artifact


mojo.transform(sourceArtifacts[0]);

assertEquals(1, mavenProject.getAttachedArtifacts()
.size());
final Artifact transformedArtifact = mavenProject.getAttachedArtifacts()
.get(0);

assertEquals("org.superbiz.rest", transformedArtifact.getGroupId());
assertEquals("rest-sample", transformedArtifact.getArtifactId());
assertEquals("1.0-SNAPSHOT", transformedArtifact.getVersion());
assertEquals("jar", transformedArtifact.getType());
assertEquals("transformed", transformedArtifact.getClassifier());
assertThat(getSignatureFileEntries(transformedArtifact.getFile())).isEmpty();

This comment has been minimized.

Copy link
@jluehe

jluehe Aug 18, 2024

Author Contributor

Assert that signature files have been removed from the transformed artifact

}

public MavenProject createMavenProject(final File modelDirectory, final File pom, final String packaging,
final String artfifactId) {
final MavenProject mavenProject = new MavenProject();
Expand All @@ -205,19 +261,44 @@ public MavenProject createMavenProject(final File modelDirectory, final File pom
.setOutputDirectory(modelDirectory.getAbsolutePath());
mavenProject.setArtifact(
new DefaultArtifact(mavenProject.getGroupId(), mavenProject.getArtifactId(), mavenProject.getVersion(),
(String) null, "war", (String) null, new DefaultArtifactHandlerStub(packaging, null)));
null, packaging, null, new DefaultArtifactHandlerStub(packaging, null)));
return mavenProject;
}

public File createService() throws IOException {
final File tempFile = File.createTempFile("service", ".war");
private static File createService(String packaging, File targetDirectory) throws IOException {
final File tempFile = File.createTempFile("service", "." + packaging);
tempFile.delete();

final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "service.war")
.addClass(EchoService.class);
final Archive archive;
if (packaging.equals("jar")) {
archive = ShrinkWrap.create(JavaArchive.class, "service." + packaging)
.addClass(EchoService.class);
} else {
archive = ShrinkWrap.create(WebArchive.class, "service." + packaging)
.addClass(EchoService.class);
}

webArchive.as(ZipExporter.class)
if (packaging.equals("jar")) {
archive.add(new FileAsset(new File(targetDirectory, "META-INF/MYKEY.SF")), "META-INF/MYKEY.SF");
archive.add(new FileAsset(new File(targetDirectory, "META-INF/MYKEY.DSA")), "META-INF/MYKEY.DSA");
}

archive.as(ZipExporter.class)
.exportTo(tempFile, true);
return tempFile;
}

private static Set<String> getSignatureFileEntries(File file) throws IOException {
try (ZipFile zipFile = new ZipFile(file)) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
final Set<String> signatureFiles = new HashSet<>();
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
if (ElementAction.SIGNATURE_FILE_PATTERN.matcher(zipEntry.getName()).matches()) {
signatureFiles.add(zipEntry.getName());
}
}
return signatureFiles;
}
}
}
Binary file not shown.
Loading

0 comments on commit 94e34c8

Please sign in to comment.