-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #125: Fixed sonar findings Co-authored-by: Muhammet Orazov <[email protected]>
- Loading branch information
1 parent
28efa91
commit 096b73e
Showing
4 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/exasol/projectkeeper/validators/changesfile/TemporaryPomFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/com/exasol/projectkeeper/validators/changesfile/TemporaryPomFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |