Skip to content

Commit

Permalink
#125: Fixed sonar findings (#129)
Browse files Browse the repository at this point in the history
* #125: Fixed sonar findings

Co-authored-by: Muhammet Orazov <[email protected]>
  • Loading branch information
jakobbraun and morazow authored May 12, 2021
1 parent 28efa91 commit 096b73e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 36 deletions.
3 changes: 2 additions & 1 deletion doc/changes/changes_0.7.1.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Project keeper maven plugin 0.7.1, released 2021-05-10
# Project keeper maven plugin 0.7.1, released 2021-05-12

Code name: Support running on Windows

Expand All @@ -15,6 +15,7 @@ Code name: Support running on Windows

* #119: Fixed windows compatibility issues
* #123: Fixed jar compatibility (jar built on linux did not work as expected on Windows)
* #125: Fixed sonar findings

## Dependency Updates

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@

import static com.exasol.projectkeeper.validators.changesfile.ChangesFile.DEPENDENCY_UPDATES_HEADING;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;

import org.apache.maven.model.Build;
import org.apache.maven.model.Model;

import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.pom.MavenFileModelReader;
import com.exasol.projectkeeper.validators.changesfile.dependencies.DependencyChangeReport;
import com.exasol.projectkeeper.validators.changesfile.dependencies.DependencyChangeReportReader;
import com.exasol.projectkeeper.validators.changesfile.dependencies.DependencyChangeReportRenderer;
import com.exasol.projectkeeper.validators.changesfile.dependencies.*;

/**
* This class fixes the dependency section of a {@link ChangesFile}.
Expand Down Expand Up @@ -84,36 +78,10 @@ private Model getOldModel() {
private Model parseOldPomFile(final String pomFileContents) {
try (final var temporaryPomFile = new TemporaryPomFile(pomFileContents)) {
return this.mavenModelReader.readModel(temporaryPomFile.getPomFile().toFile());
} catch (final MavenFileModelReader.ReadFailedException | IOException exception) {
} catch (final MavenFileModelReader.ReadFailedException exception) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-38")
.message("Failed to parse pom file of previous release.").toString(), exception);
}
}

private static class TemporaryPomFile implements AutoCloseable {
private final Path pomFile;
private final Path tempDirectory;

public TemporaryPomFile(final String content) throws IOException {
this.tempDirectory = Files.createTempDirectory("pom");
this.tempDirectory.toFile().setReadable(false);
this.tempDirectory.toFile().setWritable(false);
this.tempDirectory.toFile().setExecutable(false);
this.tempDirectory.toFile().setReadable(true, true);
this.tempDirectory.toFile().setWritable(true, true);
this.tempDirectory.toFile().setExecutable(true, true);
this.pomFile = this.tempDirectory.resolve("pom.xml");
Files.writeString(this.pomFile, content);
}

@Override
public void close() throws IOException {
Files.delete(this.pomFile);
Files.delete(this.tempDirectory);
}

public Path getPomFile() {
return this.pomFile;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.exasol.projectkeeper.validators.changesfile;

import java.io.IOException;
import java.nio.file.*;

import com.exasol.errorreporting.ExaError;

/**
* This class temporarily saves a pom file so that it can be parsed by the
* {@link org.apache.maven.project.MavenProjectBuildingResult} that unfortunately only can parse files.
*/
class TemporaryPomFile implements AutoCloseable {
private final Path pomFile;

/**
* Create a new {@link TemporaryPomFile}.
*
* @param content pom file content
*/
public TemporaryPomFile(final String content) {
try {
this.pomFile = Files.createTempFile("pom-file-cache", ".xml");
Files.writeString(this.pomFile, content, StandardOpenOption.TRUNCATE_EXISTING);
} catch (final IOException exception) {
throw new IllegalStateException(
ExaError.messageBuilder("E-PK-58").message("Failed to temporarily store pom file on disk.")
.mitigation("Check the permissions for the temp directory of your OS.").toString(),
exception);
}
}

@Override
public void close() {
try {
Files.delete(this.pomFile);
} catch (final IOException exception) {
throw new IllegalStateException(
ExaError.messageBuilder("E-PK-59").message("Failed to remove temporary cache file.")
.mitigation("Check the file permissions.").toString(),
exception);
}
}

/**
* Get the path of the pom file on disk.
*
* @return path of the pom file
*/
public Path getPomFile() {
return this.pomFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.exasol.projectkeeper.validators.changesfile;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.io.IOException;
import java.nio.file.Files;

import org.junit.jupiter.api.Test;

class TemporaryPomFileTest {

@Test
void testCreate() throws IOException {
try (final TemporaryPomFile pomFile = new TemporaryPomFile("test")) {
assertThat(Files.readString(pomFile.getPomFile()), equalTo("test"));
}
}

@Test
void testDelete() throws IOException {
final TemporaryPomFile pomFile = new TemporaryPomFile("test");
pomFile.close();
assertFalse(Files.exists(pomFile.getPomFile()));
}
}

0 comments on commit 096b73e

Please sign in to comment.