Skip to content

Commit

Permalink
a unit test to validate BOMInputStream object fixes the issue in all …
Browse files Browse the repository at this point in the history
…cases

Signed-off-by: Gregory Anne <ganne@effisoft-group.com>
  • Loading branch information
Gregory Anne committed Aug 17, 2021
1 parent 619933f commit 64d10a7
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 2 deletions.
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@
<version>3.6.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-plugin-integration-testing</artifactId>
<type>pom</type>
<scope>test</scope>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-plugin-testing</artifactId>
<scope>test</scope>
<version>2.9.2</version>
</dependency>
</dependencies>

<prerequisites>
Expand All @@ -148,6 +163,21 @@

<build>
<plugins>
<plugin>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-lifecycle-plugin</artifactId>
<version>1.13.9</version>
<extensions>true</extensions>
<executions>
<execution>
<id>testProperties</id>
<phase>process-test-resources</phase>
<goals>
<goal>testProperties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/org/cyclonedx/maven/Issue117Test.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
66 changes: 66 additions & 0 deletions src/test/resources/issue-117/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>issue-117</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>

<name>Issue-117</name>

<licenses>
<license>
<name>Apache-2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
<version>${test.input.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>makeAggregateBom</goal>
</goals>
</execution>
</executions>
<configuration>
<projectType>library</projectType>
<schemaVersion>1.3</schemaVersion>
<includeBomSerialNumber>true</includeBomSerialNumber>
<includeCompileScope>true</includeCompileScope>
<includeProvidedScope>true</includeProvidedScope>
<includeRuntimeScope>true</includeRuntimeScope>
<includeSystemScope>true</includeSystemScope>
<includeTestScope>true</includeTestScope>
<includeLicenseText>true</includeLicenseText>
<outputFormat>all</outputFormat>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit 64d10a7

Please sign in to comment.