Skip to content

Commit

Permalink
Merge pull request #1480 from Hccake/fix-linebreak
Browse files Browse the repository at this point in the history
Fix the problem that the inconsistent newline characters of different platforms
  • Loading branch information
bnasslahsen committed Jan 28, 2022
2 parents 9ae0ac6 + 5126802 commit 9f73f4e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
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

0 comments on commit 9f73f4e

Please sign in to comment.