BAEL-1978: Added a test to demostrate classpath resource with relative path

This commit is contained in:
Yatendra Goel 2018-12-28 21:57:38 +05:30
parent 3b30ae74ce
commit 4d6cb52be4
1 changed files with 19 additions and 0 deletions

View File

@ -7,6 +7,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.util.stream.Collectors;
@ -23,6 +25,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.util.ResourceUtils;
import com.baeldung.dependson.config.TestConfig;
/**
* Test class illustrating various methods of accessing a file from the classpath using Resource.
* @author tritty
@ -98,4 +102,19 @@ public class SpringResourceIntegrationTest {
final String employees = new String(Files.readAllBytes(resource.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenClassPathResourceWithAbsoultePath_thenReadSuccessful() throws IOException {
final File resource = new ClassPathResource("/data/employees.dat", this.getClass()).getFile();
final String employees = new String(Files.readAllBytes(resource.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
@Test
public void whenClassPathResourceWithRelativePath_thenReadSuccessful() throws IOException {
// final File resource = new ClassPathResource("../../../data/employees.dat", SpringResourceIntegrationTest.class).getFile();
final File resource = new ClassPathResource("/data/employees.dat", SpringResourceIntegrationTest.class).getFile();
final String employees = new String(Files.readAllBytes(resource.toPath()));
assertEquals(EMPLOYEES_EXPECTED, employees);
}
}