Bael 2434 (#6635)
* 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
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user