Harmonize assertions on text file content

This commit harmonizes assertions on text file content by sharing the
code that reads and validate candidates. Make sure that streams are
properly closed which may fix build failures on Windows.

See gh-862
This commit is contained in:
Stephane Nicoll 2019-03-13 14:42:23 +01:00
parent 9f4c6a5326
commit ec6ac4b1d8
12 changed files with 117 additions and 145 deletions

View File

@ -16,10 +16,7 @@
package io.spring.initializr.generator.spring.build.gradle; package io.spring.initializr.generator.spring.build.gradle;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
@ -46,8 +43,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -115,7 +110,7 @@ class GradleProjectGenerationConfigurationTests {
} }
@Test @Test
void buildDotGradleIsContributedWhenGeneratingGradleProject() throws IOException { void buildDotGradleIsContributedWhenGeneratingGradleProject() {
ProjectDescription description = new ProjectDescription(); ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.1.0.RELEASE")); description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
description.setLanguage(new JavaLanguage("11")); description.setLanguage(new JavaLanguage("11"));
@ -124,8 +119,7 @@ class GradleProjectGenerationConfigurationTests {
ProjectStructure projectStructure = this.projectTester.generate(description); ProjectStructure projectStructure = this.projectTester.generate(description);
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths).contains("build.gradle"); assertThat(relativePaths).contains("build.gradle");
Path path = projectStructure.resolve("build.gradle"); List<String> lines = projectStructure.readAllLines("build.gradle");
String[] lines = readAllLines(path);
assertThat(lines).containsExactly("plugins {", assertThat(lines).containsExactly("plugins {",
" id 'org.springframework.boot' version '2.1.0.RELEASE'", " id 'org.springframework.boot' version '2.1.0.RELEASE'",
" id 'java'", "}", "", " id 'java'", "}", "",
@ -173,12 +167,4 @@ class GradleProjectGenerationConfigurationTests {
assertThat(generate).hasSize((contributorExpected) ? 1 : 0); assertThat(generate).hasSize((contributorExpected) ? 1 : 0);
} }
private static String[] readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
String[] lines = content.split("\\r?\\n");
assertThat(content).endsWith(System.lineSeparator());
return lines;
}
} }

View File

@ -24,6 +24,7 @@ import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.io.IndentingWriterFactory; import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy; import io.spring.initializr.generator.io.SimpleIndentStrategy;
import io.spring.initializr.generator.test.project.ProjectStructure;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
@ -78,9 +79,7 @@ class SettingsGradleProjectContributorTests {
Path projectDir = Files.createTempDirectory(this.directory, "project-"); Path projectDir = Files.createTempDirectory(this.directory, "project-");
new SettingsGradleProjectContributor(build, indentingWriterFactory) new SettingsGradleProjectContributor(build, indentingWriterFactory)
.contribute(projectDir); .contribute(projectDir);
Path settingsGradle = projectDir.resolve("settings.gradle"); return new ProjectStructure(projectDir).readAllLines("settings.gradle");
assertThat(settingsGradle).isRegularFile();
return Files.readAllLines(settingsGradle);
} }
} }

View File

@ -16,12 +16,7 @@
package io.spring.initializr.generator.spring.code.groovy; package io.spring.initializr.generator.spring.code.groovy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem; import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
@ -36,8 +31,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -70,14 +63,14 @@ class GroovyProjectGenerationConfigurationTests {
} }
@Test @Test
void testClassIsContributed() throws IOException { void testClassIsContributed() {
ProjectStructure projectStructure = this.projectTester ProjectStructure projectStructure = this.projectTester
.generate(new ProjectDescription()); .generate(new ProjectDescription());
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths) assertThat(relativePaths)
.contains("src/test/groovy/com/example/demo/DemoApplicationTests.groovy"); .contains("src/test/groovy/com/example/demo/DemoApplicationTests.groovy");
List<String> lines = readAllLines(projectStructure List<String> lines = projectStructure.readAllLines(
.resolve("src/test/groovy/com/example/demo/DemoApplicationTests.groovy")); "src/test/groovy/com/example/demo/DemoApplicationTests.groovy");
assertThat(lines).containsExactly("package com.example.demo", "", assertThat(lines).containsExactly("package com.example.demo", "",
"import org.junit.Test", "import org.junit.runner.RunWith", "import org.junit.Test", "import org.junit.runner.RunWith",
"import org.springframework.boot.test.context.SpringBootTest", "import org.springframework.boot.test.context.SpringBootTest",
@ -88,8 +81,7 @@ class GroovyProjectGenerationConfigurationTests {
} }
@Test @Test
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() {
throws IOException {
ProjectDescription description = new ProjectDescription(); ProjectDescription description = new ProjectDescription();
description.setPackaging(new WarPackaging()); description.setPackaging(new WarPackaging());
description.setApplicationName("Demo2Application"); description.setApplicationName("Demo2Application");
@ -97,8 +89,8 @@ class GroovyProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths) assertThat(relativePaths)
.contains("src/main/groovy/com/example/demo/ServletInitializer.groovy"); .contains("src/main/groovy/com/example/demo/ServletInitializer.groovy");
List<String> lines = readAllLines(projectStructure List<String> lines = projectStructure.readAllLines(
.resolve("src/main/groovy/com/example/demo/ServletInitializer.groovy")); "src/main/groovy/com/example/demo/ServletInitializer.groovy");
assertThat(lines).containsExactly("package com.example.demo", "", assertThat(lines).containsExactly("package com.example.demo", "",
"import org.springframework.boot.builder.SpringApplicationBuilder", "import org.springframework.boot.builder.SpringApplicationBuilder",
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer", "import org.springframework.boot.web.servlet.support.SpringBootServletInitializer",
@ -108,12 +100,4 @@ class GroovyProjectGenerationConfigurationTests {
" application.sources(Demo2Application)", " }", "", "}"); " application.sources(Demo2Application)", " }", "", "}");
} }
private static List<String> readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
String[] lines = content.split("\\r?\\n");
assertThat(content).endsWith(System.lineSeparator());
return Arrays.asList(lines);
}
} }

View File

@ -16,12 +16,7 @@
package io.spring.initializr.generator.spring.code.java; package io.spring.initializr.generator.spring.code.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem; import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
@ -36,8 +31,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -71,14 +64,14 @@ class JavaProjectGenerationConfigurationTests {
} }
@Test @Test
void testClassIsContributed() throws IOException { void testClassIsContributed() {
ProjectDescription description = new ProjectDescription(); ProjectDescription description = new ProjectDescription();
ProjectStructure projectStructure = this.projectTester.generate(description); ProjectStructure projectStructure = this.projectTester.generate(description);
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths) assertThat(relativePaths)
.contains("src/test/java/com/example/demo/DemoApplicationTests.java"); .contains("src/test/java/com/example/demo/DemoApplicationTests.java");
List<String> lines = readAllLines(projectStructure List<String> lines = projectStructure
.resolve("src/test/java/com/example/demo/DemoApplicationTests.java")); .readAllLines("src/test/java/com/example/demo/DemoApplicationTests.java");
assertThat(lines).containsExactly("package com.example.demo;", "", assertThat(lines).containsExactly("package com.example.demo;", "",
"import org.junit.Test;", "import org.junit.runner.RunWith;", "import org.junit.Test;", "import org.junit.runner.RunWith;",
"import org.springframework.boot.test.context.SpringBootTest;", "import org.springframework.boot.test.context.SpringBootTest;",
@ -89,8 +82,7 @@ class JavaProjectGenerationConfigurationTests {
} }
@Test @Test
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() {
throws IOException {
ProjectDescription description = new ProjectDescription(); ProjectDescription description = new ProjectDescription();
description.setPackaging(new WarPackaging()); description.setPackaging(new WarPackaging());
description.setApplicationName("MyDemoApplication"); description.setApplicationName("MyDemoApplication");
@ -98,8 +90,8 @@ class JavaProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths) assertThat(relativePaths)
.contains("src/main/java/com/example/demo/ServletInitializer.java"); .contains("src/main/java/com/example/demo/ServletInitializer.java");
List<String> lines = readAllLines(projectStructure List<String> lines = projectStructure
.resolve("src/main/java/com/example/demo/ServletInitializer.java")); .readAllLines("src/main/java/com/example/demo/ServletInitializer.java");
assertThat(lines).containsExactly("package com.example.demo;", "", assertThat(lines).containsExactly("package com.example.demo;", "",
"import org.springframework.boot.builder.SpringApplicationBuilder;", "import org.springframework.boot.builder.SpringApplicationBuilder;",
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;", "import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;",
@ -133,12 +125,4 @@ class JavaProjectGenerationConfigurationTests {
"src/test/java/com/example/demo/MyApplicationTests.java"); "src/test/java/com/example/demo/MyApplicationTests.java");
} }
private static List<String> readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
String[] lines = content.split("\\r?\\n");
assertThat(content).endsWith(System.lineSeparator());
return Arrays.asList(lines);
}
} }

View File

@ -16,12 +16,7 @@
package io.spring.initializr.generator.spring.code.kotlin; package io.spring.initializr.generator.spring.code.kotlin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem; import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
@ -36,8 +31,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -73,14 +66,14 @@ class KotlinProjectGenerationConfigurationTests {
} }
@Test @Test
void testClassIsContributed() throws IOException { void testClassIsContributed() {
ProjectStructure projectStructure = this.projectTester ProjectStructure projectStructure = this.projectTester
.generate(new ProjectDescription()); .generate(new ProjectDescription());
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths) assertThat(relativePaths)
.contains("src/test/kotlin/com/example/demo/DemoApplicationTests.kt"); .contains("src/test/kotlin/com/example/demo/DemoApplicationTests.kt");
List<String> lines = readAllLines(projectStructure List<String> lines = projectStructure
.resolve("src/test/kotlin/com/example/demo/DemoApplicationTests.kt")); .readAllLines("src/test/kotlin/com/example/demo/DemoApplicationTests.kt");
assertThat(lines).containsExactly("package com.example.demo", "", assertThat(lines).containsExactly("package com.example.demo", "",
"import org.junit.Test", "import org.junit.runner.RunWith", "import org.junit.Test", "import org.junit.runner.RunWith",
"import org.springframework.boot.test.context.SpringBootTest", "import org.springframework.boot.test.context.SpringBootTest",
@ -91,8 +84,7 @@ class KotlinProjectGenerationConfigurationTests {
} }
@Test @Test
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() {
throws IOException {
ProjectDescription description = new ProjectDescription(); ProjectDescription description = new ProjectDescription();
description.setPackaging(new WarPackaging()); description.setPackaging(new WarPackaging());
description.setApplicationName("KotlinDemoApplication"); description.setApplicationName("KotlinDemoApplication");
@ -100,8 +92,8 @@ class KotlinProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles(); List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths) assertThat(relativePaths)
.contains("src/main/kotlin/com/example/demo/ServletInitializer.kt"); .contains("src/main/kotlin/com/example/demo/ServletInitializer.kt");
List<String> lines = readAllLines(projectStructure List<String> lines = projectStructure
.resolve("src/main/kotlin/com/example/demo/ServletInitializer.kt")); .readAllLines("src/main/kotlin/com/example/demo/ServletInitializer.kt");
assertThat(lines).containsExactly("package com.example.demo", "", assertThat(lines).containsExactly("package com.example.demo", "",
"import org.springframework.boot.builder.SpringApplicationBuilder", "import org.springframework.boot.builder.SpringApplicationBuilder",
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer", "import org.springframework.boot.web.servlet.support.SpringBootServletInitializer",
@ -111,12 +103,4 @@ class KotlinProjectGenerationConfigurationTests {
" }", "", "}"); " }", "", "}");
} }
private static List<String> readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
String[] lines = content.split("\\r?\\n");
assertThat(content).endsWith(System.lineSeparator());
return Arrays.asList(lines);
}
} }

View File

@ -21,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.test.project.ProjectStructure;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
@ -40,11 +41,9 @@ class ApplicationPropertiesContributorTests {
void applicationConfigurationWithDefaultSettings() throws IOException { void applicationConfigurationWithDefaultSettings() throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-"); Path projectDir = Files.createTempDirectory(this.directory, "project-");
new ApplicationPropertiesContributor().contribute(projectDir); new ApplicationPropertiesContributor().contribute(projectDir);
Path configuration = projectDir List<String> lines = new ProjectStructure(projectDir)
.resolve("src/main/resources/application.properties"); .readAllLines("src/main/resources/application.properties");
assertThat(configuration).isRegularFile(); assertThat(lines).isEmpty();
List<String> lines = Files.readAllLines(configuration);
assertThat(lines).containsOnly("");
} }
} }

View File

@ -22,6 +22,7 @@ import java.nio.file.Path;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer; import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
import io.spring.initializr.generator.test.project.ProjectStructure;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
@ -64,7 +65,7 @@ class HelpDocumentProjectContributorTests {
assertThat(lines).containsExactly("# Getting Started", "", "### Guides", assertThat(lines).containsExactly("# Getting Started", "", "### Guides",
"The following guides illustrate how to use some features concretely:", "The following guides illustrate how to use some features concretely:",
"", "* [test](https://test.example.com)", "", "* [test](https://test.example.com)",
"* [test2](https://test2.example.com)", ""); "* [test2](https://test2.example.com)");
} }
@Test @Test
@ -77,7 +78,7 @@ class HelpDocumentProjectContributorTests {
"### Reference Documentation", "### Reference Documentation",
"For further reference, please consider the following sections:", "", "For further reference, please consider the following sections:", "",
"* [doc](https://test.example.com)", "* [doc](https://test.example.com)",
"* [doc2](https://test2.example.com)", ""); "* [doc2](https://test2.example.com)");
} }
@Test @Test
@ -88,7 +89,7 @@ class HelpDocumentProjectContributorTests {
List<String> lines = generateDocument(document); List<String> lines = generateDocument(document);
assertThat(lines).containsExactly("# Getting Started", "", "### Additional Links", assertThat(lines).containsExactly("# Getting Started", "", "### Additional Links",
"These additional references should also help you:", "", "These additional references should also help you:", "",
"* [Something](https://test.example.com)", ""); "* [Something](https://test.example.com)");
} }
@Test @Test
@ -116,9 +117,7 @@ class HelpDocumentProjectContributorTests {
private List<String> generateDocument(HelpDocument document) throws IOException { private List<String> generateDocument(HelpDocument document) throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-"); Path projectDir = Files.createTempDirectory(this.directory, "project-");
new HelpDocumentProjectContributor(document).contribute(projectDir); new HelpDocumentProjectContributor(document).contribute(projectDir);
Path helpDocument = projectDir.resolve("HELP.md"); return new ProjectStructure(projectDir).readAllLines("HELP.md");
assertThat(helpDocument).isRegularFile();
return Files.readAllLines(helpDocument);
} }
} }

View File

@ -16,25 +16,20 @@
package io.spring.initializr.generator.language.groovy; package io.spring.initializr.generator.language.groovy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.io.IndentingWriterFactory; import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.language.Annotation; import io.spring.initializr.generator.language.Annotation;
import io.spring.initializr.generator.language.Parameter; import io.spring.initializr.generator.language.Parameter;
import io.spring.initializr.generator.test.io.PathTestUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -218,8 +213,7 @@ class GroovySourceCodeWriterTests {
private List<String> writeSingleType(GroovySourceCode sourceCode, String location) private List<String> writeSingleType(GroovySourceCode sourceCode, String location)
throws IOException { throws IOException {
Path source = writeSourceCode(sourceCode).resolve(location); Path source = writeSourceCode(sourceCode).resolve(location);
assertThat(source).isRegularFile(); return PathTestUtils.readAllLines(source);
return readAllLines(source);
} }
private Path writeSourceCode(GroovySourceCode sourceCode) throws IOException { private Path writeSourceCode(GroovySourceCode sourceCode) throws IOException {
@ -228,12 +222,4 @@ class GroovySourceCodeWriterTests {
return projectDirectory; return projectDirectory;
} }
private static List<String> readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
String[] lines = content.split("\\r?\\n");
assertThat(content).endsWith(System.lineSeparator());
return Arrays.asList(lines);
}
} }

View File

@ -16,25 +16,20 @@
package io.spring.initializr.generator.language.java; package io.spring.initializr.generator.language.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.io.IndentingWriterFactory; import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.language.Annotation; import io.spring.initializr.generator.language.Annotation;
import io.spring.initializr.generator.language.Parameter; import io.spring.initializr.generator.language.Parameter;
import io.spring.initializr.generator.test.io.PathTestUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -220,8 +215,7 @@ class JavaSourceCodeWriterTests {
private List<String> writeSingleType(JavaSourceCode sourceCode, String location) private List<String> writeSingleType(JavaSourceCode sourceCode, String location)
throws IOException { throws IOException {
Path source = writeSourceCode(sourceCode).resolve(location); Path source = writeSourceCode(sourceCode).resolve(location);
assertThat(source).isRegularFile(); return PathTestUtils.readAllLines(source);
return readAllLines(source);
} }
private Path writeSourceCode(JavaSourceCode sourceCode) throws IOException { private Path writeSourceCode(JavaSourceCode sourceCode) throws IOException {
@ -230,12 +224,4 @@ class JavaSourceCodeWriterTests {
return projectDirectory; return projectDirectory;
} }
private static List<String> readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
assertThat(content).endsWith(System.lineSeparator());
String[] lines = content.split("\\r?\\n");
return Arrays.asList(lines);
}
} }

View File

@ -16,24 +16,19 @@
package io.spring.initializr.generator.language.kotlin; package io.spring.initializr.generator.language.kotlin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.io.IndentingWriterFactory; import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.language.Annotation; import io.spring.initializr.generator.language.Annotation;
import io.spring.initializr.generator.language.Parameter; import io.spring.initializr.generator.language.Parameter;
import io.spring.initializr.generator.test.io.PathTestUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -230,8 +225,7 @@ class KotlinSourceCodeWriterTests {
private List<String> writeSingleType(KotlinSourceCode sourceCode, String location) private List<String> writeSingleType(KotlinSourceCode sourceCode, String location)
throws IOException { throws IOException {
Path source = writeSourceCode(sourceCode).resolve(location); Path source = writeSourceCode(sourceCode).resolve(location);
assertThat(source).isRegularFile(); return PathTestUtils.readAllLines(source);
return readAllLines(source);
} }
private Path writeSourceCode(KotlinSourceCode sourceCode) throws IOException { private Path writeSourceCode(KotlinSourceCode sourceCode) throws IOException {
@ -240,12 +234,4 @@ class KotlinSourceCodeWriterTests {
return projectDirectory; return projectDirectory;
} }
private static List<String> readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);
assertThat(content).endsWith(System.lineSeparator());
String[] lines = content.split("\\r?\\n");
return Arrays.asList(lines);
}
} }

View File

@ -0,0 +1,65 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.test.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@link Path}-related test utilities.
*
* @author Stephane Nicoll
*/
public final class PathTestUtils {
private PathTestUtils() {
}
/**
* Read all lines from the specified {@link Path source}. Check the given
* {@code source} is a regular file that ends with a new line.
* @param source a text file
* @return all lines from the file
*/
public static List<String> readAllLines(Path source) {
assertThat(source).isRegularFile();
try {
BufferedReader reader = Files.newBufferedReader(source,
StandardCharsets.UTF_8);
StringWriter writer = new StringWriter();
FileCopyUtils.copy(reader, writer);
String content = writer.toString();
String[] lines = content.split("\\r?\\n");
assertThat(content).endsWith(System.lineSeparator());
return Arrays.asList(lines);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}

View File

@ -25,6 +25,8 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import io.spring.initializr.generator.test.io.PathTestUtils;
/** /**
* Test helper to assert content of a generated project structure. * Test helper to assert content of a generated project structure.
* *
@ -51,7 +53,7 @@ public class ProjectStructure {
} }
/** /**
* Resolve a {@link Path} relative to the project directory * Resolve a {@link Path} relative to the project directory.
* @param other the path string to resolve against the root of the project structure * @param other the path string to resolve against the root of the project structure
* @return the resulting path * @return the resulting path
* @see Path#resolve(String) * @see Path#resolve(String)
@ -60,6 +62,18 @@ public class ProjectStructure {
return this.projectDirectory.resolve(other); return this.projectDirectory.resolve(other);
} }
/**
* Resolve a {@link Path} relative to the project directory and return all lines.
* Check that the resolved {@link Path} is a regular text file that ends with a
* newline.
* @param other the path string to resolve against the root of the project structure
* @return all lines from the resolve file
* @see PathTestUtils#readAllLines(Path)
*/
public List<String> readAllLines(String other) {
return PathTestUtils.readAllLines(resolve(other));
}
/** /**
* Return the relative paths of all files. * Return the relative paths of all files.
* @return the relative path of all files * @return the relative path of all files