Coverage Report - org.tap4j.representer.Tap13Representer
 
Classes in this File Line Coverage Branch Coverage Complexity
Tap13Representer
0%
0/119
0%
0/58
3.308
 
 1  
 /*
 2  
  * The MIT License
 3  
  *
 4  
  * Copyright (c) 2010 tap4j team (see AUTHORS)
 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.tap4j.model.BailOut;
 32  
 import org.tap4j.model.Comment;
 33  
 import org.tap4j.model.Footer;
 34  
 import org.tap4j.model.Header;
 35  
 import org.tap4j.model.Plan;
 36  
 import org.tap4j.model.TapElement;
 37  
 import org.tap4j.model.TestResult;
 38  
 import org.tap4j.model.TestSet;
 39  
 import org.yaml.snakeyaml.DumperOptions;
 40  
 import org.yaml.snakeyaml.DumperOptions.LineBreak;
 41  
 import org.yaml.snakeyaml.Yaml;
 42  
 
 43  
 /**
 44  
  * A TAP 13 representer.
 45  
  *
 46  
  * @since 1.0
 47  
  */
 48  
 public class Tap13Representer implements Representer {
 49  
 
 50  
     /**
 51  
      * Line separator.
 52  
      */
 53  0
     private static final CharSequence LINE_SEPARATOR = "\n";
 54  
 
 55  
     /**
 56  
      * Dumper options.
 57  
      */
 58  
     private org.tap4j.representer.DumperOptions options;
 59  
 
 60  
     /**
 61  
      * YAML parser and emitter.
 62  
      */
 63  0
     private Yaml yaml = null;
 64  
 
 65  
     /**
 66  
      * Default constructor.
 67  
      */
 68  
     public Tap13Representer() {
 69  0
         this(new org.tap4j.representer.DumperOptions());
 70  0
     }
 71  
 
 72  
     /**
 73  
      * @param options Dumper options
 74  
      */
 75  
     public Tap13Representer(org.tap4j.representer.DumperOptions options) {
 76  0
         super();
 77  0
         this.options = options;
 78  0
         if (options.isPrintDiagnostics()) {
 79  0
             final DumperOptions yamlDumperOptions = new DumperOptions();
 80  0
             yamlDumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
 81  0
             yamlDumperOptions.setLineBreak(LineBreak.getPlatformLineBreak());
 82  0
             yamlDumperOptions.setExplicitStart(true);
 83  0
             yamlDumperOptions.setExplicitEnd(true);
 84  0
             yaml = new Yaml(yamlDumperOptions);
 85  
         }
 86  0
     }
 87  
 
 88  
     /**
 89  
      * @return the options
 90  
      */
 91  
     public org.tap4j.representer.DumperOptions getOptions() {
 92  0
         return options;
 93  
     }
 94  
 
 95  
     /**
 96  
      * {@inheritDoc}
 97  
      */
 98  
     public String representData(TestSet testSet) {
 99  0
         StringWriter sw = new StringWriter();
 100  0
         PrintWriter pw = new PrintWriter(sw);
 101  0
         printHeader(pw, testSet.getHeader());
 102  0
         printPlan(pw, testSet.getPlan());
 103  0
         for (TapElement tapLine : testSet.getTapLines()) {
 104  0
             printTapLine(pw, tapLine);
 105  0
         }
 106  0
         printFooter(pw, testSet.getFooter());
 107  0
         return sw.toString();
 108  
     }
 109  
 
 110  
     /**
 111  
      * @param pw Print Writer
 112  
      * @param tapResult TAP test result
 113  
      */
 114  
     protected void printTapLine(PrintWriter pw, TapElement tapResult) {
 115  0
         if (tapResult instanceof BailOut) {
 116  0
             printBailOut(pw, (BailOut) tapResult);
 117  0
         } else if (tapResult instanceof Comment) {
 118  0
             printComment(pw, (Comment) tapResult);
 119  0
             pw.append(LINE_SEPARATOR);
 120  0
         } else if (tapResult instanceof TestResult) {
 121  0
             printTestResult(pw, (TestResult) tapResult);
 122  
         }
 123  0
     }
 124  
 
 125  
     /**
 126  
      * @param pw Print Writer
 127  
      * @param testResult TAP test result
 128  
      */
 129  
     protected void printTestResult(PrintWriter pw, TestResult testResult) {
 130  0
         if (testResult.getSubtest() != null) {
 131  0
             int indent = this.options.getIndent();
 132  0
             int spaces = this.options.getSpaces();
 133  0
             this.options.setIndent(indent + spaces);
 134  0
             pw.append(this.representData(testResult.getSubtest()));
 135  0
             this.options.setIndent(indent);
 136  
         }
 137  0
         printFiller(pw);
 138  0
         pw.append(testResult.getStatus().toString());
 139  0
         pw.append(' ' + Integer.toString(testResult.getTestNumber()));
 140  0
         if (testResult.getDescription() != null && testResult.getDescription().trim().length() > 0) {
 141  0
             pw.append(' ' + testResult.getDescription());
 142  
         }
 143  0
         if (testResult.getDirective() != null) {
 144  0
             pw.append(" # "
 145  0
                     + testResult.getDirective().getDirectiveValue().toString());
 146  0
             String reason = testResult.getDirective().getReason();
 147  0
             if (reason != null && reason.trim().length() > 0) {
 148  0
                 pw.append(' ' + testResult.getDirective().getReason());
 149  
             }
 150  
         }
 151  0
         List<Comment> comments = testResult.getComments();
 152  0
         if (comments.size() > 0) {
 153  0
             for (Comment comment : comments) {
 154  0
                 if (comment.isInline()) {
 155  0
                     pw.append(' ');
 156  0
                     printComment(pw, comment);
 157  
                 } else {
 158  0
                     pw.append(LINE_SEPARATOR);
 159  0
                     printComment(pw, comment);
 160  
                 }
 161  0
             }
 162  
         }
 163  0
         printDiagnostic(pw, testResult);
 164  0
         pw.append(LINE_SEPARATOR);
 165  0
     }
 166  
 
 167  
     /**
 168  
      * @param pw Print Writer
 169  
      * @param bailOut Bail Out!
 170  
      */
 171  
     protected void printBailOut(PrintWriter pw, BailOut bailOut) {
 172  0
         printFiller(pw);
 173  0
         pw.append("Bail out!");
 174  0
         if (bailOut.getReason() != null) {
 175  0
             pw.append(' ' + bailOut.getReason());
 176  
         }
 177  0
         if (bailOut.getComment() != null) {
 178  0
             pw.append(' ');
 179  0
             printComment(pw, bailOut.getComment());
 180  
         }
 181  0
         printDiagnostic(pw, bailOut);
 182  0
         pw.append(LINE_SEPARATOR);
 183  0
     }
 184  
 
 185  
     /**
 186  
      * @param pw Print Writer
 187  
      * @param footer Footer
 188  
      */
 189  
     protected void printFooter(PrintWriter pw, Footer footer) {
 190  0
         if (footer != null) {
 191  0
             printFiller(pw);
 192  0
             pw.append("TAP " + footer.getText());
 193  0
             if (footer.getComment() != null) {
 194  0
                 pw.append(' ');
 195  0
                 printComment(pw, footer.getComment());
 196  
             }
 197  0
             printDiagnostic(pw, footer);
 198  0
             pw.append(LINE_SEPARATOR);
 199  
         }
 200  0
     }
 201  
 
 202  
     /**
 203  
      * @param pw Print Writer
 204  
      * @param plan Plan
 205  
      */
 206  
     protected void printPlan(PrintWriter pw, Plan plan) {
 207  0
         if (plan != null) {
 208  0
             printFiller(pw);
 209  0
             pw.append(plan.getInitialTestNumber()
 210  
                     + ".."
 211  0
                     + plan.getLastTestNumber());
 212  0
             if (plan.getSkip() != null) {
 213  0
                 pw.append(" skip ");
 214  0
                 pw.append(plan.getSkip().getReason());
 215  
             }
 216  0
             if (plan.getComment() != null) {
 217  0
                 pw.append(' ');
 218  0
                 this.printComment(pw, plan.getComment());
 219  
             }
 220  0
             printDiagnostic(pw, plan);
 221  0
             pw.append(LINE_SEPARATOR);
 222  
         } else {
 223  0
             if (options.isAllowEmptyTestPlan() == Boolean.FALSE) {
 224  0
                 throw new RepresenterException("Missing required TAP Plan");
 225  
             }
 226  
         }
 227  0
     }
 228  
 
 229  
     /**
 230  
      * @param pw Print Writer
 231  
      * @param header Header
 232  
      */
 233  
     protected void printHeader(PrintWriter pw, Header header) {
 234  0
         if (header != null) {
 235  0
             printFiller(pw);
 236  0
             pw.append("TAP version " + header.getVersion());
 237  0
             if (header.getComment() != null) {
 238  0
                 pw.append(' ');
 239  0
                 this.printComment(pw, header.getComment());
 240  
             }
 241  0
             printDiagnostic(pw, header);
 242  0
             pw.append(LINE_SEPARATOR);
 243  
         }
 244  0
     }
 245  
 
 246  
     /**
 247  
      * @param pw Print Writer
 248  
      * @param comment Comment
 249  
      */
 250  
     protected void printComment(PrintWriter pw, Comment comment) {
 251  0
         pw.append("# " + comment.getText());
 252  0
     }
 253  
 
 254  
     /**
 255  
      * Prints diagnostic of the TAP Element into the Print Writer.
 256  
      *
 257  
      * @param pw Print Writer
 258  
      * @param tapElement TAP element
 259  
      */
 260  
     protected void printDiagnostic(PrintWriter pw, TapElement tapElement) {
 261  0
         if (this.yaml != null) {
 262  0
             Map<String, Object> diagnostic = tapElement.getDiagnostic();
 263  0
             if (diagnostic != null && !diagnostic.isEmpty()) {
 264  0
                 String diagnosticText = yaml.dump(diagnostic);
 265  0
                 diagnosticText = diagnosticText.replaceAll("((?m)^)", "  ");
 266  0
                 pw.append(LINE_SEPARATOR);
 267  0
                 printFiller(pw);
 268  0
                 pw.append(diagnosticText);
 269  
             }
 270  
         }
 271  0
     }
 272  
 
 273  
     /**
 274  
      * Print filler.
 275  
      *
 276  
      * @param pw Print Writer
 277  
      */
 278  
     protected void printFiller(PrintWriter pw) {
 279  0
         if (this.options.getIndent() > 0) {
 280  0
             for (int i = 0; i < options.getIndent(); i++) {
 281  0
                 pw.append(' ');
 282  
             }
 283  
         }
 284  0
     }
 285  
 
 286  
 }