|
| 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 | +} |
0 commit comments