From 64d10a7a535db99d7f3510edf97f348151fefce2 Mon Sep 17 00:00:00 2001 From: Gregory Anne Date: Tue, 17 Aug 2021 18:26:57 +0200 Subject: [PATCH] a unit test to validate BOMInputStream object fixes the issue in all cases Signed-off-by: Gregory Anne --- pom.xml | 30 +++++++++ .../cyclonedx/maven/BaseCycloneDxMojo.java | 13 +++- .../org/cyclonedx/maven/Issue117Test.java | 57 ++++++++++++++++ src/test/resources/issue-117/pom.xml | 66 +++++++++++++++++++ 4 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/cyclonedx/maven/Issue117Test.java create mode 100644 src/test/resources/issue-117/pom.xml diff --git a/pom.xml b/pom.xml index 8333ccd1..ed4aee2c 100644 --- a/pom.xml +++ b/pom.xml @@ -140,6 +140,21 @@ 3.6.1 provided + + + io.takari.maven.plugins + takari-plugin-integration-testing + pom + test + 2.9.2 + + + + io.takari.maven.plugins + takari-plugin-testing + test + 2.9.2 + @@ -148,6 +163,21 @@ + + io.takari.maven.plugins + takari-lifecycle-plugin + 1.13.9 + true + + + testProperties + process-test-resources + + testProperties + + + + org.apache.maven.plugins maven-plugin-plugin diff --git a/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java b/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java index a314685a..4e652bbd 100644 --- a/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java +++ b/src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java @@ -95,6 +95,7 @@ import java.util.UUID; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import org.apache.commons.io.input.BOMInputStream; import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; @@ -727,10 +728,18 @@ private MavenProject readPom(File file) throws IOException { private MavenProject readPom(InputStream in) { try { final MavenXpp3Reader mavenreader = new MavenXpp3Reader(); - try (final InputStreamReader reader = new InputStreamReader(in)) { + try (final InputStreamReader reader = new InputStreamReader(new BOMInputStream(in))) { final Model model = mavenreader.read(reader); return new MavenProject(model); - } + } + //if you don't like BOMInputStream you can also escape the error this way: +// catch (XmlPullParserException xppe){ +// if (! xppe.getMessage().startsWith("only whitespace content allowed before start tag")){ +// throw xppe; +// } else { +// getLog().debug("The pom.xml starts with a Byte Order Marker and MavenXpp3Reader doesn't like it"); +// } +// } } catch (XmlPullParserException | IOException e) { getLog().error("An error occurred attempting to read POM", e); } diff --git a/src/test/java/org/cyclonedx/maven/Issue117Test.java b/src/test/java/org/cyclonedx/maven/Issue117Test.java new file mode 100644 index 00000000..d5a0133a --- /dev/null +++ b/src/test/java/org/cyclonedx/maven/Issue117Test.java @@ -0,0 +1,57 @@ +package org.cyclonedx.maven; + +import io.takari.maven.testing.TestResources; +import io.takari.maven.testing.executor.MavenRuntime; +import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder; +import io.takari.maven.testing.executor.MavenVersions; +import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner; +import java.io.File; +import java.io.IOException; +import java.util.Properties; +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(MavenJUnitTestRunner.class) +@MavenVersions({"3.6.3"}) +public class Issue117Test { + + @Rule + public final TestResources resources = new TestResources( + "target/test-classes", + "target/test-classes/transformed-projects" + ); + + public final MavenRuntime verifier; + + public Issue117Test(MavenRuntimeBuilder runtimeBuilder) + throws Exception { + this.verifier = runtimeBuilder.build(); //.withCliOptions(opts) // // + } + + @Test + public void testPluginWithActiviti() throws IOException, Exception { + File projectDirTransformed = new File( + "target/test-classes/transformed-projects/issue-117" + ); + if (projectDirTransformed.exists()) { + FileUtils.cleanDirectory(projectDirTransformed); + projectDirTransformed.delete(); + } + + File projDir = resources.getBasedir("issue-117"); + + Properties props = new Properties(); + + props.load(Issue117Test.class.getClassLoader().getResourceAsStream("test.properties")); + String projectVersion = String.class.cast(props.get("project.version")); + verifier + .forProject(projDir) // + .withCliOption("-Dtest.input.version=" + projectVersion) // debug + .withCliOption("-X") // debug + .withCliOption("-B") + .execute("clean", "package") + .assertErrorFreeLog(); + } +} diff --git a/src/test/resources/issue-117/pom.xml b/src/test/resources/issue-117/pom.xml new file mode 100644 index 00000000..990f054b --- /dev/null +++ b/src/test/resources/issue-117/pom.xml @@ -0,0 +1,66 @@ + + + + 4.0.0 + + com.example + issue-117 + jar + 1.0.0 + + Issue-117 + + + + Apache-2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + 1.8 + 1.8 + UTF-8 + UTF-8 + + + + + org.activiti + activiti-engine + 5.14 + + + + + + org.cyclonedx + cyclonedx-maven-plugin + ${test.input.version} + + + package + + makeAggregateBom + + + + + library + 1.3 + true + true + true + true + true + true + true + all + + + + + +