Skip to content

Commit

Permalink
Fix logging in Maven Mojo test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jan 30, 2024
1 parent 92f7943 commit cb8b942
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.exasol.projectkeeper.plugin;

import static com.exasol.projectkeeper.plugin.TestEnvBuilder.CURRENT_VERSION;
import static java.util.stream.Collectors.joining;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.io.FileMatchers.anExistingFile;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.*;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.List;
import java.util.logging.Logger;

import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
Expand All @@ -29,18 +32,31 @@

class ProjectKeeperMojoIT {
private static MavenIntegrationTestEnvironment mavenIntegrationTestEnvironment;
private static final Logger LOG = Logger.getLogger(ProjectKeeperMojoIT.class.getName());

@TempDir
protected Path projectDir;
private Verifier verifier;

@BeforeAll
static void beforeAll() {
mavenIntegrationTestEnvironment = TestEnvBuilder.getTestEnv();
}

@BeforeEach
void beforeEach() throws IOException, GitAPIException {
void beforeEach(final TestInfo test) throws IOException, GitAPIException {
Git.init().setDirectory(this.projectDir.toFile()).call().close();
new MvnProjectWithProjectKeeperPluginWriter(CURRENT_VERSION).writeAsPomToProject(this.projectDir);
LOG.info(() -> "Running test " + test.getDisplayName() + "...");
verifier = mavenIntegrationTestEnvironment.getVerifier(this.projectDir);
}

@AfterEach
void logMavenOutput(final TestInfo test) throws VerificationException {
verifier.resetStreams();
final List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
LOG.info(() -> "Maven log output for test " + test.getDisplayName() + ": " + lines.size() + " lines\n"
+ lines.stream().collect(joining("\n")));
}

@Test
Expand All @@ -51,7 +67,6 @@ void testVerify() throws IOException {
"sources:\n" + //
" - type: maven\n" + //
" path: pom.xml\n");
final Verifier verifier = getVerifier();
final VerificationException exception = assertThrows(VerificationException.class,
() -> verifier.executeGoal("project-keeper:verify"));
final String output = exception.getMessage();
Expand All @@ -70,7 +85,6 @@ void testJacocoAgentIsExtracted() throws VerificationException, IOException {
" modules:\n" + //
" - integration_tests\n" + //
" - udf_coverage\n");
final Verifier verifier = getVerifier();
verifier.executeGoal("project-keeper:fix");
verifier.executeGoal("package");
assertThat(this.projectDir.resolve(Path.of("target", "jacoco-agent", "org.jacoco.agent-runtime.jar")).toFile(),
Expand All @@ -79,12 +93,10 @@ void testJacocoAgentIsExtracted() throws VerificationException, IOException {

@Test
void testUpgradeDependencies() throws VerificationException, IOException {
final PrintStream out = System.out;
Files.writeString(this.projectDir.resolve(".project-keeper.yml"), //
"sources:\n" + //
" - type: maven\n" + //
" path: pom.xml\n");
final Verifier verifier = getVerifier();
verifier.executeGoal("project-keeper:fix");
assertThat("original version", readPom().getVersion(), equalTo("0.1.0"));

Expand All @@ -94,9 +106,6 @@ void testUpgradeDependencies() throws VerificationException, IOException {
verifier.executeGoal("project-keeper:update-dependencies");
verifier.verify(true);

final List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
out.println("Got " + lines.size() + " lines:");
lines.forEach(out::println);
assertThat("incremented version", readPom().getVersion(), equalTo("0.1.1"));
}

Expand Down Expand Up @@ -128,13 +137,8 @@ void testSkip(final String phase) throws IOException, VerificationException {
"sources:\n" + //
" - type: maven\n" + //
" path: pom.xml\n");
final Verifier verifier = getVerifier();
verifier.setSystemProperty("project-keeper.skip", "true");
verifier.executeGoal("project-keeper:" + phase);
verifier.verifyTextInLog("Skipping project-keeper.");
}

protected Verifier getVerifier() {
return mavenIntegrationTestEnvironment.getVerifier(this.projectDir);
}
}

0 comments on commit cb8b942

Please sign in to comment.