diff --git a/CHANGELOG.org b/CHANGELOG.org index 5ea4936..ec22da6 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +* next-release + +** Changed + + - Use `file-reader-writer` to replace own implementations (#290) + * [5.4.0] - 2020-03-18 ** Changed diff --git a/builder/pom.xml b/builder/pom.xml index 69aad6d..b7acb8c 100644 --- a/builder/pom.xml +++ b/builder/pom.xml @@ -41,6 +41,7 @@ 5-complexity net/kemitix/checkstyle-${kemitix.checkstyle.ruleset.level}.xml 1.3.2 + 1.0.0 @@ -56,6 +57,11 @@ + + net.kemitix + file-reader-writer + ${file-reader-writer.version} + org.projectlombok lombok diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java index a69ed7f..cdd0b42 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java @@ -1,6 +1,7 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.RequiredArgsConstructor; +import net.kemitix.files.FileReaderWriter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -37,4 +38,14 @@ public class BuilderConfiguration { private static AbstractMap.SimpleEntry entry(final K key, final V value) { return new AbstractMap.SimpleEntry<>(key, value); } + + /** + * A wrapper for reading and writing files. + * + * @return An instance of {@link FileReaderWriter}. + */ + @Bean + public FileReaderWriter fileReaderWriter() { + return new FileReaderWriter(); + } } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/FileReader.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/FileReader.java deleted file mode 100644 index d6f8614..0000000 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/FileReader.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.kemitix.checkstyle.ruleset.builder; - -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - -@Component -public class FileReader { - private static final String NEWLINE = "\n"; - - /** - * Reads the content from the file path. - * - *

File content will be read using UTF-8 encoding and line endings will - * be replaced with a newline (\n).

- * - * @param path the file to read - * @throws IOException if there is an error. - */ - public String read(final Path path) throws IOException { - return String.join(NEWLINE, - Files.readAllLines(path, StandardCharsets.UTF_8)); - } -} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/FileWriter.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/FileWriter.java deleted file mode 100644 index 3356e10..0000000 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/FileWriter.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.kemitix.checkstyle.ruleset.builder; - -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; - -@Component -public class FileWriter { - - /** - * Writes the content to the file path, replacing any existing file. - * - *

File content will be written using UTF-8 encoding.

- * - * @param path the file to write - * @param content the content to write - * @throws IOException if there is an error. - */ - public void write( - final Path path, - final String content - ) throws IOException { - Files.write(path, - content.getBytes(StandardCharsets.UTF_8), - StandardOpenOption.TRUNCATE_EXISTING); - } -} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java index edf08ee..b4995bf 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java @@ -1,10 +1,11 @@ package net.kemitix.checkstyle.ruleset.builder; import lombok.RequiredArgsConstructor; +import net.kemitix.files.FileReaderWriter; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; -import java.nio.file.Path; +import java.io.File; import java.nio.file.Paths; /** @@ -19,16 +20,15 @@ class ReadmeWriter implements CommandLineRunner { private final TemplateProperties templateProperties; private final OutputProperties outputProperties; private final ReadmeBuilder readmeBuilder; - private final FileReader fileReader; - private final FileWriter fileWriter; + private final FileReaderWriter fileReaderWriter; @Override public void run(final String... args) throws Exception { - Path templatePath = templateProperties.getReadmeTemplate(); - String templateBody = fileReader.read(templatePath); - Path outputPath = Paths.get(outputProperties.getReadme()); + File templateFile = templateProperties.getReadmeTemplate(); + String templateBody = fileReaderWriter.read(templateFile); + File outputFile = Paths.get(outputProperties.getReadme()).toFile(); String outputBody = readmeBuilder.build(templateBody); - fileWriter.write(outputPath, outputBody); + fileReaderWriter.write(outputFile, outputBody); } } diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateProperties.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateProperties.java index 259d759..d99341b 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateProperties.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/TemplateProperties.java @@ -5,6 +5,7 @@ import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; +import java.io.File; import java.nio.file.Path; /** @@ -26,7 +27,7 @@ class TemplateProperties { /** * Template for README.md file. */ - private Path readmeTemplate; + private File readmeTemplate; /** * The directory containing the README fragments. diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/FileWriterReaderTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/FileWriterReaderTest.java deleted file mode 100644 index ad85ea4..0000000 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/FileWriterReaderTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package net.kemitix.checkstyle.ruleset.builder; - -import org.junit.Before; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.UUID; - -import static org.assertj.core.api.Assertions.assertThat; - -public class FileWriterReaderTest { - - - private String line1 = UUID.randomUUID().toString(); - private String line2 = UUID.randomUUID().toString(); - private String body = line1 + "\n" + line2; - - @org.junit.Rule - public TemporaryFolder folder = new TemporaryFolder(); - - private Path path; - - private FileWriter writer = new FileWriter(); - private FileReader reader = new FileReader(); - - @Before - public void setUp() throws IOException { - path = folder.newFile().toPath(); - } - - @Test - public void writeTheReadFile() throws IOException { - //when - writer.write(path, body); - String read = reader.read(path); - //then - assertThat(read).isEqualTo(body); - } -} \ No newline at end of file diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java index bfd70a9..a4edf52 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriterTest.java @@ -1,13 +1,14 @@ package net.kemitix.checkstyle.ruleset.builder; +import net.kemitix.files.FileReaderWriter; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.io.File; import java.io.IOException; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; @@ -28,16 +29,13 @@ public class ReadmeWriterTest { private OutputProperties outputProperties = new OutputProperties(); @Mock - private FileReader fileReader; - - @Mock - private FileWriter fileWriter; + private FileReaderWriter fileReaderWriter; @InjectMocks private ReadmeWriter readmeWriter; @Mock - private Path templatePath; + private File templateFile; private String outputPath = UUID.randomUUID().toString(); private String templateBody = UUID.randomUUID().toString(); private String formattedOutput = UUID.randomUUID().toString(); @@ -48,11 +46,11 @@ public class ReadmeWriterTest { readmeWriter = new ReadmeWriter(templateProperties, outputProperties, - readmeBuilder, fileReader, fileWriter); + readmeBuilder, fileReaderWriter); - templateProperties.setReadmeTemplate(templatePath); + templateProperties.setReadmeTemplate(templateFile); outputProperties.setReadme(outputPath); - given(fileReader.read(templatePath)) + given(fileReaderWriter.read(templateFile)) .willReturn(templateBody); given(readmeBuilder.build(templateBody)) .willReturn(formattedOutput); @@ -63,7 +61,8 @@ public class ReadmeWriterTest { //when readmeWriter.run(); //then - verify(fileWriter).write(Paths.get(outputPath), formattedOutput); + verify(fileReaderWriter) + .write(Paths.get(outputPath).toFile(), formattedOutput); } } diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java index 1b4e044..db3da8e 100644 --- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java +++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/TemplatePropertiesTest.java @@ -4,6 +4,7 @@ import org.assertj.core.api.SoftAssertions; import org.junit.Before; import org.junit.Test; +import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; @@ -25,7 +26,7 @@ public class TemplatePropertiesTest { public void setAndGet() throws Exception { //given final Path checkstyleXml = Paths.get("checkstyle.xml"); - final Path readmeTemplate = Paths.get("readme.md"); + final File readmeTemplate = Paths.get("readme.md").toFile(); final Path readmeFragments = Paths.get("readme.dir"); //when templateProperties.setCheckstyleXml(checkstyleXml);