diff --git a/spring-core/src/main/java/com/baeldung/resource/AppConfig.java b/spring-core/src/main/java/com/baeldung/resource/AppConfig.java new file mode 100644 index 0000000000..5b266cb5cf --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/resource/AppConfig.java @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000000..f1e88eef04 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/resource/ClassPathResourceReader.java @@ -0,0 +1,98 @@ +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 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 ofa 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(); + } + + fileReader.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 new file mode 100644 index 0000000000..cf3720cd66 --- /dev/null +++ b/spring-core/src/main/resources/data/resource-data.txt @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000..7703a016f4 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/resource/SpringResourceTest.java @@ -0,0 +1,57 @@ +package com.baeldung.resource; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class) +public class SpringResourceTest { + + @Autowired + private ClassPathResourceReader classPathResourceReader; + + static final String testData = "This is a sample text to demonstrate usage of Spring Resource."; + + @Test + public void whenManualInstance_thenReadSuccessful() throws IOException { + Resource resource = classPathResourceReader.constructResourceManually(); + String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); + assertEquals(testData, fileData); + } + + @Test + public void whenResourceLoader_thenReadSuccessful() throws IOException { + Resource resource = classPathResourceReader.retrieveResourceUsingResourceLoader(); + String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); + assertEquals(testData, fileData); + } + + @Test + public void whenApplicationContext_thenReadSuccessful() throws IOException { + Resource resource = classPathResourceReader.retrieveResourceUsingApplicationContext(); + String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); + assertEquals(testData, fileData); + } + + @Test + public void whenAutowired_thenReadSuccessful() throws IOException { + Resource resource = classPathResourceReader.getSampleFile(); + String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); + assertEquals(testData, fileData); + } + + @Test + public void whenResourceUtils_thenReadSuccessful() throws IOException { + String fileData = classPathResourceReader.listResourceContentsUsingFile(classPathResourceReader.retrieveFileUsingResourceUtils()); + assertEquals(testData, fileData); + } +} diff --git a/spring-core/src/test/resources/data/resource-data.txt b/spring-core/src/test/resources/data/resource-data.txt new file mode 100644 index 0000000000..cf3720cd66 --- /dev/null +++ b/spring-core/src/test/resources/data/resource-data.txt @@ -0,0 +1 @@ +This is a sample text to demonstrate usage of Spring Resource. \ No newline at end of file