Skip to content

Commit

Permalink
Change to try with resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Oct 30, 2023
1 parent 7a258da commit debfed5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package de.jplag.reporting.jsonfactory;
package de.jplag.reporting.reportobject.writer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.reporting.reportobject.writer.FileWriter;

public class DummyWriter implements FileWriter<Object> {
private static final Logger logger = LoggerFactory.getLogger(DummyWriter.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
*/
public class JsonWriter implements FileWriter<Object> {
private static final Logger logger = LoggerFactory.getLogger(JsonWriter.class);

private static final ObjectMapper objectMapper = new ObjectMapper();
private static final String WRITE_ERROR = "Failed to write JSON file {}";

@Override
public void writeFile(Object fileToSave, String folderPath, String fileName) {
Path path = Path.of(folderPath, fileName);
try {
objectMapper.writeValue(Path.of(folderPath, fileName).toFile(), fileToSave);
objectMapper.writeValue(path.toFile(), fileToSave);
} catch (IOException e) {
logger.error("Failed to save json file " + fileName + ": " + e.getMessage(), e);
logger.error(WRITE_ERROR, e, path);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class TextWriter implements FileWriter<String> {
@Override
public void writeFile(String fileContent, String folderPath, String fileName) {
String path = Path.of(folderPath, fileName).toString();
try {
BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(path));
try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(path))) {
writer.write(fileContent);
writer.close();
} catch (IOException e) {
logger.error(WRITE_ERROR, e, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import de.jplag.TestBase;
import de.jplag.exceptions.ExitException;
import de.jplag.reporting.jsonfactory.ComparisonReportWriter;
import de.jplag.reporting.jsonfactory.DummyWriter;
import de.jplag.reporting.reportobject.writer.DummyWriter;
import de.jplag.reporting.reportobject.writer.FileWriter;

public class ComparisonReportWriterTest extends TestBase {
Expand Down

0 comments on commit debfed5

Please sign in to comment.