Coverage Report - org.jbehave.core.io.CodeLocations
 
Classes in this File Line Coverage Branch Coverage Complexity
CodeLocations
100%
12/12
N/A
2.5
CodeLocations$InvalidCodeLocation
100%
2/2
N/A
2.5
 
 1  
 package org.jbehave.core.io;
 2  
 
 3  
 import static org.apache.commons.lang.StringUtils.removeEnd;
 4  
 
 5  
 import java.io.File;
 6  
 import java.net.URL;
 7  
 
 8  
 /**
 9  
  * Collection of utility methods to create code location URLs
 10  
  */
 11  1
 public class CodeLocations {
 12  
 
 13  
     /**
 14  
      * Creates a code location URL from a class
 15  
      * 
 16  
      * @param codeLocationClass the class
 17  
      * @return A URL created from Class
 18  
      * @throws InvalidCodeLocation if URL creation fails
 19  
      */
 20  
     public static URL codeLocationFromClass(Class<?> codeLocationClass) {
 21  25
         String pathOfClass = codeLocationClass.getName().replace(".", "/") + ".class";
 22  25
         URL classResource = codeLocationClass.getClassLoader().getResource(pathOfClass);
 23  25
         String codeLocationPath = removeEnd(classResource.getFile(), pathOfClass);
 24  25
         return codeLocationFromPath(codeLocationPath);
 25  
     }
 26  
 
 27  
     /**
 28  
      * Creates a code location URL from a file path
 29  
      * 
 30  
      * @param filePath the file path
 31  
      * @return A URL created from File
 32  
      * @throws InvalidCodeLocation if URL creation fails
 33  
      */
 34  
     public static URL codeLocationFromPath(String filePath) {
 35  
         try {
 36  242
             return new File(filePath).toURL();
 37  1
         } catch (Exception e) {
 38  1
             throw new InvalidCodeLocation(filePath);
 39  
         }
 40  
     }
 41  
 
 42  
     /**
 43  
      * Creates a code location URL from a URL
 44  
      * 
 45  
      * @param url the URL external form
 46  
      * @return A URL created from URL
 47  
      * @throws InvalidCodeLocation if URL creation fails
 48  
      */
 49  
     public static URL codeLocationFromURL(String url) {
 50  
         try {
 51  3
             return new URL(url);
 52  1
         } catch (Exception e) {
 53  1
             throw new InvalidCodeLocation(url);
 54  
         }
 55  
     }
 56  
 
 57  1
     @SuppressWarnings("serial")
 58  
     public static class InvalidCodeLocation extends RuntimeException {
 59  
 
 60  
         public InvalidCodeLocation(String path) {
 61  2
             super(path);
 62  2
         }
 63  
 
 64  
     }
 65  
 
 66  
 }