Coverage Report - org.tap4j.producer.TapProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
TapProducer
87%
29/33
N/A
2.667
 
 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.producer;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.IOException;
 28  
 import java.io.Writer;
 29  
 
 30  
 import org.apache.commons.io.FileUtils;
 31  
 import org.tap4j.model.TestSet;
 32  
 import org.tap4j.representer.Representer;
 33  
 import org.tap4j.representer.RepresenterException;
 34  
 import org.tap4j.representer.Tap13Representer;
 35  
 
 36  
 /**
 37  
  * TAP Producer is responsible for writing the TAP Stream onto some media.
 38  
  * 
 39  
  * @since 1.0
 40  
  */
 41  
 public class TapProducer implements Producer {
 42  
 
 43  
     /**
 44  
      * Represents the TAP Stream.
 45  
      */
 46  
     private Representer representer;
 47  
 
 48  
     /**
 49  
      * Default constructor.
 50  
      */
 51  
     public TapProducer() {
 52  11
         super();
 53  11
         representer = new Tap13Representer();
 54  11
     }
 55  
 
 56  
     /**
 57  
      * Constructor with parameter.
 58  
      * 
 59  
      * @param representer
 60  
      */
 61  3
     public TapProducer(Representer representer) {
 62  3
         this.representer = representer;
 63  3
     }
 64  
 
 65  
     /*
 66  
      * (non-Javadoc)
 67  
      * @see org.tap4j.producer.TapProducer#dump(org.tap4j.model.TestSet)
 68  
      */
 69  
     public String dump(TestSet testSet) {
 70  13
         String dumpData = null;
 71  
 
 72  
         try {
 73  13
             dumpData = this.representer.representData(testSet);
 74  1
         } catch (RepresenterException re) {
 75  1
             throw new ProducerException("Failed to produce test set dump: " +
 76  
                                         re.getMessage(), re);
 77  12
         }
 78  
 
 79  12
         return dumpData;
 80  
     }
 81  
 
 82  
     /*
 83  
      * (non-Javadoc)
 84  
      * @see org.tap4j.producer.TapProducer#dump(org.tap4j.model.TestSet,
 85  
      * java.io.Writer)
 86  
      */
 87  
     public void dump(TestSet testSet, Writer writer) {
 88  3
         String tapStream = null;
 89  
 
 90  
         try {
 91  3
             tapStream = this.dump(testSet);
 92  0
         } catch (RepresenterException re) {
 93  0
             throw new ProducerException("Failed to dump Test Set to writer: " +
 94  
                                         re.getMessage(), re);
 95  3
         }
 96  
 
 97  
         try {
 98  3
             writer.append(tapStream);
 99  1
         } catch (IOException e) {
 100  1
             throw new ProducerException("Failed to dump TAP Stream: " +
 101  
                                         e.getMessage(), e);
 102  1
         }
 103  1
     }
 104  
 
 105  
     /*
 106  
      * (non-Javadoc)
 107  
      * @see org.tap4j.producer.TapProducer#dump(org.tap4j.model.TestSet,
 108  
      * java.io.File)
 109  
      */
 110  
     public void dump(TestSet testSet, File output) {
 111  8
         String tapStream = null;
 112  
 
 113  
         try {
 114  8
             tapStream = this.dump(testSet);
 115  0
         } catch (RepresenterException re) {
 116  0
             throw new ProducerException(
 117  
                                         "Failed to dump Test Set to output file '" +
 118  
                                                 output +
 119  
                                                 "': " +
 120  
                                                 re.getMessage(), re);
 121  7
         }
 122  
 
 123  
         try {
 124  7
             FileUtils.writeStringToFile(output, tapStream);
 125  1
         } catch (IOException e) {
 126  1
             throw new ProducerException("Failed to dump TAP Stream: " +
 127  
                                         e.getMessage(), e);
 128  6
         }
 129  6
     }
 130  
 
 131  
     /*
 132  
      * (non-Javadoc)
 133  
      * @see org.tap4j.producer.TapProducer#getRepresenter()
 134  
      */
 135  
     public Representer getRepresenter() {
 136  2
         return this.representer;
 137  
     }
 138  
 
 139  
 }