forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BAEL-2434 * BAEL-2434 * Minor alterations to reflect changes in the article * Fixing Spel expression and removing extra code that is not referenced anymore in the article * Moved the files from spring-core to spring-static-resources. Renamed ResourceUtil to ResourceReader. * Changes due to change of article outline. * Changes due to change of article outline. * Removing Converter from article
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...-static-resources/src/main/java/com/baeldung/loadresourceasstring/LoadResourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.loadresourceasstring; | ||
|
||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class LoadResourceConfig { | ||
|
||
@Bean | ||
public String resourceString() { | ||
return ResourceReader.readFileToString("resource.txt"); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
spring-static-resources/src/main/java/com/baeldung/loadresourceasstring/ResourceReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.baeldung.loadresourceasstring; | ||
|
||
import org.springframework.core.io.DefaultResourceLoader; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.util.FileCopyUtils; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.io.UncheckedIOException; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
|
||
public class ResourceReader { | ||
|
||
public static String readFileToString(String path) { | ||
ResourceLoader resourceLoader = new DefaultResourceLoader(); | ||
Resource resource = resourceLoader.getResource(path); | ||
return asString(resource); | ||
} | ||
|
||
public static String asString(Resource resource) { | ||
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) { | ||
return FileCopyUtils.copyToString(reader); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a resource text file. This file will be loaded as a resource and use its contents as a string. |
57 changes: 57 additions & 0 deletions
57
.../src/test/java/com/baeldung/loadresourceasstring/LoadResourceAsStringIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.baeldung.loadresourceasstring; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.core.io.DefaultResourceLoader; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.support.AnnotationConfigContextLoader; | ||
import org.springframework.util.FileCopyUtils; | ||
|
||
import java.io.InputStreamReader; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = LoadResourceConfig.class) | ||
public class LoadResourceAsStringIntegrationTest { | ||
|
||
private static final String EXPECTED_RESOURCE_VALUE = "This is a resource text file. This file will be loaded as a " + "resource and use its contents as a string."; | ||
|
||
@Value("#{T(com.baeldung.loadresourceasstring.ResourceReader).readFileToString('classpath:resource.txt')}") | ||
private String resourceStringUsingSpel; | ||
|
||
@Autowired | ||
@Qualifier("resourceString") | ||
private String resourceString; | ||
|
||
@Autowired | ||
private ResourceLoader resourceLoader; | ||
|
||
@Test | ||
public void givenUsingResourceLoadAndFileCopyUtils_whenConvertingAResourceToAString_thenCorrect() { | ||
Resource resource = resourceLoader.getResource("classpath:resource.txt"); | ||
assertEquals(EXPECTED_RESOURCE_VALUE, ResourceReader.asString(resource)); | ||
} | ||
|
||
@Test | ||
public void givenUsingResourceStringBean_whenConvertingAResourceToAString_thenCorrect() { | ||
assertEquals(EXPECTED_RESOURCE_VALUE, resourceString); | ||
} | ||
|
||
@Test | ||
public void givenUsingSpel_whenConvertingAResourceToAString_thenCorrect() { | ||
assertEquals(EXPECTED_RESOURCE_VALUE, resourceStringUsingSpel); | ||
} | ||
|
||
|
||
|
||
|
||
} |