Skip to content

Commit

Permalink
Merge pull request #277 from CycloneDX/simplify-its
Browse files Browse the repository at this point in the history
simplify ITs code
  • Loading branch information
hboutemy committed Feb 13, 2023
2 parents 55b4bac + b069b57 commit e8f3762
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 196 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@

<build>
<plugins>
<plugin>
<plugin><!-- https://github.com/takari/takari-plugin-testing-project/blob/master/testproperties.md -->
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-lifecycle-plugin</artifactId>
<version>2.0.8</version>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/cyclonedx/maven/BaseCycloneDxMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/org/cyclonedx/maven/BaseMavenVerifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import org.junit.Rule;

public class BaseMavenVerifier {

@Rule
public final TestResources resources = new TestResources("target/test-classes", "target/test-classes/transformed-projects");

public final MavenRuntime verifier;

public BaseMavenVerifier(MavenRuntimeBuilder runtimeBuilder) throws Exception {
this.verifier = runtimeBuilder/*.withCliOptions(opts)*/.build();
}

public String getCurrentVersion() throws IOException {
// extract current cyclonedx-maven-plugin version from test.properties https://github.com/takari/takari-plugin-testing-project/blob/master/testproperties.md
Properties props = new Properties();
props.load(this.getClass().getClassLoader().getResourceAsStream("test.properties"));
return (String) props.get("project.version");
}

// source: https://github.com/takari/takari-plugin-testing-project/blob/master/takari-plugin-testing/src/main/java/io/takari/maven/testing/AbstractTestResources.java#L103
protected static String fileRead(File file, boolean normalizeEOL) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
if (normalizeEOL) {
String str;
while ((str = r.readLine()) != null) {
sb.append(str).append('\n');
}
} else {
int ch;
while ((ch = r.read()) != -1) {
sb.append((char) ch);
}
}
}
return sb.toString();
}
}
53 changes: 17 additions & 36 deletions src/test/java/org/cyclonedx/maven/BomDependenciesTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
package org.cyclonedx.maven;

import io.takari.maven.testing.TestResources;
import io.takari.maven.testing.executor.MavenExecution;
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 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.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import static org.junit.Assert.*;
import io.takari.maven.testing.executor.MavenExecution;
import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder;
import io.takari.maven.testing.executor.MavenVersions;
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner;

/**
* test for https://github.com/CycloneDX/cyclonedx-maven-plugin/issues/256
*
*/
@RunWith(MavenJUnitTestRunner.class)
@MavenVersions({"3.6.3"})
public class BomDependenciesTest {
public class BomDependenciesTest extends BaseMavenVerifier {

private static final String SHARED_DEPENDENCY1 = "pkg:maven/com.example/shared_dependency1@1.0.0?type=jar";
private static final String SHARED_DEPENDENCY2 = "pkg:maven/com.example/shared_dependency2@1.0.0?type=jar";
Expand All @@ -49,17 +51,8 @@ public class BomDependenciesTest {
private static final String PROVIDED_DEPENDENCY = "pkg:maven/com.example/provided_dependency@1.0.0?type=jar";
private static final String DEPENDENCY1 = "pkg:maven/com.example/dependency1@1.0.0?type=jar";

@Rule
public final TestResources resources = new TestResources(
"target/test-classes",
"target/test-classes/transformed-projects"
);

public final MavenRuntime verifier;

public BomDependenciesTest(MavenRuntimeBuilder runtimeBuilder)
throws Exception {
this.verifier = runtimeBuilder.build(); //.withCliOptions(opts) // //
public BomDependenciesTest(MavenRuntimeBuilder runtimeBuilder) throws Exception {
super(runtimeBuilder);
}

@Test
Expand Down Expand Up @@ -332,23 +325,11 @@ private void testHiddenVersionedTransitiveDependencies(final File projDir) throw
}

private File cleanAndBuild(final String[] excludeTypes) throws Exception {
File projectDirTransformed = new File(
"target/test-classes/transformed-projects/bom-dependencies"
);
if (projectDirTransformed.exists()) {
FileUtils.cleanDirectory(projectDirTransformed);
projectDirTransformed.delete();
}

File projDir = resources.getBasedir("bom-dependencies");

Properties props = new Properties();

props.load(BomDependenciesTest.class.getClassLoader().getResourceAsStream("test.properties"));
String projectVersion = (String) props.get("project.version");
final MavenExecution initExecution = verifier
.forProject(projDir) //
.withCliOption("-Dtest.input.version=" + projectVersion) // debug
.forProject(projDir)
.withCliOption("-Dcurrent.version=" + getCurrentVersion()) // inject cyclonedx-maven-plugin version
.withCliOption("-X") // debug
.withCliOption("-B");
final MavenExecution execution;
Expand Down
71 changes: 16 additions & 55 deletions src/test/java/org/cyclonedx/maven/Issue116Test.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,36 @@
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.*;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import static org.junit.Assert.assertEquals;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder;
import io.takari.maven.testing.executor.MavenVersions;
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner;

/**
* test for https://github.com/CycloneDX/cyclonedx-maven-plugin/issues/116
* dependencies in BOM file are missing a reference
*/
@RunWith(MavenJUnitTestRunner.class)
@MavenVersions({"3.6.3"})
public class Issue116Test {

@Rule
public final TestResources resources = new TestResources(
"target/test-classes",
"target/test-classes/transformed-projects"
);
public class Issue116Test extends BaseMavenVerifier {

public final MavenRuntime verifier;

public Issue116Test(MavenRuntimeBuilder runtimeBuilder)
throws Exception {
this.verifier = runtimeBuilder.build(); //.withCliOptions(opts) // //
public Issue116Test(MavenRuntimeBuilder runtimeBuilder) throws Exception {
super(runtimeBuilder);
}

@Test
public void testPluginWithActiviti() throws Exception {
File projectDirTransformed = new File(
"target/test-classes/transformed-projects/issue-116"
);
if (projectDirTransformed.exists()) {
FileUtils.cleanDirectory(projectDirTransformed);
projectDirTransformed.delete();
}

File projDir = resources.getBasedir("issue-116");

Properties props = new Properties();

props.load(Issue116Test.class.getClassLoader().getResourceAsStream("test.properties"));
String projectVersion = (String) props.get("project.version");
verifier
.forProject(projDir) //
.withCliOption("-Dtest.input.version=" + projectVersion) // debug
.forProject(projDir)
.withCliOption("-Dcurrent.version=" + getCurrentVersion()) // inject cyclonedx-maven-plugin version
.withCliOption("-X") // debug
.withCliOption("-B")
.execute("clean", "package")
Expand All @@ -61,23 +41,4 @@ public void testPluginWithActiviti() throws Exception {
int matches = StringUtils.countMatches(bomContents, "<dependency ref=\"pkg:maven/org.apache.commons/commons-lang3@3.1?type=jar\"/>");
assertEquals(4, matches); // 1 for the definition, 3 for each of its usages
}

// source: https://github.com/takari/takari-plugin-testing-project/blob/master/takari-plugin-testing/src/main/java/io/takari/maven/testing/AbstractTestResources.java#L103
private static String fileRead(File file, boolean normalizeEOL) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
if (normalizeEOL) {
String str;
while ((str = r.readLine()) != null) {
sb.append(str).append('\n');
}
} else {
int ch;
while ((ch = r.read()) != -1) {
sb.append((char) ch);
}
}
}
return sb.toString();
}
}
50 changes: 15 additions & 35 deletions src/test/java/org/cyclonedx/maven/Issue117Test.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,33 @@
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.util.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;

import org.junit.Test;
import org.junit.runner.RunWith;

import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder;
import io.takari.maven.testing.executor.MavenVersions;
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner;

/**
* test for https://github.com/CycloneDX/cyclonedx-maven-plugin/issues/117
* issue with pom.xml UTF-8 encoding with Byte Order Mark
*/
@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 class Issue117Test extends BaseMavenVerifier {

public final MavenRuntime verifier;

public Issue117Test(MavenRuntimeBuilder runtimeBuilder)
throws Exception {
this.verifier = runtimeBuilder.build(); //.withCliOptions(opts) // //
public Issue117Test(MavenRuntimeBuilder runtimeBuilder) throws Exception {
super(runtimeBuilder);
}

@Test
public void testPluginWithActiviti() throws Exception {
File projectDirTransformed = new File(
"target/test-classes/transformed-projects/issue-117"
);
if (projectDirTransformed.exists()) {
FileUtils.cleanDirectory(projectDirTransformed);
projectDirTransformed.delete();
}

public void testByteOrderMarkFromActiviti() throws Exception {
File projDir = resources.getBasedir("issue-117");

Properties props = new Properties();

props.load(Issue117Test.class.getClassLoader().getResourceAsStream("test.properties"));
String projectVersion = (String) props.get("project.version");
verifier
.forProject(projDir) //
.withCliOption("-Dtest.input.version=" + projectVersion) // debug
.forProject(projDir)
.withCliOption("-Dcurrent.version=" + getCurrentVersion()) // inject cyclonedx-maven-plugin version
.withCliOption("-X") // debug
.withCliOption("-B")
.execute("clean", "package")
Expand Down

0 comments on commit e8f3762

Please sign in to comment.