Coverage Report - org.jbehave.core.io.LoadFromClasspath
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadFromClasspath
100%
17/17
100%
2/2
1.714
 
 1  
 package org.jbehave.core.io;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.InputStream;
 5  
 
 6  
 import org.apache.commons.lang.builder.ToStringBuilder;
 7  
 import org.apache.commons.lang.builder.ToStringStyle;
 8  
 
 9  
 /**
 10  
  * Loads story resources from classpath
 11  
  */
 12  
 public class LoadFromClasspath implements StoryLoader {
 13  
 
 14  
     protected final ClassLoader classLoader;
 15  
 
 16  
     public LoadFromClasspath() {
 17  1232
         this(Thread.currentThread().getContextClassLoader());
 18  1232
     }
 19  
 
 20  
     public LoadFromClasspath(Class<?> loadFromClass) {
 21  31
         this(loadFromClass.getClassLoader());
 22  31
     }
 23  
 
 24  1277
     public LoadFromClasspath(ClassLoader classLoader) {
 25  1277
         this.classLoader = classLoader;
 26  1277
     }
 27  
 
 28  
     public String loadResourceAsText(String resourcePath) {
 29  48
         InputStream stream = resourceAsStream(resourcePath);
 30  
         try {
 31  47
             return IOUtils.toString(stream, true);
 32  1
         } catch (IOException e) {
 33  1
             throw new InvalidStoryResource(resourcePath, stream, e);
 34  
         }
 35  
     }
 36  
 
 37  
     public String loadStoryAsText(String storyPath) {
 38  48
         return loadResourceAsText(storyPath);
 39  
     }
 40  
 
 41  
     protected InputStream resourceAsStream(String resourcePath) {
 42  48
         InputStream stream = classLoader.getResourceAsStream(resourcePath);
 43  48
         if (stream == null) {
 44  1
             throw new StoryResourceNotFound(resourcePath, classLoader);
 45  
         }
 46  47
         return stream;
 47  
     }
 48  
 
 49  
     @Override
 50  
     public String toString() {
 51  1
         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 52  
     }
 53  
 
 54  
 }