mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +08:00
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:
@@ -16,10 +16,7 @@
|
||||
|
||||
package io.spring.initializr.generator.spring.build.gradle;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
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.MethodSource;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -115,7 +110,7 @@ class GradleProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildDotGradleIsContributedWhenGeneratingGradleProject() throws IOException {
|
||||
void buildDotGradleIsContributedWhenGeneratingGradleProject() {
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
|
||||
description.setLanguage(new JavaLanguage("11"));
|
||||
@@ -124,8 +119,7 @@ class GradleProjectGenerationConfigurationTests {
|
||||
ProjectStructure projectStructure = this.projectTester.generate(description);
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths).contains("build.gradle");
|
||||
Path path = projectStructure.resolve("build.gradle");
|
||||
String[] lines = readAllLines(path);
|
||||
List<String> lines = projectStructure.readAllLines("build.gradle");
|
||||
assertThat(lines).containsExactly("plugins {",
|
||||
" id 'org.springframework.boot' version '2.1.0.RELEASE'",
|
||||
" id 'java'", "}", "",
|
||||
@@ -173,12 +167,4 @@ class GradleProjectGenerationConfigurationTests {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||
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.io.TempDir;
|
||||
|
||||
@@ -78,9 +79,7 @@ class SettingsGradleProjectContributorTests {
|
||||
Path projectDir = Files.createTempDirectory(this.directory, "project-");
|
||||
new SettingsGradleProjectContributor(build, indentingWriterFactory)
|
||||
.contribute(projectDir);
|
||||
Path settingsGradle = projectDir.resolve("settings.gradle");
|
||||
assertThat(settingsGradle).isRegularFile();
|
||||
return Files.readAllLines(settingsGradle);
|
||||
return new ProjectStructure(projectDir).readAllLines("settings.gradle");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,12 +16,7 @@
|
||||
|
||||
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.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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.io.TempDir;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -70,14 +63,14 @@ class GroovyProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClassIsContributed() throws IOException {
|
||||
void testClassIsContributed() {
|
||||
ProjectStructure projectStructure = this.projectTester
|
||||
.generate(new ProjectDescription());
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths)
|
||||
.contains("src/test/groovy/com/example/demo/DemoApplicationTests.groovy");
|
||||
List<String> lines = readAllLines(projectStructure
|
||||
.resolve("src/test/groovy/com/example/demo/DemoApplicationTests.groovy"));
|
||||
List<String> lines = projectStructure.readAllLines(
|
||||
"src/test/groovy/com/example/demo/DemoApplicationTests.groovy");
|
||||
assertThat(lines).containsExactly("package com.example.demo", "",
|
||||
"import org.junit.Test", "import org.junit.runner.RunWith",
|
||||
"import org.springframework.boot.test.context.SpringBootTest",
|
||||
@@ -88,8 +81,7 @@ class GroovyProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
|
||||
throws IOException {
|
||||
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() {
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
description.setPackaging(new WarPackaging());
|
||||
description.setApplicationName("Demo2Application");
|
||||
@@ -97,8 +89,8 @@ class GroovyProjectGenerationConfigurationTests {
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths)
|
||||
.contains("src/main/groovy/com/example/demo/ServletInitializer.groovy");
|
||||
List<String> lines = readAllLines(projectStructure
|
||||
.resolve("src/main/groovy/com/example/demo/ServletInitializer.groovy"));
|
||||
List<String> lines = projectStructure.readAllLines(
|
||||
"src/main/groovy/com/example/demo/ServletInitializer.groovy");
|
||||
assertThat(lines).containsExactly("package com.example.demo", "",
|
||||
"import org.springframework.boot.builder.SpringApplicationBuilder",
|
||||
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer",
|
||||
@@ -108,12 +100,4 @@ class GroovyProjectGenerationConfigurationTests {
|
||||
" 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,12 +16,7 @@
|
||||
|
||||
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.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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.io.TempDir;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -71,14 +64,14 @@ class JavaProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClassIsContributed() throws IOException {
|
||||
void testClassIsContributed() {
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
ProjectStructure projectStructure = this.projectTester.generate(description);
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths)
|
||||
.contains("src/test/java/com/example/demo/DemoApplicationTests.java");
|
||||
List<String> lines = readAllLines(projectStructure
|
||||
.resolve("src/test/java/com/example/demo/DemoApplicationTests.java"));
|
||||
List<String> lines = projectStructure
|
||||
.readAllLines("src/test/java/com/example/demo/DemoApplicationTests.java");
|
||||
assertThat(lines).containsExactly("package com.example.demo;", "",
|
||||
"import org.junit.Test;", "import org.junit.runner.RunWith;",
|
||||
"import org.springframework.boot.test.context.SpringBootTest;",
|
||||
@@ -89,8 +82,7 @@ class JavaProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
|
||||
throws IOException {
|
||||
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() {
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
description.setPackaging(new WarPackaging());
|
||||
description.setApplicationName("MyDemoApplication");
|
||||
@@ -98,8 +90,8 @@ class JavaProjectGenerationConfigurationTests {
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths)
|
||||
.contains("src/main/java/com/example/demo/ServletInitializer.java");
|
||||
List<String> lines = readAllLines(projectStructure
|
||||
.resolve("src/main/java/com/example/demo/ServletInitializer.java"));
|
||||
List<String> lines = projectStructure
|
||||
.readAllLines("src/main/java/com/example/demo/ServletInitializer.java");
|
||||
assertThat(lines).containsExactly("package com.example.demo;", "",
|
||||
"import org.springframework.boot.builder.SpringApplicationBuilder;",
|
||||
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;",
|
||||
@@ -133,12 +125,4 @@ class JavaProjectGenerationConfigurationTests {
|
||||
"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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,12 +16,7 @@
|
||||
|
||||
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.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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.io.TempDir;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -73,14 +66,14 @@ class KotlinProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClassIsContributed() throws IOException {
|
||||
void testClassIsContributed() {
|
||||
ProjectStructure projectStructure = this.projectTester
|
||||
.generate(new ProjectDescription());
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths)
|
||||
.contains("src/test/kotlin/com/example/demo/DemoApplicationTests.kt");
|
||||
List<String> lines = readAllLines(projectStructure
|
||||
.resolve("src/test/kotlin/com/example/demo/DemoApplicationTests.kt"));
|
||||
List<String> lines = projectStructure
|
||||
.readAllLines("src/test/kotlin/com/example/demo/DemoApplicationTests.kt");
|
||||
assertThat(lines).containsExactly("package com.example.demo", "",
|
||||
"import org.junit.Test", "import org.junit.runner.RunWith",
|
||||
"import org.springframework.boot.test.context.SpringBootTest",
|
||||
@@ -91,8 +84,7 @@ class KotlinProjectGenerationConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
|
||||
throws IOException {
|
||||
void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging() {
|
||||
ProjectDescription description = new ProjectDescription();
|
||||
description.setPackaging(new WarPackaging());
|
||||
description.setApplicationName("KotlinDemoApplication");
|
||||
@@ -100,8 +92,8 @@ class KotlinProjectGenerationConfigurationTests {
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths)
|
||||
.contains("src/main/kotlin/com/example/demo/ServletInitializer.kt");
|
||||
List<String> lines = readAllLines(projectStructure
|
||||
.resolve("src/main/kotlin/com/example/demo/ServletInitializer.kt"));
|
||||
List<String> lines = projectStructure
|
||||
.readAllLines("src/main/kotlin/com/example/demo/ServletInitializer.kt");
|
||||
assertThat(lines).containsExactly("package com.example.demo", "",
|
||||
"import org.springframework.boot.builder.SpringApplicationBuilder",
|
||||
"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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.test.project.ProjectStructure;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
@@ -40,11 +41,9 @@ class ApplicationPropertiesContributorTests {
|
||||
void applicationConfigurationWithDefaultSettings() throws IOException {
|
||||
Path projectDir = Files.createTempDirectory(this.directory, "project-");
|
||||
new ApplicationPropertiesContributor().contribute(projectDir);
|
||||
Path configuration = projectDir
|
||||
.resolve("src/main/resources/application.properties");
|
||||
assertThat(configuration).isRegularFile();
|
||||
List<String> lines = Files.readAllLines(configuration);
|
||||
assertThat(lines).containsOnly("");
|
||||
List<String> lines = new ProjectStructure(projectDir)
|
||||
.readAllLines("src/main/resources/application.properties");
|
||||
assertThat(lines).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
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.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -64,7 +65,7 @@ class HelpDocumentProjectContributorTests {
|
||||
assertThat(lines).containsExactly("# Getting Started", "", "### Guides",
|
||||
"The following guides illustrate how to use some features concretely:",
|
||||
"", "* [test](https://test.example.com)",
|
||||
"* [test2](https://test2.example.com)", "");
|
||||
"* [test2](https://test2.example.com)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -77,7 +78,7 @@ class HelpDocumentProjectContributorTests {
|
||||
"### Reference Documentation",
|
||||
"For further reference, please consider the following sections:", "",
|
||||
"* [doc](https://test.example.com)",
|
||||
"* [doc2](https://test2.example.com)", "");
|
||||
"* [doc2](https://test2.example.com)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,7 +89,7 @@ class HelpDocumentProjectContributorTests {
|
||||
List<String> lines = generateDocument(document);
|
||||
assertThat(lines).containsExactly("# Getting Started", "", "### Additional Links",
|
||||
"These additional references should also help you:", "",
|
||||
"* [Something](https://test.example.com)", "");
|
||||
"* [Something](https://test.example.com)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,9 +117,7 @@ class HelpDocumentProjectContributorTests {
|
||||
private List<String> generateDocument(HelpDocument document) throws IOException {
|
||||
Path projectDir = Files.createTempDirectory(this.directory, "project-");
|
||||
new HelpDocumentProjectContributor(document).contribute(projectDir);
|
||||
Path helpDocument = projectDir.resolve("HELP.md");
|
||||
assertThat(helpDocument).isRegularFile();
|
||||
return Files.readAllLines(helpDocument);
|
||||
return new ProjectStructure(projectDir).readAllLines("HELP.md");
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user