Coverage Report - org.jbehave.core.model.OutcomesTable
 
Classes in this File Line Coverage Branch Coverage Complexity
OutcomesTable
100%
30/30
100%
12/12
1.467
OutcomesTable$Outcome
100%
11/11
N/A
1.467
OutcomesTable$OutcomesFailed
100%
4/4
N/A
1.467
 
 1  
 package org.jbehave.core.model;
 2  
 
 3  
 import static java.util.Arrays.asList;
 4  
 
 5  
 import java.util.ArrayList;
 6  
 import java.util.Iterator;
 7  
 import java.util.List;
 8  
 
 9  
 import org.apache.commons.lang.builder.ToStringBuilder;
 10  
 import org.apache.commons.lang.builder.ToStringStyle;
 11  
 import org.hamcrest.Matcher;
 12  
 
 13  18
 public class OutcomesTable {
 14  
 
 15  1
     private static final List<String> FIELDS = asList("Description", "Value", "Matcher", "Verified");
 16  
     private static final String NEWLINE = "\n";
 17  
     private static final String HEADER_SEPARATOR = "|";
 18  
     private static final String VALUE_SEPARATOR = "|";
 19  
 
 20  18
     private List<Outcome<?>> outcomes = new ArrayList<Outcome<?>>();
 21  18
     private List<Outcome<?>> failedOutcomes = new ArrayList<Outcome<?>>();
 22  
     private OutcomesFailed failureCause;
 23  
 
 24  
     public <T> void addOutcome(String description, T value, Matcher<T> matcher) {
 25  18
         outcomes.add(new Outcome<T>(description, value, matcher));
 26  18
     }
 27  
 
 28  
     public void verify() {
 29  14
         boolean failed = false;
 30  14
         failedOutcomes.clear();
 31  14
         for (Outcome<?> outcome : outcomes) {
 32  16
             if (!outcome.isVerified()) {
 33  13
                 failedOutcomes.add(outcome);
 34  13
                 failed = true;
 35  13
                 break;
 36  
             }
 37  
         }
 38  14
         if (failed) {
 39  13
             failureCause = new OutcomesFailed(this);
 40  13
             throw failureCause;
 41  
         }
 42  1
     }
 43  
 
 44  
     public Throwable failureCause() {
 45  13
         return failureCause;
 46  
     }
 47  
 
 48  
     public List<Outcome<?>> getOutcomes() {
 49  15
         return outcomes;
 50  
     }
 51  
 
 52  
     public List<Outcome<?>> getFailedOutcomes() {
 53  2
         return failedOutcomes;
 54  
     }
 55  
 
 56  
     public List<String> getOutcomeFields() {
 57  12
         return FIELDS;
 58  
     }
 59  
 
 60  
     @Override
 61  
     public String toString() {
 62  2
         StringBuilder sb = new StringBuilder();
 63  2
         for (Iterator<String> iterator = FIELDS.iterator(); iterator.hasNext();) {
 64  8
             sb.append(HEADER_SEPARATOR).append(iterator.next());
 65  8
             if (!iterator.hasNext()) {
 66  2
                 sb.append(HEADER_SEPARATOR).append(NEWLINE);
 67  
             }
 68  
         }
 69  2
         for (Outcome<?> outcome : outcomes) {
 70  2
             sb.append(VALUE_SEPARATOR).append(outcome.getDescription()).append(VALUE_SEPARATOR).append(
 71  
                     outcome.getValue()).append(VALUE_SEPARATOR).append(outcome.getMatcher()).append(VALUE_SEPARATOR)
 72  
                     .append(outcome.isVerified()).append(VALUE_SEPARATOR).append(NEWLINE);
 73  
         }
 74  2
         return sb.toString();
 75  
     }
 76  
 
 77  
     public static class Outcome<T> {
 78  
 
 79  
         private final String description;
 80  
         private final T value;
 81  
         private final Matcher<T> matcher;
 82  
         private final boolean verified;
 83  
 
 84  18
         public Outcome(String description, T value, Matcher<T> matcher) {
 85  18
             this.description = description;
 86  18
             this.value = value;
 87  18
             this.matcher = matcher;
 88  18
             this.verified = matcher.matches(value);
 89  18
         }
 90  
 
 91  
         public String getDescription() {
 92  15
             return description;
 93  
         }
 94  
 
 95  
         public T getValue() {
 96  15
             return value;
 97  
         }
 98  
 
 99  
         public Matcher<T> getMatcher() {
 100  14
             return matcher;
 101  
         }
 102  
 
 103  
         public boolean isVerified() {
 104  42
             return verified;
 105  
         }
 106  
 
 107  
         @Override
 108  
         public String toString() {
 109  2
             return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
 110  
         }
 111  
     }
 112  
 
 113  18
     @SuppressWarnings("serial")
 114  
     public static class OutcomesFailed extends RuntimeException {
 115  
         private OutcomesTable outcomes;
 116  
 
 117  14
         public OutcomesFailed(OutcomesTable outcomes) {
 118  14
             this.outcomes = outcomes;
 119  14
         }
 120  
 
 121  
         public OutcomesTable outcomesTable() {
 122  14
             return outcomes;
 123  
         }
 124  
 
 125  
     }
 126  
 
 127  
 }