Java Resource Get getResourceDir(final Class testClass)

Here you can find the source of getResourceDir(final Class testClass)

Description

Get test resource directory

License

Apache License

Parameter

Parameter Description
testClass test class

Exception

Parameter Description
RuntimeException if retrieving the directory failed

Return

resource directory

Declaration

public static File getResourceDir(final Class<?> testClass) throws RuntimeException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**//from w  w  w. j a  va2 s  .  c  o  m
     * Get test resource directory
     * 
     * @param testClass test class
     * @return resource directory
     * @throws RuntimeException if retrieving the directory failed
     */
    public static File getResourceDir(final Class<?> testClass) throws RuntimeException {
        final URL dir = ClassLoader.getSystemResource(testClass.getSimpleName());
        if (dir == null) {
            throw new RuntimeException("Failed to find resource for " + testClass.getSimpleName());
        }
        try {
            return new File(dir.toURI());
        } catch (URISyntaxException e) {
            throw new RuntimeException(
                    "Failed to find resource for " + testClass.getSimpleName() + ":" + e.getMessage(), e);
        }
    }
}

Related

  1. getResourceAsString(String prefix, String resource)
  2. getResourceAsString(String resource)
  3. getResourceBase()
  4. getResourceBundleLocaleString(String sKey, Object[] sParam, String sResourceBundle)
  5. getResourceContent(Object object, String resource)
  6. getResourceExpression(String select)
  7. getResourceFile(Class clazz, String fileName)
  8. getResourceFile(String name)
  9. getResourceFromBundle(Class clazz, String location)