Skip to content

Commit 3b603b4

Browse files
the-java-guypivovarit
authored andcommitted
BAEL-1628 (eugenp#4354)
* Bean Object, server side and client side example for event streaming example * BAEL-1628 Access a File from the Classpath in a Spring Application * inputstream retrieval added * Removed files related to evaluation article * + Aligning code to the article. Removed Utility methods and classes * BAEL - 1628 * PMD fixes * Code Review changes Refactored : whenResourceUtils_thenReadSuccessful
1 parent efa7186 commit 3b603b4

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.baeldung.resource;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.BufferedReader;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.nio.file.Files;
11+
import java.util.stream.Collectors;
12+
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.beans.factory.annotation.Value;
17+
import org.springframework.context.ApplicationContext;
18+
import org.springframework.core.io.ClassPathResource;
19+
import org.springframework.core.io.Resource;
20+
import org.springframework.core.io.ResourceLoader;
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
24+
import org.springframework.util.ResourceUtils;
25+
26+
/**
27+
* Test class illustrating various methods of accessing a file from the classpath using Resource.
28+
* @author tritty
29+
*
30+
*/
31+
32+
@RunWith(SpringJUnit4ClassRunner.class)
33+
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
34+
public class SpringResourceIntegrationTest {
35+
/**
36+
* Resource loader instance for lazily loading resources.
37+
*/
38+
@Autowired
39+
private ResourceLoader resourceLoader;
40+
41+
@Autowired
42+
private ApplicationContext appContext;
43+
44+
/**
45+
* Injecting resource
46+
*/
47+
@Value("classpath:data/employees.dat")
48+
private Resource resourceFile;
49+
50+
/**
51+
* Data in data/employee.dat
52+
*/
53+
private static final String EMPLOYEES_EXPECTED = "Joe Employee,Jan Employee,James T. Employee";
54+
55+
@Test
56+
public void whenResourceLoader_thenReadSuccessful() throws IOException {
57+
final Resource resource = resourceLoader.getResource("classpath:data/employees.dat");
58+
final String employees = new String(Files.readAllBytes(resource.getFile()
59+
.toPath()));
60+
assertEquals(EMPLOYEES_EXPECTED, employees);
61+
}
62+
63+
@Test
64+
public void whenApplicationContext_thenReadSuccessful() throws IOException {
65+
final Resource resource = appContext.getResource("classpath:data/employees.dat");
66+
final String employees = new String(Files.readAllBytes(resource.getFile()
67+
.toPath()));
68+
assertEquals(EMPLOYEES_EXPECTED, employees);
69+
}
70+
71+
@Test
72+
public void whenAutowired_thenReadSuccessful() throws IOException {
73+
final String employees = new String(Files.readAllBytes(resourceFile.getFile()
74+
.toPath()));
75+
assertEquals(EMPLOYEES_EXPECTED, employees);
76+
}
77+
78+
@Test
79+
public void whenResourceUtils_thenReadSuccessful() throws IOException {
80+
final File employeeFile = ResourceUtils.getFile("classpath:data/employees.dat");
81+
final String employees = new String(Files.readAllBytes(employeeFile.toPath()));
82+
assertEquals(EMPLOYEES_EXPECTED, employees);
83+
}
84+
85+
@Test
86+
public void whenResourceAsStream_thenReadSuccessful() throws IOException {
87+
final InputStream resource = new ClassPathResource("data/employees.dat").getInputStream();
88+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource))) {
89+
final String employees = reader.lines()
90+
.collect(Collectors.joining("\n"));
91+
assertEquals(EMPLOYEES_EXPECTED, employees);
92+
}
93+
}
94+
95+
@Test
96+
public void whenResourceAsFile_thenReadSuccessful() throws IOException {
97+
final File resource = new ClassPathResource("data/employees.dat").getFile();
98+
final String employees = new String(Files.readAllBytes(resource.toPath()));
99+
assertEquals(EMPLOYEES_EXPECTED, employees);
100+
}
101+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Joe Employee,Jan Employee,James T. Employee

0 commit comments

Comments
 (0)