diff --git a/spring-core/src/main/java/com/baeldung/resource/AppConfig.java b/spring-core/src/main/java/com/baeldung/resource/AppConfig.java deleted file mode 100644 index 5b266cb5cf..0000000000 --- a/spring-core/src/main/java/com/baeldung/resource/AppConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.resource; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan("com.baeldung.resource") -public class AppConfig { - -} diff --git a/spring-core/src/main/java/com/baeldung/resource/ClassPathResourceReader.java b/spring-core/src/main/java/com/baeldung/resource/ClassPathResourceReader.java deleted file mode 100644 index c4618247e9..0000000000 --- a/spring-core/src/main/java/com/baeldung/resource/ClassPathResourceReader.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.baeldung.resource; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationContext; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.core.io.ResourceLoader; -import org.springframework.stereotype.Component; -import org.springframework.util.ResourceUtils; - -@Component -public class ClassPathResourceReader { - - @Autowired - ResourceLoader resourceLoader; - - @Autowired - ApplicationContext applicationContext; - - @Value("classpath:data/resource-data.txt") - Resource resourceFile; - - /** - * Constructs Resource object by making use of its built-in implementations. - * - * @return Resource - */ - public Resource constructResourceManually() { - Resource resource = new ClassPathResource("data/resource-data.txt"); - return resource; - } - - /** - * Constructs resource object by making use of ResourceLoader. - * - * @return Resource - */ - public Resource retrieveResourceUsingResourceLoader() { - Resource resource = resourceLoader.getResource("classpath:data/resource-data.txt"); - return resource; - } - - /** - * Constructs Resource instance by making use of ApplicationContext. - * - * @return Resource - */ - public Resource retrieveResourceUsingApplicationContext() { - Resource resource = applicationContext.getResource("classpath:data/resource-data.txt"); - return resource; - } - - /** - * ResourceUtils example for getting file data. - * - * @return - * @throws FileNotFoundException - */ - public File retrieveFileUsingResourceUtils() throws FileNotFoundException { - return ResourceUtils.getFile("classpath:data/resource-data.txt"); - } - - /** - * Utility method to list contents of a file. - * - * @param fileResource - * @return - * @throws IOException - */ - public String listResourceContentsUsingFile(File fileResource) throws IOException { - FileReader fileReader = new FileReader(fileResource); - BufferedReader bufReader = new BufferedReader(fileReader); - String dataLine = bufReader.readLine(); - StringBuilder fileData = new StringBuilder(); - while (null != dataLine) { - fileData.append(dataLine); - dataLine = bufReader.readLine(); - } - bufReader.close(); - fileReader.close(); - return fileData.toString(); - } - - /** - * Utility method to list contents of a stream - * - * @param ipStream - * @return - * @throws IOException - */ - public String listResourceContentsUsingInputStream(InputStream ipStream) throws IOException { - BufferedReader bufReader = new BufferedReader(new InputStreamReader(ipStream, "UTF-8")); - String dataLine = bufReader.readLine(); - StringBuilder fileData = new StringBuilder(); - while (null != dataLine) { - fileData.append(dataLine); - dataLine = bufReader.readLine(); - } - bufReader.close(); - return fileData.toString(); - } - - public Resource getSampleFile() { - return resourceFile; - } - - public void setSampleFile(Resource sampleFile) { - this.resourceFile = sampleFile; - } -} diff --git a/spring-core/src/main/resources/data/resource-data.txt b/spring-core/src/main/resources/data/resource-data.txt deleted file mode 100644 index cf3720cd66..0000000000 --- a/spring-core/src/main/resources/data/resource-data.txt +++ /dev/null @@ -1 +0,0 @@ -This is a sample text to demonstrate usage of Spring Resource. \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/resource/SpringResourceTest.java b/spring-core/src/test/java/com/baeldung/resource/SpringResourceTest.java index ba1e2c6d37..cd2b9ee7c1 100644 --- a/spring-core/src/test/java/com/baeldung/resource/SpringResourceTest.java +++ b/spring-core/src/test/java/com/baeldung/resource/SpringResourceTest.java @@ -2,63 +2,100 @@ package com.baeldung.resource; import static org.junit.Assert.assertEquals; +import java.io.BufferedReader; +import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.util.stream.Collectors; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.core.io.ClassPathResource; 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.ResourceUtils; + +/** + * Test class illustrating various methods of accessing a file from the classpath using Resource. + * @author tritty + * + */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) public class SpringResourceTest { + /** + * Resource loader instance for lazily loading resources. + */ + @Autowired + private ResourceLoader resourceLoader; @Autowired - private ClassPathResourceReader classPathResourceReader; + private ApplicationContext appContext; - static final String testData = "This is a sample text to demonstrate usage of Spring Resource."; + /** + * Injecting resource + */ + @Value("classpath:data/employees.dat") + private Resource resourceFile; - @Test - public void whenManualInstance_thenReadSuccessful() throws IOException { - Resource resource = classPathResourceReader.constructResourceManually(); - String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); - assertEquals(testData, fileData); - } + /** + * Data in data/employee.dat + */ + private static final String EMPLOYEES_EXPECTED = "Joe Employee,Jan Employee,James T. Employee"; @Test public void whenResourceLoader_thenReadSuccessful() throws IOException { - Resource resource = classPathResourceReader.retrieveResourceUsingResourceLoader(); - String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); - assertEquals(testData, fileData); + final Resource resource = resourceLoader.getResource("classpath:data/employees.dat"); + final String employees = new String(Files.readAllBytes(resource.getFile() + .toPath())); + assertEquals(EMPLOYEES_EXPECTED, employees); } @Test public void whenApplicationContext_thenReadSuccessful() throws IOException { - Resource resource = classPathResourceReader.retrieveResourceUsingApplicationContext(); - String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); - assertEquals(testData, fileData); + final Resource resource = appContext.getResource("classpath:data/employees.dat"); + final String employees = new String(Files.readAllBytes(resource.getFile() + .toPath())); + assertEquals(EMPLOYEES_EXPECTED, employees); } @Test public void whenAutowired_thenReadSuccessful() throws IOException { - Resource resource = classPathResourceReader.getSampleFile(); - String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); - assertEquals(testData, fileData); + final String employees = new String(Files.readAllBytes(resourceFile.getFile() + .toPath())); + assertEquals(EMPLOYEES_EXPECTED, employees); } @Test public void whenResourceUtils_thenReadSuccessful() throws IOException { - String fileData = classPathResourceReader.listResourceContentsUsingFile(classPathResourceReader.retrieveFileUsingResourceUtils()); - assertEquals(testData, fileData); + final String employees = new String(Files.readAllBytes(ResourceUtils.getFile("classpath:data/employees.dat") + .toPath())); + assertEquals(EMPLOYEES_EXPECTED, employees); } @Test public void whenResourceAsStream_thenReadSuccessful() throws IOException { - Resource resource = classPathResourceReader.retrieveResourceUsingResourceLoader(); - String fileData = classPathResourceReader.listResourceContentsUsingInputStream(resource.getInputStream()); - assertEquals(testData, fileData); + final InputStream resource = new ClassPathResource("data/employees.dat").getInputStream(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) { + final String employees = reader.lines() + .collect(Collectors.joining("\n")); + assertEquals(EMPLOYEES_EXPECTED, employees); + } + } + + @Test + public void whenResourceAsFile_thenReadSuccessful() throws IOException { + final File resource = new ClassPathResource("data/employees.dat").getFile(); + final String employees = new String(Files.readAllBytes(resource.toPath())); + assertEquals(EMPLOYEES_EXPECTED, employees); } } diff --git a/spring-core/src/test/resources/data/resource-data.txt b/spring-core/src/test/resources/data/resource-data.txt deleted file mode 100644 index cf3720cd66..0000000000 --- a/spring-core/src/test/resources/data/resource-data.txt +++ /dev/null @@ -1 +0,0 @@ -This is a sample text to demonstrate usage of Spring Resource. \ No newline at end of file