Coverage Report - org.tap4j.representer.Tap13Representer
 
Classes in this File Line Coverage Branch Coverage Complexity
Tap13Representer
87%
97/111
74%
37/50
3
 
 1  
 /*
 2  
  * The MIT License
 3  
  *
 4  
  * Copyright (c) <2010> <tap4j>
 5  
  * 
 6  
  * Permission is hereby granted, free of charge, to any person obtaining a copy
 7  
  * of this software and associated documentation files (the "Software"), to deal
 8  
  * in the Software without restriction, including without limitation the rights
 9  
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  
  * copies of the Software, and to permit persons to whom the Software is
 11  
  * furnished to do so, subject to the following conditions:
 12  
  * 
 13  
  * The above copyright notice and this permission notice shall be included in
 14  
  * all copies or substantial portions of the Software.
 15  
  * 
 16  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22  
  * THE SOFTWARE.
 23  
  */
 24  
 package org.tap4j.representer;
 25  
 
 26  
 import java.io.PrintWriter;
 27  
 import java.io.StringWriter;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 import org.apache.commons.lang.StringUtils;
 32  
 import org.tap4j.model.BailOut;
 33  
 import org.tap4j.model.Comment;
 34  
 import org.tap4j.model.Footer;
 35  
 import org.tap4j.model.Header;
 36  
 import org.tap4j.model.Plan;
 37  
 import org.tap4j.model.TapElement;
 38  
 import org.tap4j.model.TapResult;
 39  
 import org.tap4j.model.TestResult;
 40  
 import org.tap4j.model.TestSet;
 41  
 import org.yaml.snakeyaml.DumperOptions;
 42  
 import org.yaml.snakeyaml.DumperOptions.LineBreak;
 43  
 import org.yaml.snakeyaml.Yaml;
 44  
 
 45  
 /**
 46  
  * A TAP 13 representer.
 47  
  * 
 48  
  * @since 1.0
 49  
  */
 50  
 public class Tap13Representer implements Representer {
 51  
 
 52  
     /**
 53  
      * Line separator.
 54  
      */
 55  1
     private static final CharSequence LINE_SEPARATOR = "\n";
 56  
 
 57  
     /**
 58  
      * Dumper options.
 59  
      */
 60  
     private org.tap4j.representer.DumperOptions options;
 61  
 
 62  
     /**
 63  
      * YAML parser and emitter.
 64  
      */
 65  
     private Yaml yaml;
 66  
 
 67  
     public Tap13Representer() {
 68  12
         this(new org.tap4j.representer.DumperOptions());
 69  12
     }
 70  
 
 71  
     public Tap13Representer(org.tap4j.representer.DumperOptions options) {
 72  14
         super();
 73  14
         this.options = options;
 74  
 
 75  14
         final DumperOptions yamlDumperOptions = new DumperOptions();
 76  14
         yamlDumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
 77  
         // options.setDefaultScalarStyle(DumperOptions.ScalarStyle.LITERAL);
 78  14
         yamlDumperOptions.setLineBreak(LineBreak.getPlatformLineBreak());
 79  14
         yamlDumperOptions.setExplicitStart(true);
 80  14
         yamlDumperOptions.setExplicitEnd(true);
 81  
         // TBD: set indent is not working on yaml, perhaps we should implement
 82  
         // a representer...
 83  14
         yaml = new Yaml(yamlDumperOptions);
 84  14
     }
 85  
 
 86  
     /**
 87  
      * @return the options
 88  
      */
 89  
     public org.tap4j.representer.DumperOptions getOptions() {
 90  0
         return options;
 91  
     }
 92  
 
 93  
     /*
 94  
      * (non-Javadoc)
 95  
      * @see
 96  
      * org.tap4j.representer.Representer#representData(org.tap4j.model.TestSet)
 97  
      */
 98  
     public String representData(TestSet testSet) {
 99  15
         StringWriter sw = new StringWriter();
 100  15
         PrintWriter pw = new PrintWriter(sw);
 101  15
         printHeader(pw, testSet.getHeader());
 102  15
         printPlan(pw, testSet.getPlan());
 103  14
         for (TapResult tapLine : testSet.getTapLines()) {
 104  124
             printTapLine(pw, tapLine);
 105  
         }
 106  14
         printFooter(pw, testSet.getFooter());
 107  14
         return sw.toString();
 108  
     }
 109  
 
 110  
     /**
 111  
      * @param pw
 112  
      * @param tapLine
 113  
      */
 114  
     protected void printTapLine(PrintWriter pw, TapResult tapLine) {
 115  124
         if (tapLine instanceof BailOut) {
 116  1
             printBailOut(pw, (BailOut) tapLine);
 117  123
         } else if (tapLine instanceof Comment) {
 118  4
             printComment(pw, (Comment) tapLine);
 119  4
             pw.append(LINE_SEPARATOR);
 120  119
         } else if (tapLine instanceof TestResult) {
 121  119
             printTestResult(pw, (TestResult) tapLine);
 122  
         }
 123  124
     }
 124  
 
 125  
     /**
 126  
      * @param pw
 127  
      * @param testResult
 128  
      */
 129  
     protected void printTestResult(PrintWriter pw, TestResult testResult) {
 130  119
         printFiller(pw);
 131  119
         pw.append(testResult.getStatus().toString());
 132  119
         if (testResult.getTestNumber() != null) {
 133  119
             pw.append(' ' + Integer.toString(testResult.getTestNumber()));
 134  
         }
 135  119
         if (StringUtils.isNotBlank(testResult.getDescription())) {
 136  107
             pw.append(' ' + testResult.getDescription());
 137  
         }
 138  119
         if (testResult.getDirective() != null) {
 139  0
             pw.append(" # " +
 140  
                       testResult.getDirective().getDirectiveValue().toString());
 141  0
             if (StringUtils.isNotBlank(testResult.getDirective().getReason())) {
 142  0
                 pw.append(' ' + testResult.getDirective().getReason());
 143  
             }
 144  
         }
 145  119
         List<Comment> comments = testResult.getComments();
 146  119
         if (comments.size() > 0) {
 147  1
             for (Comment comment : comments) {
 148  1
                 if (comment.isInline()) {
 149  0
                     pw.append(' ');
 150  0
                     printComment(pw, comment);
 151  
                 } else {
 152  1
                     pw.append(LINE_SEPARATOR);
 153  1
                     printComment(pw, comment);
 154  
                 }
 155  
             }
 156  
         }
 157  119
         printDiagnostic(pw, testResult);
 158  119
         pw.append(LINE_SEPARATOR);
 159  119
         if (testResult.getSubtest() != null) {
 160  2
             int indent = this.options.getIndent();
 161  2
             int spaces = this.options.getSpaces();
 162  2
             this.options.setIndent(indent + spaces);
 163  2
             pw.append(this.representData(testResult.getSubtest()));
 164  2
             this.options.setIndent(indent);
 165  
         }
 166  119
     }
 167  
 
 168  
     /**
 169  
      * @param pw
 170  
      * @param bailOut
 171  
      */
 172  
     protected void printBailOut(PrintWriter pw, BailOut bailOut) {
 173  1
         printFiller(pw);
 174  1
         pw.append("Bail out!");
 175  1
         if (bailOut.getReason() != null) {
 176  1
             pw.append(' ' + bailOut.getReason());
 177  
         }
 178  1
         if (bailOut.getComment() != null) {
 179  0
             pw.append(' ');
 180  0
             printComment(pw, bailOut.getComment());
 181  
         }
 182  1
         printDiagnostic(pw, bailOut);
 183  1
         pw.append(LINE_SEPARATOR);
 184  1
     }
 185  
 
 186  
     /**
 187  
      * @param pw
 188  
      * @param footer
 189  
      */
 190  
     protected void printFooter(PrintWriter pw, Footer footer) {
 191  14
         if (footer != null) {
 192  3
             printFiller(pw);
 193  3
             pw.append("TAP " + footer.getText());
 194  3
             if (footer.getComment() != null) {
 195  0
                 pw.append(' ');
 196  0
                 printComment(pw, footer.getComment());
 197  
             }
 198  3
             printDiagnostic(pw, footer);
 199  3
             pw.append(LINE_SEPARATOR);
 200  
         }
 201  14
     }
 202  
 
 203  
     /**
 204  
      * @param pw
 205  
      * @param plan
 206  
      */
 207  
     protected void printPlan(PrintWriter pw, Plan plan) {
 208  15
         if (plan != null) {
 209  14
             printFiller(pw);
 210  14
             pw.append(plan.getInitialTestNumber() + ".." +
 211  
                       plan.getLastTestNumber());
 212  14
             if (plan.getSkip() != null) {
 213  0
                 pw.append(" skip ");
 214  0
                 pw.append(plan.getSkip().getReason());
 215  
             }
 216  14
             if (plan.getComment() != null) {
 217  1
                 pw.append(' ');
 218  1
                 this.printComment(pw, plan.getComment());
 219  
             }
 220  14
             printDiagnostic(pw, plan);
 221  14
             pw.append(LINE_SEPARATOR);
 222  
         } else {
 223  1
             if (options.isAllowEmptyTestPlan() == Boolean.FALSE) {
 224  1
                 throw new RepresenterException("Missing required TAP Plan");
 225  
             }
 226  
         }
 227  14
     }
 228  
 
 229  
     /**
 230  
      * @param pw
 231  
      * @param header
 232  
      */
 233  
     protected void printHeader(PrintWriter pw, Header header) {
 234  15
         if (header != null) {
 235  3
             printFiller(pw);
 236  3
             pw.append("TAP version " + header.getVersion());
 237  3
             if (header.getComment() != null) {
 238  0
                 pw.append(' ');
 239  0
                 this.printComment(pw, header.getComment());
 240  
             }
 241  3
             printDiagnostic(pw, header);
 242  3
             pw.append(LINE_SEPARATOR);
 243  
         }
 244  15
     }
 245  
 
 246  
     /**
 247  
      * @param pw
 248  
      * @param comment
 249  
      */
 250  
     protected void printComment(PrintWriter pw, Comment comment) {
 251  6
         pw.append("# " + comment.getText());
 252  6
     }
 253  
 
 254  
     /**
 255  
      * Prints diagnostic of the TAP Element into the Print Writer.
 256  
      * 
 257  
      * @param pw PrintWriter
 258  
      * @param tapElement TAP element
 259  
      */
 260  
     protected void printDiagnostic(PrintWriter pw, TapElement tapElement) {
 261  140
         Map<String, Object> diagnostic = tapElement.getDiagnostic();
 262  140
         if (diagnostic != null && !diagnostic.isEmpty()) {
 263  101
             String diagnosticText = yaml.dump(diagnostic);
 264  101
             diagnosticText = diagnosticText.replaceAll("((?m)^)", "  ");
 265  101
             pw.append(LINE_SEPARATOR);
 266  101
             printFiller(pw);
 267  101
             pw.append(diagnosticText);
 268  
         }
 269  140
     }
 270  
 
 271  
     protected void printFiller(PrintWriter pw) {
 272  241
         if (this.options.getIndent() > 0) {
 273  6
             pw.append(StringUtils.repeat(" ", this.options.getIndent()));
 274  
         }
 275  241
     }
 276  
 
 277  
 }