Coverage Report - org.jbehave.core.io.LoadFromRelativeFile
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadFromRelativeFile
72%
24/33
50%
4/8
2
LoadFromRelativeFile$StoryFilePath
100%
5/5
N/A
2
 
 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  18
 public class LoadFromRelativeFile implements ResourceLoader, StoryLoader {
 37  
 
 38  
     private final StoryFilePath[] traversals;
 39  
     private final URL location;
 40  
 
 41  
     public LoadFromRelativeFile(URL location) {
 42  5
         this(location, mavenModuleStoryFilePath("src/test/java"));
 43  5
     }
 44  
 
 45  7
     public LoadFromRelativeFile(URL location, StoryFilePath... traversals) {
 46  7
         this.traversals = traversals;
 47  7
         this.location = location;
 48  7
     }
 49  
     
 50  
     public String loadResourceAsText(String resourcePath) {
 51  0
         List<String> traversalPaths = new ArrayList<String>();
 52  0
         String locationPath = normalise(new File(CodeLocations.getPathFromURL(location)).getAbsolutePath());
 53  0
         for (StoryFilePath traversal : traversals) {
 54  0
             String filePath = locationPath.replace(traversal.toRemove, traversal.relativePath) + "/" + resourcePath;
 55  0
             File file = new File(filePath);
 56  0
             if (file.exists()) {
 57  0
                 return loadContent(filePath);
 58  
             } else {
 59  0
                 traversalPaths.add(filePath);
 60  
             }
 61  
         }
 62  0
         throw new StoryResourceNotFound(resourcePath, traversalPaths);
 63  
     }
 64  
 
 65  
     public String loadStoryAsText(String storyPath) {
 66  6
         List<String> traversalPaths = new ArrayList<String>();
 67  6
         String locationPath = new File(CodeLocations.getPathFromURL(location)).getAbsolutePath();
 68  7
         for (StoryFilePath traversal : traversals) {
 69  5
             String filePath = locationPath.replace(traversal.toRemove, traversal.relativePath) + "/" + storyPath;
 70  5
             File file = new File(filePath);
 71  5
             if (file.exists()) {
 72  4
                 return loadContent(filePath);
 73  
             } else {
 74  1
                 traversalPaths.add(filePath);
 75  
             }
 76  
         }
 77  2
         throw new StoryResourceNotFound(storyPath, traversalPaths);
 78  
     }
 79  
 
 80  
     protected String loadContent(String path) {
 81  
         try {
 82  5
             return IOUtils.toString(new FileInputStream(new File(path)));
 83  1
         } catch (Exception e) {
 84  1
             throw new InvalidStoryResource(path, e);
 85  
         }
 86  
     }
 87  
 
 88  
     private static String normalise(String path) {
 89  18
         return path.replace('\\', '/');
 90  
     }
 91  
 
 92  
     /**
 93  
      * For use the the varargs constructor of {@link LoadFromRelativeFile}, to
 94  
      * allow a range of possibilities for locating Story file paths
 95  
      */
 96  10
     public static class StoryFilePath {
 97  
         private final String toRemove;
 98  
         private final String relativePath;
 99  
 
 100  9
         public StoryFilePath(String toRemove, String relativePath) {
 101  9
             this.toRemove = normalise(toRemove);
 102  9
             this.relativePath = normalise(relativePath);
 103  9
         }
 104  
 
 105  
     }
 106  
 
 107  
     /**
 108  
      * Maven by default, has its PRODUCTION classes in target/classes. This
 109  
      * story file path is relative to that.
 110  
      * 
 111  
      * @param relativePath
 112  
      *            the path to the stories' base-dir inside the module
 113  
      * @return the resulting StoryFilePath
 114  
      */
 115  
     public static StoryFilePath mavenModuleStoryFilePath(String relativePath) {
 116  6
         return new StoryFilePath("target/classes", relativePath);
 117  
     }
 118  
 
 119  
     /**
 120  
      * Maven by default, has its TEST classes in target/test-classes. This story
 121  
      * file path is relative to that.
 122  
      * 
 123  
      * @param relativePath
 124  
      *            the path to the stories' base-dir inside the module
 125  
      * @return the resulting StoryFilePath
 126  
      */
 127  
     public static StoryFilePath mavenModuleTestStoryFilePath(String relativePath) {
 128  1
         return new StoryFilePath("target/test-classes", relativePath);
 129  
     }
 130  
 
 131  
     /**
 132  
      * Intellij by default, has its PRODUCTION classes in classes/production.
 133  
      * This story file path is relative to that.
 134  
      * 
 135  
      * @param relativePath
 136  
      *            the path to the stories' base-dir inside the module
 137  
      * @return the resulting StoryFilePath
 138  
      */
 139  
     public static StoryFilePath intellijProjectStoryFilePath(String relativePath) {
 140  1
         return new StoryFilePath("classes/production", relativePath);
 141  
     }
 142  
 
 143  
     /**
 144  
      * Intellij by default, has its TEST classes in classes/test. This story
 145  
      * file path is relative to that.
 146  
      * 
 147  
      * @param relativePath
 148  
      *            the path to the stories' base-dir inside the module
 149  
      * @return the resulting StoryFilePath
 150  
      */
 151  
     public static StoryFilePath intellijProjectTestStoryFilePath(String relativePath) {
 152  1
         return new StoryFilePath("classes/test", relativePath);
 153  
     }
 154  
 
 155  
 }