Skip to content

Commit 9f73f4e

Browse files
authored
Merge pull request #1480 from Hccake/fix-linebreak
Fix the problem that the inconsistent newline characters of different platforms
2 parents 9ae0ac6 + 5126802 commit 9f73f4e

File tree

8 files changed

+30
-30
lines changed

8 files changed

+30
-30
lines changed

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/AbstractCommonTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.Files;
55
import java.nio.file.Path;
66
import java.nio.file.Paths;
7+
import java.util.List;
78

89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
@@ -25,8 +26,12 @@ public abstract class AbstractCommonTest {
2526
protected String getContent(String fileName) {
2627
try {
2728
Path path = Paths.get(AbstractCommonTest.class.getClassLoader().getResource(fileName).toURI());
28-
byte[] fileBytes = Files.readAllBytes(path);
29-
return new String(fileBytes, StandardCharsets.UTF_8);
29+
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
30+
StringBuilder sb = new StringBuilder();
31+
for (String line : lines) {
32+
sb.append(line).append("\n");
33+
}
34+
return sb.toString();
3035
}
3136
catch (Exception e) {
3237
throw new RuntimeException("Failed to read file: " + fileName, e);

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/AbstractSpringDocTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ protected void checkHTML(String fileName, String uri)throws Exception {
3939
MvcResult mvcResult = mockMvc.perform(get(uri)).andExpect(status().isOk()).andReturn();
4040
String transformedIndex = mvcResult.getResponse().getContentAsString();
4141
assertTrue(transformedIndex.contains("Swagger UI"));
42-
assertEquals(this.getContent(fileName), transformedIndex);
42+
assertEquals(this.getContent(fileName), transformedIndex.replace("\r", ""));
4343
}
44+
4445
protected void chekHTML(String fileName) throws Exception {
4546
checkHTML( fileName, DEFAULT_SWAGGER_UI_URL);
4647
}
@@ -50,4 +51,9 @@ protected void chekHTML() throws Exception {
5051
String testNumber = className.replaceAll("[^0-9]", "");
5152
checkHTML( "results/app" + testNumber, DEFAULT_SWAGGER_UI_URL);
5253
}
54+
55+
protected void checkHTMLResult(String fileName, String htmlResult)throws Exception {
56+
assertTrue(htmlResult.contains("Swagger UI"));
57+
assertEquals(this.getContent(fileName), htmlResult.replace("\r", ""));
58+
}
5359
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocConfigPathsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public void should_display_swaggerui_page() throws Exception {
5050

5151
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
5252
String transformedIndex = mvcResult.getResponse().getContentAsString();
53-
assertTrue(transformedIndex.contains("Swagger UI"));
54-
assertEquals(this.getContent("results/app1-contextpath"), transformedIndex);
53+
checkHTMLResult("results/app1-contextpath", transformedIndex);
5554
}
5655

5756
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app5/SpringDocOauthServletPathsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public void should_display_oauth2_redirect_page() throws Exception {
4747

4848
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").servletPath("/servlet-path").contextPath("/context-path")).andExpect(status().isOk()).andReturn();
4949
String transformedIndex = mvcResult.getResponse().getContentAsString();
50-
assertTrue(transformedIndex.contains("Swagger UI"));
51-
assertEquals(this.getContent("results/app5-contextpath"), transformedIndex);
50+
checkHTMLResult("results/app5-contextpath", transformedIndex);
5251
}
5352

5453
@Test

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/AbstractCommonTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.Files;
55
import java.nio.file.Path;
66
import java.nio.file.Paths;
7+
import java.util.List;
78

89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
@@ -25,8 +26,12 @@ public abstract class AbstractCommonTest {
2526
protected String getContent(String fileName) {
2627
try {
2728
Path path = Paths.get(AbstractCommonTest.class.getClassLoader().getResource(fileName).toURI());
28-
byte[] fileBytes = Files.readAllBytes(path);
29-
return new String(fileBytes, StandardCharsets.UTF_8);
29+
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
30+
StringBuilder sb = new StringBuilder();
31+
for (String line : lines) {
32+
sb.append(line).append("\n");
33+
}
34+
return sb.toString();
3035
}
3136
catch (Exception e) {
3237
throw new RuntimeException("Failed to read file: " + fileName, e);

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/AbstractSpringDocTest.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,18 @@ public abstract class AbstractSpringDocTest extends AbstractCommonTest {
5151

5252
private static final String DEFAULT_SWAGGER_UI_URL= Constants.DEFAULT_WEB_JARS_PREFIX_URL + Constants.SWAGGER_UI_URL;
5353

54-
protected String getContent(String fileName) {
55-
try {
56-
Path path = Paths.get(AbstractSpringDocTest.class.getClassLoader().getResource(fileName).toURI());
57-
byte[] fileBytes = Files.readAllBytes(path);
58-
return new String(fileBytes, StandardCharsets.UTF_8);
59-
}
60-
catch (Exception e) {
61-
throw new RuntimeException("Failed to read file: " + fileName, e);
62-
}
63-
}
64-
6554
protected void checkHTML(String fileName, String uri) {
6655
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(uri)
6756
.exchange()
6857
.expectStatus().isOk()
6958
.expectBody().returnResult();
70-
String result = new String(getResult.getResponseBody());
59+
checkHTMLResult(fileName, new String(getResult.getResponseBody()));
60+
}
61+
62+
protected void checkHTMLResult(String fileName, String result) {
7163
assertTrue(result.contains("Swagger UI"));
7264
String expected = getContent("results/" + fileName);
73-
assertEquals(expected, result);
65+
assertEquals(expected, result.replace("\r", ""));
7466
}
7567

7668
protected void checkHTML(String fileName) {

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app6/SpringDocApp6Test.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public void transformed_index_with_oauth() throws Exception {
4242
.exchange()
4343
.expectStatus().isOk()
4444
.expectBody().returnResult();
45-
String result = new String(getResult.getResponseBody());
46-
assertTrue(result.contains("Swagger UI"));
47-
String expected = getContent("results/index6");
48-
assertEquals(expected, result);
45+
checkHTMLResult("index6", new String(getResult.getResponseBody()));
4946
}
5047

5148
@SpringBootApplication

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app7/SpringDocApp7Test.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public void transformed_index_with_oauth() throws Exception {
4242
.exchange()
4343
.expectStatus().isOk()
4444
.expectBody().returnResult();
45-
String result = new String(getResult.getResponseBody());
46-
assertTrue(result.contains("Swagger UI"));
47-
String expected = getContent("results/index7");
48-
assertEquals(expected, result);
45+
checkHTMLResult("index7", new String(getResult.getResponseBody()));
4946
}
5047

5148
@SpringBootApplication

0 commit comments

Comments
 (0)