Coverage Report - org.tap4j.representer.TapJunitRepresenter
 
Classes in this File Line Coverage Branch Coverage Complexity
Statuses
0%
0/24
N/A
1.667
TapJunitRepresenter
0%
0/31
0%
0/20
1.667
 
 1  
 /*
 2  
  * The MIT License
 3  
  *
 4  
  * Copyright (c) <2012> <Bruno P. Kinoshita>
 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  
 
 29  
 import org.tap4j.model.BailOut;
 30  
 import org.tap4j.model.TapResult;
 31  
 import org.tap4j.model.TestResult;
 32  
 import org.tap4j.model.TestSet;
 33  
 import org.tap4j.util.DirectiveValues;
 34  
 import org.tap4j.util.StatusValues;
 35  
 
 36  
 
 37  
 /**
 38  
  * TAP JUnit representer. Outputs Junit XML.
 39  
  * 
 40  
  * @since 3.1
 41  
  */
 42  
 public class TapJunitRepresenter implements Representer {
 43  
 
 44  
     private String name;
 45  
 
 46  0
     public TapJunitRepresenter(String name) {
 47  0
         this.name = name;
 48  0
     }
 49  
 
 50  
     /*
 51  
      * (non-Javadoc)
 52  
      * @see
 53  
      * org.tap4j.representer.Representer#representData(org.tap4j.model.TestSet)
 54  
      */
 55  
     public String representData(TestSet testSet) {
 56  0
         StringWriter sw = new StringWriter();
 57  0
         PrintWriter pw = new PrintWriter(sw);
 58  0
         pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); // TBD: get
 59  
                                                                    // encoding
 60  
                                                                    // from
 61  
                                                                    // dumper
 62  
                                                                    // options
 63  0
         Statuses statuses = this.getStatuses(testSet);
 64  0
         pw.println("<testsuite failures=\"" + statuses.getFailures() + "\" " +
 65  
                    "time=\"" + statuses.getTime() + "\" errors=\"" +
 66  
                    statuses.getErrors() + "\" " + "skipped=\"" +
 67  
                    statuses.getSkipped() + "\" tests=\"" + statuses.getTests() +
 68  
                    "\" " + "name=\"" + this.name + "\">");
 69  
         // TBD: output TAP header, TAP plan as properties
 70  0
         for (TapResult tapLine : testSet.getTapLines()) {
 71  0
             if (tapLine instanceof TestResult) {
 72  0
                 pw.println("<testcase time=\"0\" classname=\"" + this.name +
 73  
                            "\" name=\"" +
 74  
                            ((TestResult) tapLine).getDescription() + "\">");
 75  0
                 if (((TestResult) tapLine).getDirective() != null &&
 76  
                     ((TestResult) tapLine).getDirective().getDirectiveValue() == DirectiveValues.SKIP) {
 77  0
                     pw.println("<skipped/>");
 78  
                 }
 79  0
                 if (((TestResult) tapLine).getStatus() == StatusValues.NOT_OK) {
 80  0
                     pw.println("<failure message=\"" +
 81  
                                ((TestResult) tapLine).getDescription() +
 82  
                                "\" type=\"Failure\" />");
 83  
                 }
 84  0
                 pw.println("</testcase>");
 85  
             }
 86  0
             if (tapLine instanceof BailOut) {
 87  0
                 pw.println("<testcase time=\"0\" classname=\"" + this.name +
 88  
                            "\" name=\"" + ((BailOut) tapLine).getReason() +
 89  
                            "\">");
 90  0
                 pw.println("<error message=\"" +
 91  
                            ((BailOut) tapLine).getReason() +
 92  
                            "\" type=\"BailOut\"/>");
 93  0
                 pw.println("</testcase>");
 94  
             }
 95  
         }
 96  0
         pw.println("</testsuite>");
 97  0
         return sw.toString();
 98  
     }
 99  
 
 100  
     /**
 101  
      * @param testSet
 102  
      * @return Statuses
 103  
      */
 104  
     private Statuses getStatuses(TestSet testSet) {
 105  0
         Statuses statuses = new Statuses();
 106  0
         statuses.setErrors(testSet.getBailOuts().size());
 107  0
         for (TestResult tr : testSet.getTestResults()) {
 108  0
             statuses.setTests(statuses.getTests() + 1);
 109  0
             if (tr.getDirective() != null &&
 110  
                 tr.getDirective().getDirectiveValue() == DirectiveValues.SKIP) {
 111  0
                 statuses.setSkipped(statuses.getSkipped() + 1);
 112  0
             } else if (tr.getStatus() == StatusValues.NOT_OK) {
 113  0
                 statuses.setFailures(statuses.getFailures() + 1);
 114  
             }
 115  
         }
 116  0
         return statuses;
 117  
     }
 118  
 
 119  
 }
 120  
 
 121  
 /**
 122  
  * Helper class for Junit report.
 123  
  * 
 124  
  * @author Bruno P. Kinoshita - http://www.kinoshita.eti.br
 125  
  * @since 3.1
 126  
  */
 127  
 class Statuses {
 128  
 
 129  
     private int errors;
 130  
 
 131  
     private int failures;
 132  
 
 133  
     private int skipped;
 134  
 
 135  
     private int tests;
 136  
 
 137  
     private double time;
 138  
 
 139  
     /**
 140  
      * 
 141  
      */
 142  
     public Statuses() {
 143  0
         super();
 144  0
     }
 145  
 
 146  
     /**
 147  
      * @param errors
 148  
      * @param failures
 149  
      * @param skipped
 150  
      * @param tests
 151  
      * @param time
 152  
      */
 153  
     public Statuses(int errors, int failures, int skipped, int tests,
 154  
                     double time) {
 155  0
         super();
 156  0
         this.errors = errors;
 157  0
         this.failures = failures;
 158  0
         this.skipped = skipped;
 159  0
         this.tests = tests;
 160  0
         this.time = time;
 161  0
     }
 162  
 
 163  
     /**
 164  
      * @return the errors
 165  
      */
 166  
     public int getErrors() {
 167  0
         return errors;
 168  
     }
 169  
 
 170  
     /**
 171  
      * @param errors the errors to set
 172  
      */
 173  
     public void setErrors(int errors) {
 174  0
         this.errors = errors;
 175  0
     }
 176  
 
 177  
     /**
 178  
      * @return the failures
 179  
      */
 180  
     public int getFailures() {
 181  0
         return failures;
 182  
     }
 183  
 
 184  
     /**
 185  
      * @param failures the failures to set
 186  
      */
 187  
     public void setFailures(int failures) {
 188  0
         this.failures = failures;
 189  0
     }
 190  
 
 191  
     /**
 192  
      * @return the skipped
 193  
      */
 194  
     public int getSkipped() {
 195  0
         return skipped;
 196  
     }
 197  
 
 198  
     /**
 199  
      * @param skipped the skipped to set
 200  
      */
 201  
     public void setSkipped(int skipped) {
 202  0
         this.skipped = skipped;
 203  0
     }
 204  
 
 205  
     /**
 206  
      * @return the tests
 207  
      */
 208  
     public int getTests() {
 209  0
         return tests;
 210  
     }
 211  
 
 212  
     /**
 213  
      * @param tests the tests to set
 214  
      */
 215  
     public void setTests(int tests) {
 216  0
         this.tests = tests;
 217  0
     }
 218  
 
 219  
     /**
 220  
      * @return the time
 221  
      */
 222  
     public double getTime() {
 223  0
         return time;
 224  
     }
 225  
 
 226  
     /**
 227  
      * @param time the time to set
 228  
      */
 229  
     public void setTime(double time) {
 230  0
         this.time = time;
 231  0
     }
 232  
 
 233  
 }