Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.net.URLDecoder;
- import org.apache.log4j.Logger;
- import com.eztech.file.FileUtils;
- import com.eztech.util.StringUtil;
- /**
- * Class to make support files in unit tests more portable by allowing them to
- * live in different directories for different environment. Uses
- * ClassLoader.getSystemResource to resolve the name of the file to its path.
- *
- * @author Sam
- *
- */
- public class SupportFilePathResolver {
- public static final String DEFAULT_SUPPORT_FILE_FOLDER = "supportfiles";
- /**
- * Setting this to true, will cause this to print the Classpath to STDERR if
- * it fails to look up a file
- */
- public static boolean dumpClasspathOnError = false;
- private static final Logger LOG = Logger.getLogger(SupportFilePathResolver.class);
- private static SupportFilePathResolver instance;
- private String baseSupportFileFolder = DEFAULT_SUPPORT_FILE_FOLDER;
- public String getBaseSupportFileFolder() {
- return baseSupportFileFolder;
- }
- public void setBaseSupportFileFolder(String baseSupportFileFolder) {
- this.baseSupportFileFolder = baseSupportFileFolder;
- }
- public SupportFilePathResolver() {
- }
- public SupportFilePathResolver(Class<?> basePackageClass) {
- this(FileUtils.getPackageNameAsRelativePath(basePackageClass));
- }
- public SupportFilePathResolver(String baseFolderName) {
- this.baseSupportFileFolder = baseFolderName;
- }
- public static void setInstance(SupportFilePathResolver resolverInstance) {
- instance = resolverInstance;
- }
- public static SupportFilePathResolver getInstance() {
- if (instance == null) {
- instance = new SupportFilePathResolver();
- }
- return instance;
- }
- /**
- * Gets full path for specified file name. File must be in classpath. If a
- * base folder is set on the instance, will start search from specified base
- * folder.
- *
- * @param fileName
- * name of file to find. can be prefixed with additional folder
- * names. Use '/' (backslash) to separate directories
- * @return the file system file path. Throws RuntimeException if file is not
- * in classpath.
- */
- public static String resolveSupportFilePath(String fileName) {
- return getInstance().getSupportFilePath(fileName);
- }
- /**
- * Gets full path for specified file name. File must be in classpath. If a
- * base folder is set on the instance, will start search from specified base
- * folder. Throws a RuntimeException if the file cannot be found in the
- * classpath.
- *
- * @param fileName
- * name of file to find. can be prefixed with additional folder
- * names. Use '/' (backslash) to separate directories
- * @return the file system file path. Throws RuntimeException if file is not
- * in classpath.
- */
- public String getSupportFilePath(String fileName) {
- URL fileUrl = getFileURL(fileName);
- String filePath = new File(fileUrl.getFile()).getAbsolutePath();
- if (StringUtil.stringContains(filePath, "%")) {
- try {
- filePath = URLDecoder.decode(filePath, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- return filePath;
- }
- /**
- * Gets the URL for file in classpath, starting with base support folder.
- * File name should be a relative path, using directory separator character
- * to specify the path to the file, e.g. com/eztech/test/myfile.txt
- *
- * @param fileName
- * @return url of file
- */
- public URL getFileURL(String fileName) {
- if (baseSupportFileFolder != null) {
- fileName = baseSupportFileFolder + "/" + fileName;
- }
- LOG.info("getSupportFilePath(): trying to find URL for fileName=" + fileName);
- URL fileUrl = this.getClass().getClassLoader().getResource(fileName);
- if (fileUrl == null) {
- if (dumpClasspathOnError) {
- dumpClasspath();
- }
- throw new RuntimeException("Unable to locate resource with name=" + fileName);
- }
- return fileUrl;
- }
- private void dumpClasspath() {
- //Get the System Classloader
- ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
- //Get the URLs
- URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();
- System.err.println("Dumping classpath (SysClassLoader)...:");
- for (int i = 0; i < urls.length; i++) {
- System.err.println(urls[i].getFile());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment