Generated code should have only one newline at the end

Fixes gh-828
This commit is contained in:
Madhura Bhave
2019-02-15 15:13:20 -08:00
parent 56187f571d
commit 11ffe794fa
36 changed files with 139 additions and 91 deletions

View File

@@ -16,9 +16,12 @@
package io.spring.initializr.generator.spring.code.groovy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
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;
@@ -33,6 +36,8 @@ 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,7 +76,7 @@ class GroovyProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths)
.contains("src/test/groovy/com/example/demo/DemoApplicationTests.groovy");
List<String> lines = Files.readAllLines(projectStructure
List<String> lines = readAllLines(projectStructure
.resolve("src/test/groovy/com/example/demo/DemoApplicationTests.groovy"));
assertThat(lines).containsExactly("package com.example.demo", "",
"import org.junit.Test", "import org.junit.runner.RunWith",
@@ -79,7 +84,7 @@ class GroovyProjectGenerationConfigurationTests {
"import org.springframework.test.context.junit4.SpringRunner", "",
"@RunWith(SpringRunner)", "@SpringBootTest",
"class DemoApplicationTests {", "", " @Test",
" void contextLoads() {", " }", "", "}", "");
" void contextLoads() {", " }", "", "}");
}
@Test
@@ -92,7 +97,7 @@ class GroovyProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths)
.contains("src/main/groovy/com/example/demo/ServletInitializer.groovy");
List<String> lines = Files.readAllLines(projectStructure
List<String> lines = readAllLines(projectStructure
.resolve("src/main/groovy/com/example/demo/ServletInitializer.groovy"));
assertThat(lines).containsExactly("package com.example.demo", "",
"import org.springframework.boot.builder.SpringApplicationBuilder",
@@ -100,7 +105,15 @@ class GroovyProjectGenerationConfigurationTests {
"", "class ServletInitializer extends SpringBootServletInitializer {", "",
" @Override",
" protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {",
" 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,9 +16,12 @@
package io.spring.initializr.generator.spring.code.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
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;
@@ -33,6 +36,8 @@ 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;
/**
@@ -72,7 +77,7 @@ class JavaProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths)
.contains("src/test/java/com/example/demo/DemoApplicationTests.java");
List<String> lines = Files.readAllLines(projectStructure
List<String> lines = readAllLines(projectStructure
.resolve("src/test/java/com/example/demo/DemoApplicationTests.java"));
assertThat(lines).containsExactly("package com.example.demo;", "",
"import org.junit.Test;", "import org.junit.runner.RunWith;",
@@ -80,7 +85,7 @@ class JavaProjectGenerationConfigurationTests {
"import org.springframework.test.context.junit4.SpringRunner;", "",
"@RunWith(SpringRunner.class)", "@SpringBootTest",
"public class DemoApplicationTests {", "", " @Test",
" public void contextLoads() {", " }", "", "}", "");
" public void contextLoads() {", " }", "", "}");
}
@Test
@@ -93,7 +98,7 @@ class JavaProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths)
.contains("src/main/java/com/example/demo/ServletInitializer.java");
List<String> lines = Files.readAllLines(projectStructure
List<String> lines = readAllLines(projectStructure
.resolve("src/main/java/com/example/demo/ServletInitializer.java"));
assertThat(lines).containsExactly("package com.example.demo;", "",
"import org.springframework.boot.builder.SpringApplicationBuilder;",
@@ -103,7 +108,7 @@ class JavaProjectGenerationConfigurationTests {
"", " @Override",
" protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {",
" return application.sources(MyDemoApplication.class);", " }",
"", "}", "");
"", "}");
}
@Test
@@ -128,4 +133,12 @@ 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);
}
}

View File

@@ -16,9 +16,12 @@
package io.spring.initializr.generator.spring.code.kotlin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
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;
@@ -33,6 +36,8 @@ 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;
/**
@@ -74,7 +79,7 @@ class KotlinProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths)
.contains("src/test/kotlin/com/example/demo/DemoApplicationTests.kt");
List<String> lines = Files.readAllLines(projectStructure
List<String> lines = readAllLines(projectStructure
.resolve("src/test/kotlin/com/example/demo/DemoApplicationTests.kt"));
assertThat(lines).containsExactly("package com.example.demo", "",
"import org.junit.Test", "import org.junit.runner.RunWith",
@@ -82,7 +87,7 @@ class KotlinProjectGenerationConfigurationTests {
"import org.springframework.test.context.junit4.SpringRunner", "",
"@RunWith(SpringRunner::class)", "@SpringBootTest",
"class DemoApplicationTests {", "", " @Test",
" fun contextLoads() {", " }", "", "}", "");
" fun contextLoads() {", " }", "", "}");
}
@Test
@@ -95,7 +100,7 @@ class KotlinProjectGenerationConfigurationTests {
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
assertThat(relativePaths)
.contains("src/main/kotlin/com/example/demo/ServletInitializer.kt");
List<String> lines = Files.readAllLines(projectStructure
List<String> lines = readAllLines(projectStructure
.resolve("src/main/kotlin/com/example/demo/ServletInitializer.kt"));
assertThat(lines).containsExactly("package com.example.demo", "",
"import org.springframework.boot.builder.SpringApplicationBuilder",
@@ -103,7 +108,15 @@ class KotlinProjectGenerationConfigurationTests {
"", "class ServletInitializer : SpringBootServletInitializer() {", "",
" override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder {",
" return application.sources(KotlinDemoApplication::class.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

@@ -11,4 +11,3 @@ class ServletInitializer extends SpringBootServletInitializer {
}
}

View File

@@ -11,4 +11,3 @@ class ServletInitializer extends SpringBootServletInitializer {
}
}

View File

@@ -11,4 +11,3 @@ public class DemoApplication {
}
}

View File

@@ -11,4 +11,3 @@ public class MyProjectApplication {
}
}

View File

@@ -11,4 +11,3 @@ public class ServletInitializer extends SpringBootServletInitializer {
}
}

View File

@@ -11,4 +11,3 @@ public class DemoApplication {
}
}

View File

@@ -14,4 +14,3 @@ public class DemoApplicationTests {
}
}

View File

@@ -14,4 +14,3 @@ public class DemoApplicationTests {
}
}

View File

@@ -11,4 +11,3 @@ public class MyProjectApplication {
}
}

View File

@@ -14,4 +14,3 @@ public class MyProjectApplicationTests {
}
}

View File

@@ -11,4 +11,3 @@ public class ServletInitializer extends SpringBootServletInitializer {
}
}

View File

@@ -9,4 +9,3 @@ class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}

View File

@@ -9,4 +9,3 @@ class MyProjectApplication
fun main(args: Array<String>) {
SpringApplication.run(MyProjectApplication::class.java, *args)
}

View File

@@ -10,4 +10,3 @@ class ServletInitializer : SpringBootServletInitializer() {
}
}

View File

@@ -9,4 +9,3 @@ class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}

View File

@@ -14,4 +14,3 @@ class DemoApplicationTests {
}
}

View File

@@ -9,4 +9,3 @@ class MyProjectApplication
fun main(args: Array<String>) {
runApplication<MyProjectApplication>(*args)
}

View File

@@ -14,4 +14,3 @@ class MyProjectApplicationTests {
}
}

View File

@@ -10,4 +10,3 @@ class ServletInitializer : SpringBootServletInitializer() {
}
}

View File

@@ -113,7 +113,6 @@ public class GroovySourceCodeWriter implements SourceCodeWriter<GroovySourceCode
});
}
writer.println("}");
writer.println("");
}
}
}

View File

@@ -114,7 +114,6 @@ public class JavaSourceCodeWriter implements SourceCodeWriter<JavaSourceCode> {
});
}
writer.println("}");
writer.println("");
}
}
}

View File

@@ -86,18 +86,17 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
.getFunctionDeclarations();
if (!functionDeclarations.isEmpty()) {
writer.println(" {");
writer.println();
writer.indented(() -> {
for (KotlinFunctionDeclaration functionDeclaration : functionDeclarations) {
writeFunction(writer, functionDeclaration);
}
});
writer.println();
writer.println("}");
}
else {
writer.println("");
}
writer.println("");
}
List<KotlinFunctionDeclaration> topLevelFunctions = compilationUnit
.getTopLevelFunctions();
@@ -112,6 +111,7 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
private void writeFunction(IndentingWriter writer,
KotlinFunctionDeclaration functionDeclaration) {
writer.println();
writeAnnotations(writer, functionDeclaration);
writeMethodModifiers(writer, functionDeclaration);
writer.print("fun ");
@@ -144,7 +144,6 @@ public class KotlinSourceCodeWriter implements SourceCodeWriter<KotlinSourceCode
}
});
writer.println("}");
writer.println();
}
private void writeAnnotations(IndentingWriter writer, Annotatable annotatable) {

View File

@@ -16,11 +16,15 @@
package io.spring.initializr.generator.language.groovy;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import io.spring.initializr.generator.io.IndentingWriterFactory;
@@ -29,6 +33,8 @@ import io.spring.initializr.generator.language.Parameter;
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;
/**
@@ -49,7 +55,7 @@ class GroovySourceCodeWriterTests {
GroovySourceCode sourceCode = new GroovySourceCode();
sourceCode.createCompilationUnit("com.example", "Test");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
assertThat(lines).containsExactly("package com.example", "");
assertThat(lines).containsExactly("package com.example");
}
@Test
@@ -60,7 +66,7 @@ class GroovySourceCodeWriterTests {
compilationUnit.createTypeDeclaration("Test");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
"}", "");
"}");
}
@Test
@@ -73,7 +79,7 @@ class GroovySourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
assertThat(lines).containsExactly("package com.example", "",
"import com.example.build.TestParent", "",
"class Test extends TestParent {", "", "}", "");
"class Test extends TestParent {", "", "}");
}
@Test
@@ -90,7 +96,7 @@ class GroovySourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
" String trim(String value) {", " value.trim()", " }", "",
"}", "");
"}");
}
@Test
@@ -113,7 +119,7 @@ class GroovySourceCodeWriterTests {
"import org.springframework.boot.autoconfigure.SpringBootApplication", "",
"@SpringBootApplication", "class Test {", "",
" static void main(String[] args) {",
" SpringApplication.run(Test, args)", " }", "", "}", "");
" SpringApplication.run(Test, args)", " }", "", "}");
}
@Test
@@ -123,7 +129,7 @@ class GroovySourceCodeWriterTests {
(builder) -> builder.attribute("counter", Integer.class, "42")));
assertThat(lines).containsExactly("package com.example", "",
"import org.springframework.test.TestApplication", "",
"@TestApplication(counter = 42)", "class Test {", "", "}", "");
"@TestApplication(counter = 42)", "class Test {", "", "}");
}
@Test
@@ -133,7 +139,7 @@ class GroovySourceCodeWriterTests {
(builder) -> builder.attribute("name", String.class, "test")));
assertThat(lines).containsExactly("package com.example", "",
"import org.springframework.test.TestApplication", "",
"@TestApplication(name = \"test\")", "class Test {", "", "}", "");
"@TestApplication(name = \"test\")", "class Test {", "", "}");
}
@Test
@@ -143,7 +149,7 @@ class GroovySourceCodeWriterTests {
(builder) -> builder.attribute("value", String.class, "test")));
assertThat(lines).containsExactly("package com.example", "",
"import org.springframework.test.TestApplication", "",
"@TestApplication(\"test\")", "class Test {", "", "}", "");
"@TestApplication(\"test\")", "class Test {", "", "}");
}
@Test
@@ -155,8 +161,7 @@ class GroovySourceCodeWriterTests {
assertThat(lines).containsExactly("package com.example", "",
"import java.time.temporal.ChronoUnit",
"import org.springframework.test.TestApplication", "",
"@TestApplication(unit = ChronoUnit.SECONDS)", "class Test {", "", "}",
"");
"@TestApplication(unit = ChronoUnit.SECONDS)", "class Test {", "", "}");
}
@Test
@@ -168,7 +173,7 @@ class GroovySourceCodeWriterTests {
assertThat(lines).containsExactly("package com.example", "",
"import com.example.One", "import com.example.Two",
"import org.springframework.test.TestApplication", "",
"@TestApplication(target = { One, Two })", "class Test {", "", "}", "");
"@TestApplication(target = { One, Two })", "class Test {", "", "}");
}
@Test
@@ -182,7 +187,7 @@ class GroovySourceCodeWriterTests {
"import com.example.One", "import java.time.temporal.ChronoUnit",
"import org.springframework.test.TestApplication", "",
"@TestApplication(target = One, unit = ChronoUnit.NANOS)", "class Test {",
"", "}", "");
"", "}");
}
private List<String> writeClassAnnotation(Annotation annotation) throws IOException {
@@ -207,14 +212,14 @@ class GroovySourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.groovy");
assertThat(lines).containsExactly("package com.example", "",
"import com.example.test.TestAnnotation", "", "class Test {", "",
" @TestAnnotation", " void something() {", " }", "", "}", "");
" @TestAnnotation", " void something() {", " }", "", "}");
}
private List<String> writeSingleType(GroovySourceCode sourceCode, String location)
throws IOException {
Path source = writeSourceCode(sourceCode).resolve(location);
assertThat(source).isRegularFile();
return Files.readAllLines(source);
return readAllLines(source);
}
private Path writeSourceCode(GroovySourceCode sourceCode) throws IOException {
@@ -223,4 +228,12 @@ class GroovySourceCodeWriterTests {
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,11 +16,15 @@
package io.spring.initializr.generator.language.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import io.spring.initializr.generator.io.IndentingWriterFactory;
@@ -29,6 +33,8 @@ import io.spring.initializr.generator.language.Parameter;
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;
/**
@@ -49,7 +55,7 @@ class JavaSourceCodeWriterTests {
JavaSourceCode sourceCode = new JavaSourceCode();
sourceCode.createCompilationUnit("com.example", "Test");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
assertThat(lines).containsExactly("package com.example;", "");
assertThat(lines).containsExactly("package com.example;");
}
@Test
@@ -60,7 +66,7 @@ class JavaSourceCodeWriterTests {
compilationUnit.createTypeDeclaration("Test");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
assertThat(lines).containsExactly("package com.example;", "",
"public class Test {", "", "}", "");
"public class Test {", "", "}");
}
@Test
@@ -73,7 +79,7 @@ class JavaSourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
assertThat(lines).containsExactly("package com.example;", "",
"import com.example.build.TestParent;", "",
"public class Test extends TestParent {", "", "}", "");
"public class Test extends TestParent {", "", "}");
}
@Test
@@ -90,7 +96,7 @@ class JavaSourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
assertThat(lines).containsExactly("package com.example;", "",
"public class Test {", "", " public String trim(String value) {",
" return value.trim();", " }", "", "}", "");
" return value.trim();", " }", "", "}");
}
@Test
@@ -113,7 +119,7 @@ class JavaSourceCodeWriterTests {
"import org.springframework.boot.autoconfigure.SpringBootApplication;",
"", "@SpringBootApplication", "public class Test {", "",
" public static void main(String[] args) {",
" SpringApplication.run(Test.class, args);", " }", "", "}", "");
" SpringApplication.run(Test.class, args);", " }", "", "}");
}
@Test
@@ -123,7 +129,7 @@ class JavaSourceCodeWriterTests {
(builder) -> builder.attribute("counter", Integer.class, "42")));
assertThat(lines).containsExactly("package com.example;", "",
"import org.springframework.test.TestApplication;", "",
"@TestApplication(counter = 42)", "public class Test {", "", "}", "");
"@TestApplication(counter = 42)", "public class Test {", "", "}");
}
@Test
@@ -133,7 +139,7 @@ class JavaSourceCodeWriterTests {
(builder) -> builder.attribute("name", String.class, "test")));
assertThat(lines).containsExactly("package com.example;", "",
"import org.springframework.test.TestApplication;", "",
"@TestApplication(name = \"test\")", "public class Test {", "", "}", "");
"@TestApplication(name = \"test\")", "public class Test {", "", "}");
}
@Test
@@ -143,7 +149,7 @@ class JavaSourceCodeWriterTests {
(builder) -> builder.attribute("value", String.class, "test")));
assertThat(lines).containsExactly("package com.example;", "",
"import org.springframework.test.TestApplication;", "",
"@TestApplication(\"test\")", "public class Test {", "", "}", "");
"@TestApplication(\"test\")", "public class Test {", "", "}");
}
@Test
@@ -156,7 +162,7 @@ class JavaSourceCodeWriterTests {
"import java.time.temporal.ChronoUnit;",
"import org.springframework.test.TestApplication;", "",
"@TestApplication(unit = ChronoUnit.SECONDS)", "public class Test {", "",
"}", "");
"}");
}
@Test
@@ -169,7 +175,7 @@ class JavaSourceCodeWriterTests {
"import com.example.One;", "import com.example.Two;",
"import org.springframework.test.TestApplication;", "",
"@TestApplication(target = { One.class, Two.class })",
"public class Test {", "", "}", "");
"public class Test {", "", "}");
}
@Test
@@ -183,7 +189,7 @@ class JavaSourceCodeWriterTests {
"import com.example.One;", "import java.time.temporal.ChronoUnit;",
"import org.springframework.test.TestApplication;", "",
"@TestApplication(target = One.class, unit = ChronoUnit.NANOS)",
"public class Test {", "", "}", "");
"public class Test {", "", "}");
}
private List<String> writeClassAnnotation(Annotation annotation) throws IOException {
@@ -208,15 +214,14 @@ class JavaSourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.java");
assertThat(lines).containsExactly("package com.example;", "",
"import com.example.test.TestAnnotation;", "", "public class Test {", "",
" @TestAnnotation", " public void something() {", " }", "", "}",
"");
" @TestAnnotation", " public void something() {", " }", "", "}");
}
private List<String> writeSingleType(JavaSourceCode sourceCode, String location)
throws IOException {
Path source = writeSourceCode(sourceCode).resolve(location);
assertThat(source).isRegularFile();
return Files.readAllLines(source);
return readAllLines(source);
}
private Path writeSourceCode(JavaSourceCode sourceCode) throws IOException {
@@ -225,4 +230,12 @@ class JavaSourceCodeWriterTests {
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,10 +16,14 @@
package io.spring.initializr.generator.language.kotlin;
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.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import io.spring.initializr.generator.io.IndentingWriterFactory;
@@ -28,6 +32,8 @@ import io.spring.initializr.generator.language.Parameter;
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;
/**
@@ -48,7 +54,7 @@ class KotlinSourceCodeWriterTests {
KotlinSourceCode sourceCode = new KotlinSourceCode();
sourceCode.createCompilationUnit("com.example", "Test");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
assertThat(lines).containsExactly("package com.example", "");
assertThat(lines).containsExactly("package com.example");
}
@Test
@@ -58,7 +64,7 @@ class KotlinSourceCodeWriterTests {
.createCompilationUnit("com.example", "Test");
compilationUnit.createTypeDeclaration("Test");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
assertThat(lines).containsExactly("package com.example", "", "class Test", "");
assertThat(lines).containsExactly("package com.example", "", "class Test");
}
@Test
@@ -70,8 +76,7 @@ class KotlinSourceCodeWriterTests {
test.extend("com.example.build.TestParent");
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
assertThat(lines).containsExactly("package com.example", "",
"import com.example.build.TestParent", "", "class Test : TestParent()",
"");
"import com.example.build.TestParent", "", "class Test : TestParent()");
}
@Test
@@ -88,7 +93,7 @@ class KotlinSourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
" fun reverse(echo: String): String {",
" return echo.reversed()", " }", "", "}", "");
" return echo.reversed()", " }", "", "}");
}
@Test
@@ -105,7 +110,7 @@ class KotlinSourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
assertThat(lines).containsExactly("package com.example", "", "class Test {", "",
" open override fun toString(): String {",
" return super.toString()", " }", "", "}", "");
" return super.toString()", " }", "", "}");
}
@Test
@@ -125,8 +130,8 @@ class KotlinSourceCodeWriterTests {
"import org.springframework.boot.autoconfigure.SpringBootApplication",
"import org.springframework.boot.runApplication", "",
"@SpringBootApplication", "class Test", "",
"fun main(args: Array<String>) {", " runApplication<Test>(*args)", "}",
"");
"fun main(args: Array<String>) {", " runApplication<Test>(*args)",
"}");
}
@Test
@@ -136,7 +141,7 @@ class KotlinSourceCodeWriterTests {
(builder) -> builder.attribute("counter", Integer.class, "42")));
assertThat(lines).containsExactly("package com.example", "",
"import org.springframework.test.TestApplication", "",
"@TestApplication(counter = 42)", "class Test", "");
"@TestApplication(counter = 42)", "class Test");
}
@Test
@@ -146,7 +151,7 @@ class KotlinSourceCodeWriterTests {
(builder) -> builder.attribute("name", String.class, "test")));
assertThat(lines).containsExactly("package com.example", "",
"import org.springframework.test.TestApplication", "",
"@TestApplication(name = \"test\")", "class Test", "");
"@TestApplication(name = \"test\")", "class Test");
}
@Test
@@ -156,7 +161,7 @@ class KotlinSourceCodeWriterTests {
(builder) -> builder.attribute("value", String.class, "test")));
assertThat(lines).containsExactly("package com.example", "",
"import org.springframework.test.TestApplication", "",
"@TestApplication(\"test\")", "class Test", "");
"@TestApplication(\"test\")", "class Test");
}
@Test
@@ -168,7 +173,7 @@ class KotlinSourceCodeWriterTests {
assertThat(lines).containsExactly("package com.example", "",
"import java.time.temporal.ChronoUnit",
"import org.springframework.test.TestApplication", "",
"@TestApplication(unit = ChronoUnit.SECONDS)", "class Test", "");
"@TestApplication(unit = ChronoUnit.SECONDS)", "class Test");
}
@Test
@@ -180,7 +185,7 @@ class KotlinSourceCodeWriterTests {
assertThat(lines).containsExactly("package com.example", "",
"import com.example.One", "import com.example.Two",
"import org.springframework.test.TestApplication", "",
"@TestApplication(target = [One::class, Two::class])", "class Test", "");
"@TestApplication(target = [One::class, Two::class])", "class Test");
}
@Test
@@ -194,7 +199,7 @@ class KotlinSourceCodeWriterTests {
"import com.example.One", "import java.time.temporal.ChronoUnit",
"import org.springframework.test.TestApplication", "",
"@TestApplication(target = One::class, unit = ChronoUnit.NANOS)",
"class Test", "");
"class Test");
}
private List<String> writeClassAnnotation(Annotation annotation) throws IOException {
@@ -219,14 +224,14 @@ class KotlinSourceCodeWriterTests {
List<String> lines = writeSingleType(sourceCode, "com/example/Test.kt");
assertThat(lines).containsExactly("package com.example", "",
"import com.example.test.TestAnnotation", "", "class Test {", "",
" @TestAnnotation", " fun something() {", " }", "", "}", "");
" @TestAnnotation", " fun something() {", " }", "", "}");
}
private List<String> writeSingleType(KotlinSourceCode sourceCode, String location)
throws IOException {
Path source = writeSourceCode(sourceCode).resolve(location);
assertThat(source).isRegularFile();
return Files.readAllLines(source);
return readAllLines(source);
}
private Path writeSourceCode(KotlinSourceCode sourceCode) throws IOException {
@@ -235,4 +240,12 @@ class KotlinSourceCodeWriterTests {
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);
}
}