+ Aligning code to the article. Removed Utility methods and classes

This commit is contained in:
tritty 2018-06-08 08:19:19 +05:30 committed by Tritty
parent afababb85d
commit 3f75993c4b
5 changed files with 60 additions and 155 deletions

View File

@ -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 {
}

View File

@ -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;
}
}

View File

@ -1 +0,0 @@
This is a sample text to demonstrate usage of Spring Resource.

View File

@ -2,63 +2,100 @@ package com.baeldung.resource;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; 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) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class) @ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class SpringResourceTest { public class SpringResourceTest {
/**
* Resource loader instance for lazily loading resources.
*/
@Autowired
private ResourceLoader resourceLoader;
@Autowired @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 { * Data in data/employee.dat
Resource resource = classPathResourceReader.constructResourceManually(); */
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); private static final String EMPLOYEES_EXPECTED = "Joe Employee,Jan Employee,James T. Employee";
assertEquals(testData, fileData);
}
@Test @Test
public void whenResourceLoader_thenReadSuccessful() throws IOException { public void whenResourceLoader_thenReadSuccessful() throws IOException {
Resource resource = classPathResourceReader.retrieveResourceUsingResourceLoader(); final Resource resource = resourceLoader.getResource("classpath:data/employees.dat");
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); final String employees = new String(Files.readAllBytes(resource.getFile()
assertEquals(testData, fileData); .toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
} }
@Test @Test
public void whenApplicationContext_thenReadSuccessful() throws IOException { public void whenApplicationContext_thenReadSuccessful() throws IOException {
Resource resource = classPathResourceReader.retrieveResourceUsingApplicationContext(); final Resource resource = appContext.getResource("classpath:data/employees.dat");
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); final String employees = new String(Files.readAllBytes(resource.getFile()
assertEquals(testData, fileData); .toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
} }
@Test @Test
public void whenAutowired_thenReadSuccessful() throws IOException { public void whenAutowired_thenReadSuccessful() throws IOException {
Resource resource = classPathResourceReader.getSampleFile(); final String employees = new String(Files.readAllBytes(resourceFile.getFile()
String fileData = classPathResourceReader.listResourceContentsUsingFile(resource.getFile()); .toPath()));
assertEquals(testData, fileData); assertEquals(EMPLOYEES_EXPECTED, employees);
} }
@Test @Test
public void whenResourceUtils_thenReadSuccessful() throws IOException { public void whenResourceUtils_thenReadSuccessful() throws IOException {
String fileData = classPathResourceReader.listResourceContentsUsingFile(classPathResourceReader.retrieveFileUsingResourceUtils()); final String employees = new String(Files.readAllBytes(ResourceUtils.getFile("classpath:data/employees.dat")
assertEquals(testData, fileData); .toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
} }
@Test @Test
public void whenResourceAsStream_thenReadSuccessful() throws IOException { public void whenResourceAsStream_thenReadSuccessful() throws IOException {
Resource resource = classPathResourceReader.retrieveResourceUsingResourceLoader(); final InputStream resource = new ClassPathResource("data/employees.dat").getInputStream();
String fileData = classPathResourceReader.listResourceContentsUsingInputStream(resource.getInputStream()); try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
assertEquals(testData, fileData); 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);
} }
} }

View File

@ -1 +0,0 @@
This is a sample text to demonstrate usage of Spring Resource.