Introduce Resource::getContentAsString

This commit introduces the getContentAsString method to Resource,
returning the string contents of the resource.

Closes gh-24651
This commit is contained in:
Derrick Anderson
2020-03-06 21:26:28 -06:00
committed by Arjen Poutsma
parent 18896aceca
commit 4da2499613
4 changed files with 97 additions and 2 deletions

View File

@@ -456,4 +456,25 @@ class ResourceTests {
}
@Test
void getContentAsString_givenValidFile_ShouldReturnFileContent() throws IOException {
String expectedString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n"
+ "<properties version=\"1.0\">\n"
+ "\t<entry key=\"foo\">bar</entry>\n"
+ "</properties>";
String fileDirString =
new ClassPathResource("org/springframework/core/io/example.xml").getContentAsString();
assertThat(fileDirString).isNotBlank();
assertThat(fileDirString).isEqualTo(expectedString);
}
@Test
void getContentAsString_givenAnInvalidFile_ShouldThrowFileNotFoundException(){
assertThatExceptionOfType(FileNotFoundException.class)
.isThrownBy(new ClassPathResource("nonExistantFile")::getContentAsString);
}
}