Coverage Report - org.jbehave.core.model.ExamplesTableFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ExamplesTableFactory
100%
18/18
100%
4/4
1.286
 
 1  
 package org.jbehave.core.model;
 2  
 
 3  
 import org.jbehave.core.configuration.Configuration;
 4  
 import org.jbehave.core.configuration.Keywords;
 5  
 import org.jbehave.core.i18n.LocalizedKeywords;
 6  
 import org.jbehave.core.io.LoadFromClasspath;
 7  
 import org.jbehave.core.io.ResourceLoader;
 8  
 import org.jbehave.core.steps.ParameterConverters;
 9  
 
 10  
 import static org.apache.commons.lang.StringUtils.isBlank;
 11  
 
 12  
 /**
 13  
  * Factory that creates instances of ExamplesTable from different type of
 14  
  * inputs:
 15  
  * <ul>
 16  
  * <li>table text input, i.e. any input that contains a
 17  
  * {@link Keywords#examplesTableHeaderSeparator()}</li>
 18  
  * <li>resource path input, the table as text is loaded via the
 19  
  * {@link ResourceLoader} (defaulting to {@link LoadFromClasspath}).</li>
 20  
  * </ul>
 21  
  * Factory also supports optional specification of {@link ParameterConverters}
 22  
  * to allow the ExamplesTable to convert row values. 
 23  
  * <p>
 24  
  * <b>NOTE</b>: Users needing parameter conversion
 25  
  * in the ExamplesTable, i.e. invoking {@link ExamplesTable#getRowAsParameters(int)}, will need
 26  
  * to use a factory constructor providing explicitly the ParameterConverters instance
 27  
  * configured in the {@link Configuration#useParameterConverters(ParameterConverters)}.  
 28  
  * </p>
 29  
  */
 30  
 public class ExamplesTableFactory {
 31  
 
 32  
     private final Keywords keywords;
 33  
     private final ResourceLoader resourceLoader;
 34  
     private final ParameterConverters parameterConverters;
 35  
 
 36  
     public ExamplesTableFactory() {
 37  487
         this(new LocalizedKeywords());
 38  487
     }
 39  
 
 40  
     public ExamplesTableFactory(Keywords keywords) {
 41  487
         this(keywords, new LoadFromClasspath(), new ParameterConverters());
 42  487
     }
 43  
 
 44  
     public ExamplesTableFactory(ParameterConverters parameterConverters) {
 45  794
         this(new LocalizedKeywords(), new LoadFromClasspath(), parameterConverters);
 46  794
     }
 47  
 
 48  
     public ExamplesTableFactory(ResourceLoader resourceLoader) {
 49  1
         this(new LocalizedKeywords(), resourceLoader, new ParameterConverters());
 50  1
     }
 51  
 
 52  
     public ExamplesTableFactory(Keywords keywords, ResourceLoader resourceLoader,
 53  1282
             ParameterConverters parameterConverters) {
 54  1282
         this.keywords = keywords;
 55  1282
         this.resourceLoader = resourceLoader;
 56  1282
         this.parameterConverters = parameterConverters;
 57  1282
     }
 58  
 
 59  
     public ExamplesTable createExamplesTable(String input) {
 60  
         String tableAsString;
 61  127
         if (isBlank(input) || isTable(input)) {
 62  126
             tableAsString = input;
 63  
         } else {
 64  1
             tableAsString = resourceLoader.loadResourceAsText(input);
 65  
         }
 66  127
         return new ExamplesTable(tableAsString, keywords.examplesTableHeaderSeparator(),
 67  
                 keywords.examplesTableValueSeparator(), keywords.examplesTableIgnorableSeparator(), parameterConverters);
 68  
     }
 69  
 
 70  
     protected boolean isTable(String input) {
 71  9
         return input.contains(keywords.examplesTableHeaderSeparator());
 72  
     }
 73  
 
 74  
 }