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