Skip to content

Fix the problem that the inconsistent newline characters of different platforms #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,8 +26,12 @@ public abstract class AbstractCommonTest {
protected String getContent(String fileName) {
try {
Path path = Paths.get(AbstractCommonTest.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
return new String(fileBytes, StandardCharsets.UTF_8);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append(line).append("\n");
}
return sb.toString();
}
catch (Exception e) {
throw new RuntimeException("Failed to read file: " + fileName, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ protected void checkHTML(String fileName, String uri)throws Exception {
MvcResult mvcResult = mockMvc.perform(get(uri)).andExpect(status().isOk()).andReturn();
String transformedIndex = mvcResult.getResponse().getContentAsString();
assertTrue(transformedIndex.contains("Swagger UI"));
assertEquals(this.getContent(fileName), transformedIndex);
assertEquals(this.getContent(fileName), transformedIndex.replace("\r", ""));
}

protected void chekHTML(String fileName) throws Exception {
checkHTML( fileName, DEFAULT_SWAGGER_UI_URL);
}
Expand All @@ -50,4 +51,9 @@ protected void chekHTML() throws Exception {
String testNumber = className.replaceAll("[^0-9]", "");
checkHTML( "results/app" + testNumber, DEFAULT_SWAGGER_UI_URL);
}

protected void checkHTMLResult(String fileName, String htmlResult)throws Exception {
assertTrue(htmlResult.contains("Swagger UI"));
assertEquals(this.getContent(fileName), htmlResult.replace("\r", ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void should_display_swaggerui_page() throws Exception {

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

@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public void should_display_oauth2_redirect_page() throws Exception {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,8 +26,12 @@ public abstract class AbstractCommonTest {
protected String getContent(String fileName) {
try {
Path path = Paths.get(AbstractCommonTest.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
return new String(fileBytes, StandardCharsets.UTF_8);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
for (String line : lines) {
sb.append(line).append("\n");
}
return sb.toString();
}
catch (Exception e) {
throw new RuntimeException("Failed to read file: " + fileName, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,18 @@ public abstract class AbstractSpringDocTest extends AbstractCommonTest {

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

protected String getContent(String fileName) {
try {
Path path = Paths.get(AbstractSpringDocTest.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
return new String(fileBytes, StandardCharsets.UTF_8);
}
catch (Exception e) {
throw new RuntimeException("Failed to read file: " + fileName, e);
}
}

protected void checkHTML(String fileName, String uri) {
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(uri)
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();
String result = new String(getResult.getResponseBody());
checkHTMLResult(fileName, new String(getResult.getResponseBody()));
}

protected void checkHTMLResult(String fileName, String result) {
assertTrue(result.contains("Swagger UI"));
String expected = getContent("results/" + fileName);
assertEquals(expected, result);
assertEquals(expected, result.replace("\r", ""));
}

protected void checkHTML(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public void transformed_index_with_oauth() throws Exception {
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();
String result = new String(getResult.getResponseBody());
assertTrue(result.contains("Swagger UI"));
String expected = getContent("results/index6");
assertEquals(expected, result);
checkHTMLResult("index6", new String(getResult.getResponseBody()));
}

@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public void transformed_index_with_oauth() throws Exception {
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();
String result = new String(getResult.getResponseBody());
assertTrue(result.contains("Swagger UI"));
String expected = getContent("results/index7");
assertEquals(expected, result);
checkHTMLResult("index7", new String(getResult.getResponseBody()));
}

@SpringBootApplication
Expand Down