Guest User

Unit Test Support File Resolver

a guest
Apr 20th, 2012
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URL;
  4. import java.net.URLClassLoader;
  5. import java.net.URLDecoder;
  6.  
  7. import org.apache.log4j.Logger;
  8.  
  9. import com.eztech.file.FileUtils;
  10. import com.eztech.util.StringUtil;
  11.  
  12. /**
  13.  * Class to make support files in unit tests more portable by allowing them to
  14.  * live in different directories for different environment. Uses
  15.  * ClassLoader.getSystemResource to resolve the name of the file to its path.
  16.  *
  17.  * @author Sam
  18.  *
  19.  */
  20. public class SupportFilePathResolver {
  21.  
  22.     public static final String DEFAULT_SUPPORT_FILE_FOLDER = "supportfiles";
  23.  
  24.     /**
  25.      * Setting this to true, will cause this to print the Classpath to STDERR if
  26.      * it fails to look up a file
  27.      */
  28.     public static boolean dumpClasspathOnError = false;
  29.  
  30.     private static final Logger LOG = Logger.getLogger(SupportFilePathResolver.class);
  31.  
  32.     private static SupportFilePathResolver instance;
  33.     private String baseSupportFileFolder = DEFAULT_SUPPORT_FILE_FOLDER;
  34.  
  35.     public String getBaseSupportFileFolder() {
  36.         return baseSupportFileFolder;
  37.     }
  38.  
  39.     public void setBaseSupportFileFolder(String baseSupportFileFolder) {
  40.         this.baseSupportFileFolder = baseSupportFileFolder;
  41.     }
  42.  
  43.     public SupportFilePathResolver() {
  44.     }
  45.  
  46.     public SupportFilePathResolver(Class<?> basePackageClass) {
  47.         this(FileUtils.getPackageNameAsRelativePath(basePackageClass));
  48.     }
  49.  
  50.     public SupportFilePathResolver(String baseFolderName) {
  51.         this.baseSupportFileFolder = baseFolderName;
  52.     }
  53.  
  54.     public static void setInstance(SupportFilePathResolver resolverInstance) {
  55.         instance = resolverInstance;
  56.     }
  57.  
  58.     public static SupportFilePathResolver getInstance() {
  59.         if (instance == null) {
  60.             instance = new SupportFilePathResolver();
  61.         }
  62.         return instance;
  63.     }
  64.  
  65.     /**
  66.      * Gets full path for specified file name. File must be in classpath. If a
  67.      * base folder is set on the instance, will start search from specified base
  68.      * folder.
  69.      *
  70.      * @param fileName
  71.      *            name of file to find. can be prefixed with additional folder
  72.      *            names. Use '/' (backslash) to separate directories
  73.      * @return the file system file path. Throws RuntimeException if file is not
  74.      *         in classpath.
  75.      */
  76.     public static String resolveSupportFilePath(String fileName) {
  77.         return getInstance().getSupportFilePath(fileName);
  78.     }
  79.  
  80.     /**
  81.      * Gets full path for specified file name. File must be in classpath. If a
  82.      * base folder is set on the instance, will start search from specified base
  83.      * folder. Throws a RuntimeException if the file cannot be found in the
  84.      * classpath.
  85.      *
  86.      * @param fileName
  87.      *            name of file to find. can be prefixed with additional folder
  88.      *            names. Use '/' (backslash) to separate directories
  89.      * @return the file system file path. Throws RuntimeException if file is not
  90.      *         in classpath.
  91.      */
  92.     public String getSupportFilePath(String fileName) {
  93.  
  94.         URL fileUrl = getFileURL(fileName);
  95.         String filePath = new File(fileUrl.getFile()).getAbsolutePath();
  96.         if (StringUtil.stringContains(filePath, "%")) {
  97.             try {
  98.                 filePath = URLDecoder.decode(filePath, "UTF-8");
  99.             } catch (UnsupportedEncodingException e) {
  100.                 e.printStackTrace();
  101.             }
  102.         }
  103.         return filePath;
  104.     }
  105.  
  106.     /**
  107.      * Gets the URL for file in classpath, starting with base support folder.
  108.      * File name should be a relative path, using directory separator character
  109.      * to specify the path to the file, e.g. com/eztech/test/myfile.txt
  110.      *
  111.      * @param fileName
  112.      * @return url of file
  113.      */
  114.     public URL getFileURL(String fileName) {
  115.         if (baseSupportFileFolder != null) {
  116.             fileName = baseSupportFileFolder + "/" + fileName;
  117.         }
  118.         LOG.info("getSupportFilePath(): trying to find URL for fileName=" + fileName);
  119.         URL fileUrl = this.getClass().getClassLoader().getResource(fileName);
  120.         if (fileUrl == null) {
  121.             if (dumpClasspathOnError) {
  122.                 dumpClasspath();
  123.             }
  124.             throw new RuntimeException("Unable to locate resource with name=" + fileName);
  125.         }
  126.         return fileUrl;
  127.     }
  128.  
  129.     private void dumpClasspath() {
  130.         //Get the System Classloader
  131.         ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
  132.  
  133.         //Get the URLs
  134.         URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();
  135.         System.err.println("Dumping classpath (SysClassLoader)...:");
  136.         for (int i = 0; i < urls.length; i++) {        
  137.             System.err.println(urls[i].getFile());
  138.         }
  139.  
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment