Coverage Report - org.jbehave.core.io.LoadFromRelativeFile
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadFromRelativeFile
100%
23/23
100%
4/4
1.7
LoadFromRelativeFile$StoryFilePath
100%
5/5
N/A
1.7
 
 1  
 package org.jbehave.core.io;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.net.URL;
 6  
 import java.util.ArrayList;
 7  
 import java.util.List;
 8  
 
 9  
 import org.apache.commons.io.IOUtils;
 10  
 
 11  
 /**
 12  
  * Loads story resources from relative file paths that are
 13  
  * traversal to a given location.
 14  
  * 
 15  
  * StoryLoader loader = new
 16  
  * LoadFromRelativeFile(codeLocationFromClass(YourStory.class));
 17  
  * 
 18  
  * By default, it uses traversal directory
 19  
  * 'target/test-classes' with source dir in 'src/test/java'.
 20  
  * 
 21  
  * Other traversal locations can be specified via the varargs constructor:
 22  
  * 
 23  
  * StoryLoader loader = new
 24  
  * LoadFromRelativeFile(codeLocationFromClass(YourStory.class),
 25  
  * mavenModuleTestStoryFilePath("src/test/java"),
 26  
  * intellijProjectTestStoryFilePath("src/test/java"));
 27  
  * 
 28  
  * Convenience methods : {@link LoadFromRelativeFile#mavenModuleStoryFilePath},
 29  
  * {@link LoadFromRelativeFile#mavenModuleTestStoryFilePath}
 30  
  * {@link LoadFromRelativeFile#intellijProjectStoryFilePath}
 31  
  * {@link LoadFromRelativeFile#intellijProjectTestStoryFilePath}
 32  
  * 
 33  
  * @see {@link CodeLocations#codeLocationFromClass(Class)}
 34  
  * 
 35  
  */
 36  
 public class LoadFromRelativeFile implements ResourceLoader, StoryLoader {
 37  
 
 38  
     private final StoryFilePath[] traversals;
 39  
     private final URL location;
 40  
 
 41  
     public LoadFromRelativeFile(URL location) {
 42  3
         this(location, mavenModuleStoryFilePath("src/test/java"));
 43  3
     }
 44  
 
 45  5
     public LoadFromRelativeFile(URL location, StoryFilePath... traversals) {
 46  5
         this.traversals = traversals;
 47  5
         this.location = location;
 48  5
     }
 49  
     
 50  
     public String loadResourceAsText(String resourcePath) {
 51  4
         List<String> traversalPaths = new ArrayList<String>();
 52  4
         String locationPath = new File(location.getFile()).getAbsolutePath();
 53  5
         for (StoryFilePath traversal : traversals) {
 54  3
             String filePath = locationPath.replace(traversal.toRemove, traversal.relativePath) + "/" + resourcePath;
 55  3
             File file = new File(filePath);
 56  3
             if (file.exists()) {
 57  2
                 return loadContent(filePath);
 58  
             } else {
 59  1
                 traversalPaths.add(filePath);
 60  
             }
 61  
         }
 62  2
         throw new StoryResourceNotFound(resourcePath, traversalPaths);
 63  
     }
 64  
 
 65  
     public String loadStoryAsText(String storyPath) {
 66  4
         return loadResourceAsText(storyPath);
 67  
     }
 68  
 
 69  
     protected String loadContent(String path) {
 70  
         try {
 71  3
             return IOUtils.toString(new FileInputStream(new File(path)));
 72  1
         } catch (Exception e) {
 73  1
             throw new InvalidStoryResource(path, e);
 74  
         }
 75  
     }
 76  
 
 77  
     /**
 78  
      * For use the the varargs constructor of {@link LoadFromRelativeFile}, to
 79  
      * allow a range of possibilities for locating Story file paths
 80  
      */
 81  6
     public static class StoryFilePath {
 82  
         private final String toRemove;
 83  
         private final String relativePath;
 84  
 
 85  7
         public StoryFilePath(String toRemove, String relativePath) {
 86  7
             this.toRemove = toRemove.replace('\\', '/');
 87  7
             this.relativePath = relativePath.replace('\\', '/');
 88  7
         }
 89  
     }
 90  
 
 91  
     /**
 92  
      * Maven by default, has its PRODUCTION classes in target/classes. This
 93  
      * story file path is relative to that.
 94  
      * 
 95  
      * @param relativePath
 96  
      *            the path to the stories' base-dir inside the module
 97  
      * @return the resulting StoryFilePath
 98  
      */
 99  
     public static StoryFilePath mavenModuleStoryFilePath(String relativePath) {
 100  4
         return new StoryFilePath("target/classes", relativePath);
 101  
     }
 102  
 
 103  
     /**
 104  
      * Maven by default, has its TEST classes in target/test-classes. This story
 105  
      * file path is relative to that.
 106  
      * 
 107  
      * @param relativePath
 108  
      *            the path to the stories' base-dir inside the module
 109  
      * @return the resulting StoryFilePath
 110  
      */
 111  
     public static StoryFilePath mavenModuleTestStoryFilePath(String relativePath) {
 112  1
         return new StoryFilePath("target/test-classes", relativePath);
 113  
     }
 114  
 
 115  
     /**
 116  
      * Intellij by default, has its PRODUCTION classes in classes/production.
 117  
      * This story file path is relative to that.
 118  
      * 
 119  
      * @param relativePath
 120  
      *            the path to the stories' base-dir inside the module
 121  
      * @return the resulting StoryFilePath
 122  
      */
 123  
     public static StoryFilePath intellijProjectStoryFilePath(String relativePath) {
 124  1
         return new StoryFilePath("classes/production", relativePath);
 125  
     }
 126  
 
 127  
     /**
 128  
      * Intellij by default, has its TEST classes in classes/test. This story
 129  
      * file path is relative to that.
 130  
      * 
 131  
      * @param relativePath
 132  
      *            the path to the stories' base-dir inside the module
 133  
      * @return the resulting StoryFilePath
 134  
      */
 135  
     public static StoryFilePath intellijProjectTestStoryFilePath(String relativePath) {
 136  1
         return new StoryFilePath("classes/test", relativePath);
 137  
     }
 138  
 
 139  
 }