Coverage Report - org.jbehave.core.io.CodeLocations
 
Classes in this File Line Coverage Branch Coverage Complexity
CodeLocations
90%
19/21
100%
6/6
3.4
CodeLocations$InvalidCodeLocation
100%
2/2
N/A
3.4
 
 1  
 package org.jbehave.core.io;
 2  
 
 3  
 import static org.apache.commons.lang.StringUtils.removeEnd;
 4  
 import static org.apache.commons.lang.StringUtils.removeStart;
 5  
 
 6  
 import java.io.File;
 7  
 import java.net.URI;
 8  
 import java.net.URISyntaxException;
 9  
 import java.net.URL;
 10  
 
 11  
 /**
 12  
  * Collection of utility methods to create code location URLs
 13  
  */
 14  1
 public class CodeLocations {
 15  
 
 16  
     /**
 17  
      * Creates a code location URL from a class
 18  
      * 
 19  
      * @param codeLocationClass the class
 20  
      * @return A URL created from Class
 21  
      * @throws InvalidCodeLocation if URL creation fails
 22  
      */
 23  
     public static URL codeLocationFromClass(Class<?> codeLocationClass) {
 24  32
         String pathOfClass = codeLocationClass.getName().replace(".", "/") + ".class";
 25  32
         URL classResource = codeLocationClass.getClassLoader().getResource(pathOfClass);
 26  32
         String codeLocationPath = removeEnd(getPathFromURL(classResource), pathOfClass);
 27  32
         if(codeLocationPath.endsWith(".jar!/")) {
 28  1
             codeLocationPath=removeEnd(codeLocationPath,"!/");
 29  
         }
 30  32
         return codeLocationFromPath(codeLocationPath);
 31  
     }
 32  
 
 33  
     /**
 34  
      * Creates a code location URL from a file path
 35  
      * 
 36  
      * @param filePath the file path
 37  
      * @return A URL created from File
 38  
      * @throws InvalidCodeLocation if URL creation fails
 39  
      */
 40  
     public static URL codeLocationFromPath(String filePath) {
 41  
         try {
 42  348
             return new File(filePath).toURI().toURL();
 43  1
         } catch (Exception e) {
 44  1
             throw new InvalidCodeLocation(filePath);
 45  
         }
 46  
     }
 47  
 
 48  
     /**
 49  
      * Creates a code location URL from a URL
 50  
      * 
 51  
      * @param url the URL external form
 52  
      * @return A URL created from URL
 53  
      * @throws InvalidCodeLocation if URL creation fails
 54  
      */
 55  
     public static URL codeLocationFromURL(String url) {
 56  
         try {
 57  4
             return new URL(url);
 58  1
         } catch (Exception e) {
 59  1
             throw new InvalidCodeLocation(url);
 60  
         }
 61  
     }
 62  
 
 63  1
     @SuppressWarnings("serial")
 64  
     public static class InvalidCodeLocation extends RuntimeException {
 65  
 
 66  
         public InvalidCodeLocation(String path) {
 67  2
             super(path);
 68  2
         }
 69  
 
 70  
     }
 71  
 
 72  
     /**
 73  
      * Get absolute path from URL objects starting with file:
 74  
      * This method takes care of decoding %-encoded chars, e.g. %20 -> space etc
 75  
      * Since we do not use a File object, the system specific path encoding
 76  
      * is not used (e.g. C:\ on Windows). This is necessary to facilitate
 77  
      * the removal of a class file with path in codeLocationFromClass 
 78  
      * 
 79  
      * @param url the file-URL
 80  
      * @return String absolute decoded path
 81  
      * @throws InvalidCodeLocation if URL contains format errors
 82  
      */
 83  
     public static String getPathFromURL(URL url) {
 84  
         URI uri;
 85  
         try {
 86  134
             uri = url.toURI();
 87  0
         } catch (URISyntaxException e) {
 88  
             // this will probably not happen since the url was created
 89  
             // from a filename beforehand
 90  0
             throw new InvalidCodeLocation(e.toString());
 91  134
         }
 92  
 
 93  134
         if(uri.toString().startsWith("file:") || uri.toString().startsWith("jar:")) {
 94  133
             return removeStart(uri.getSchemeSpecificPart(),"file:");
 95  
         } else {
 96  
             // this is wrong, but should at least give a
 97  
             // helpful error when trying to open the file later
 98  1
             return uri.toString();
 99  
         }
 100  
     }
 101  
 
 102  
 }