Skip to content

Commit

Permalink
Generated code should have only one newline at the end
Browse files Browse the repository at this point in the history
Fixes gh-828
  • Loading branch information
mbhave committed Feb 15, 2019
1 parent 56187f5 commit 11ffe79
Show file tree
Hide file tree
Showing 36 changed files with 139 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +36,8 @@
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;

/**
Expand Down Expand Up @@ -71,15 +76,15 @@ void testClassIsContributed() throws IOException {
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",
"import org.springframework.boot.test.context.SpringBootTest",
"import org.springframework.test.context.junit4.SpringRunner", "",
"@RunWith(SpringRunner)", "@SpringBootTest",
"class DemoApplicationTests {", "", " @Test",
" void contextLoads() {", " }", "", "}", "");
" void contextLoads() {", " }", "", "}");
}

@Test
Expand All @@ -92,15 +97,23 @@ void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
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",
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer",
"", "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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +36,8 @@
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;

/**
Expand Down Expand Up @@ -72,15 +77,15 @@ void testClassIsContributed() throws IOException {
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;",
"import org.springframework.boot.test.context.SpringBootTest;",
"import org.springframework.test.context.junit4.SpringRunner;", "",
"@RunWith(SpringRunner.class)", "@SpringBootTest",
"public class DemoApplicationTests {", "", " @Test",
" public void contextLoads() {", " }", "", "}", "");
" public void contextLoads() {", " }", "", "}");
}

@Test
Expand All @@ -93,7 +98,7 @@ void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
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;",
Expand All @@ -103,7 +108,7 @@ void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
"", " @Override",
" protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {",
" return application.sources(MyDemoApplication.class);", " }",
"", "}", "");
"", "}");
}

@Test
Expand All @@ -128,4 +133,12 @@ void customApplicationNameIsUsedWhenGeneratingProject() {
"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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +36,8 @@
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;

/**
Expand Down Expand Up @@ -74,15 +79,15 @@ void testClassIsContributed() throws IOException {
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",
"import org.springframework.boot.test.context.SpringBootTest",
"import org.springframework.test.context.junit4.SpringRunner", "",
"@RunWith(SpringRunner::class)", "@SpringBootTest",
"class DemoApplicationTests {", "", " @Test",
" fun contextLoads() {", " }", "", "}", "");
" fun contextLoads() {", " }", "", "}");
}

@Test
Expand All @@ -95,15 +100,23 @@ void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
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",
"import org.springframework.boot.web.servlet.support.SpringBootServletInitializer",
"", "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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class DemoApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class MyProjectApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class ServletInitializer extends SpringBootServletInitializer {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class DemoApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class DemoApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class DemoApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class MyProjectApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class MyProjectApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ class ServletInitializer extends SpringBootServletInitializer {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public class DemoApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public class MyProjectApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public class ServletInitializer extends SpringBootServletInitializer {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public class DemoApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ public class DemoApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ public class DemoApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public class MyProjectApplication {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ public class MyProjectApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public class ServletInitializer extends SpringBootServletInitializer {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class MyProjectApplication
fun main(args: Array<String>) {
SpringApplication.run(MyProjectApplication::class.java, *args)
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ class ServletInitializer : SpringBootServletInitializer() {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class DemoApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class DemoApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class MyProjectApplication
fun main(args: Array<String>) {
runApplication<MyProjectApplication>(*args)
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ class MyProjectApplicationTests {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ class ServletInitializer : SpringBootServletInitializer() {
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ private void writeTo(Path directory, GroovyCompilationUnit compilationUnit)
});
}
writer.println("}");
writer.println("");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ private void writeTo(Path directory, JavaCompilationUnit compilationUnit)
});
}
writer.println("}");
writer.println("");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,17 @@ private void writeTo(Path directory, KotlinCompilationUnit compilationUnit)
.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();
Expand All @@ -112,6 +111,7 @@ private void writeTo(Path directory, KotlinCompilationUnit compilationUnit)

private void writeFunction(IndentingWriter writer,
KotlinFunctionDeclaration functionDeclaration) {
writer.println();
writeAnnotations(writer, functionDeclaration);
writeMethodModifiers(writer, functionDeclaration);
writer.print("fun ");
Expand Down Expand Up @@ -144,7 +144,6 @@ else if (statement instanceof KotlinReturnStatement) {
}
});
writer.println("}");
writer.println();
}

private void writeAnnotations(IndentingWriter writer, Annotatable annotatable) {
Expand Down
Loading

0 comments on commit 11ffe79

Please sign in to comment.