Coverage Report - org.jbehave.core.io.LoadFromRelativeFile
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadFromRelativeFile
70%
22/31
50%
4/8
2.1
LoadFromRelativeFile$StoryFilePath
100%
5/5
N/A
2.1
 
 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  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 = 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  
     /**
 89  
      * For use the the varargs constructor of {@link LoadFromRelativeFile}, to
 90  
      * allow a range of possibilities for locating Story file paths
 91  
      */
 92  10
     public static class StoryFilePath {
 93  
         private final String toRemove;
 94  
         private final String relativePath;
 95  
 
 96  9
         public StoryFilePath(String toRemove, String relativePath) {
 97  9
             this.toRemove = toRemove.replace('\\', '/');
 98  9
             this.relativePath = relativePath.replace('\\', '/');
 99  9
         }
 100  
     }
 101  
 
 102  
     /**
 103  
      * Maven by default, has its PRODUCTION classes in target/classes. This
 104  
      * story file path is relative to that.
 105  
      * 
 106  
      * @param relativePath
 107  
      *            the path to the stories' base-dir inside the module
 108  
      * @return the resulting StoryFilePath
 109  
      */
 110  
     public static StoryFilePath mavenModuleStoryFilePath(String relativePath) {
 111  6
         return new StoryFilePath("target/classes", relativePath);
 112  
     }
 113  
 
 114  
     /**
 115  
      * Maven by default, has its TEST classes in target/test-classes. This story
 116  
      * file path is relative to that.
 117  
      * 
 118  
      * @param relativePath
 119  
      *            the path to the stories' base-dir inside the module
 120  
      * @return the resulting StoryFilePath
 121  
      */
 122  
     public static StoryFilePath mavenModuleTestStoryFilePath(String relativePath) {
 123  1
         return new StoryFilePath("target/test-classes", relativePath);
 124  
     }
 125  
 
 126  
     /**
 127  
      * Intellij by default, has its PRODUCTION classes in classes/production.
 128  
      * This story file path is relative to that.
 129  
      * 
 130  
      * @param relativePath
 131  
      *            the path to the stories' base-dir inside the module
 132  
      * @return the resulting StoryFilePath
 133  
      */
 134  
     public static StoryFilePath intellijProjectStoryFilePath(String relativePath) {
 135  1
         return new StoryFilePath("classes/production", relativePath);
 136  
     }
 137  
 
 138  
     /**
 139  
      * Intellij by default, has its TEST classes in classes/test. This story
 140  
      * file path is relative to that.
 141  
      * 
 142  
      * @param relativePath
 143  
      *            the path to the stories' base-dir inside the module
 144  
      * @return the resulting StoryFilePath
 145  
      */
 146  
     public static StoryFilePath intellijProjectTestStoryFilePath(String relativePath) {
 147  1
         return new StoryFilePath("classes/test", relativePath);
 148  
     }
 149  
 
 150  
 }