Coverage Report - org.jbehave.core.model.ExamplesTableFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ExamplesTableFactory
72%
24/33
100%
4/4
1.167
 
 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 in the ExamplesTable, i.e.
 25  
  * invoking {@link ExamplesTable#getRowAsParameters(int)}, will need to use a
 26  
  * factory constructor providing explicitly the ParameterConverters instance
 27  
  * configured in the
 28  
  * {@link Configuration#useParameterConverters(ParameterConverters)}.
 29  
  * </p>
 30  
  */
 31  
 public class ExamplesTableFactory {
 32  
 
 33  
     private Keywords keywords;
 34  
     private final ResourceLoader resourceLoader;
 35  
     private final ParameterConverters parameterConverters;
 36  
     private final TableTransformers tableTransformers;
 37  
 
 38  
     public ExamplesTableFactory() {
 39  9
         this(new LocalizedKeywords());
 40  9
     }
 41  
 
 42  
     public ExamplesTableFactory(Keywords keywords) {
 43  370
         this(keywords, new LoadFromClasspath(), new ParameterConverters(), new TableTransformers());
 44  370
     }
 45  
 
 46  
     public ExamplesTableFactory(ResourceLoader resourceLoader) {
 47  1
         this(new LocalizedKeywords(), resourceLoader, new ParameterConverters(), new TableTransformers());
 48  1
     }
 49  
 
 50  
     public ExamplesTableFactory(ParameterConverters parameterConverters) {
 51  545
         this(new LocalizedKeywords(), new LoadFromClasspath(), parameterConverters, new TableTransformers());
 52  545
     }
 53  
 
 54  
     public ExamplesTableFactory(TableTransformers tableTransformers) {
 55  2
         this(new LocalizedKeywords(), new LoadFromClasspath(), new ParameterConverters(), tableTransformers);
 56  2
     }
 57  
 
 58  
     public ExamplesTableFactory(Keywords keywords, ResourceLoader resourceLoader,
 59  
             ParameterConverters parameterConverters) {
 60  0
         this(keywords, resourceLoader, parameterConverters, new TableTransformers());
 61  0
     }
 62  
 
 63  
     public ExamplesTableFactory(Keywords keywords, ResourceLoader resourceLoader,
 64  918
             ParameterConverters parameterConverters, TableTransformers tableTranformers) {
 65  918
         this.keywords = keywords;
 66  918
         this.resourceLoader = resourceLoader;
 67  918
         this.parameterConverters = parameterConverters;
 68  918
         this.tableTransformers = tableTranformers;
 69  918
     }
 70  
     
 71  0
     public ExamplesTableFactory(Configuration configuration) {
 72  0
             this.keywords = configuration.keywords();
 73  0
             this.resourceLoader = configuration.storyLoader();
 74  0
             this.parameterConverters = configuration.parameterConverters();
 75  0
             this.tableTransformers = new TableTransformers();
 76  0
     }
 77  
 
 78  
     public ExamplesTable createExamplesTable(String input) {
 79  
         String tableAsString;
 80  199
         if (isBlank(input) || isTable(input)) {
 81  198
             tableAsString = input;
 82  
         } else {
 83  1
             tableAsString = resourceLoader.loadResourceAsText(input);
 84  
         }
 85  199
         return new ExamplesTable(tableAsString, keywords.examplesTableHeaderSeparator(),
 86  199
                 keywords.examplesTableValueSeparator(), keywords.examplesTableIgnorableSeparator(),
 87  
                 parameterConverters, tableTransformers);
 88  
     }
 89  
 
 90  
     protected boolean isTable(String input) {
 91  24
         return input.contains(keywords.examplesTableHeaderSeparator());
 92  
     }
 93  
 
 94  
     public void useKeywords(Keywords keywords){
 95  361
             this.keywords = keywords;
 96  361
     }
 97  
 
 98  
         public Keywords keywords() {
 99  0
                 return this.keywords;
 100  
         }
 101  
 }