/** *

Shows how files in the class path can be accessed when using the standard * Maven directory layout.

* *

The test file is "src/test/resources/accessFileOnClasspath/test.txt". * It is accessed using either {@link ClassLoader#getResource(String)} or * {@link ClassLoader#getResourceAsStream(String)}. Note that the file's * path is always relative to the class path's root. Thus the test file is * accessed using "accessFileOnClasspath/test.txt".

* *

When using the {@link Class#getResource(String)} and * {@link Class#getResourceAsStream(String)} methods, the file's paths are * always relative to the package folder of the accessing class ( * see {@link AccessFileOnClasspath#accessThisNoClassLoader()}.

* */ public class AccessFileOnClasspath { public static final String FILE = "accessFileOnClasspath/test.txt"; public static final String FILE_CONTENT = "Test"; public static final String CLASS_FILE = AccessFileOnClasspath.class.getSimpleName() + ".class"; /** * When not using {@link Class#getResource(String)} to access a file * the directory of the given path is relative to the package folder * of that class. */ @Test public void accessThisNoClassLoader() { /* * The class file is not found because the base directory is not set to * the root of the class path, but ... */ URL testFile = this.getClass().getResource(FILE); Assert.assertNull(testFile); /* * ... the class file is found because the base directory is set to * the package folder of the class. */ URL classFile = this.getClass().getResource(CLASS_FILE); Assert.assertNotNull(classFile); } @Test public void accessThis() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(FILE); Assert.assertNotNull(inputStream); testFileContent(inputStream); } @Test public void accessStatic() throws IOException { InputStream inputStream = AccessFileOnClasspath.class .getClassLoader().getResourceAsStream(FILE); Assert.assertNotNull(inputStream); testFileContent(inputStream); } @Test public void accessThisUrl() throws URISyntaxException, IOException { URL url = this.getClass().getClassLoader().getResource(FILE); Assert.assertNotNull(url); testFileContent(url); } @Test public void accessStaticUrl() throws URISyntaxException, IOException { URL url = AccessFileOnClasspath.class .getClassLoader().getResource(FILE); Assert.assertNotNull(url); testFileContent(url); } /** * Generates an {@link InputStream} from the given {@link URL} and passes * it to {@link AccessFileOnClasspath#testFileContent(InputStream)}. * * @param url * @throws IOException * @throws URISyntaxException */ public void testFileContent(URL url) throws IOException, URISyntaxException { File file = new File(url.toURI()); InputStream inputStream = new FileInputStream(file); testFileContent(inputStream); } /** * Checks the first line of the given {@link InputStream} and closes it. * * @param inputStream * @throws IOException */ public void testFileContent(InputStream inputStream) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = reader.readLine(); Assert.assertEquals(FILE_CONTENT, line); reader.close(); } }