| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 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 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class Tap13Representer implements Representer { |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 1 | private static final CharSequence LINE_SEPARATOR = "\n"; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private org.tap4j.representer.DumperOptions options; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 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 | |
|
| 78 | 14 | yamlDumperOptions.setLineBreak(LineBreak.getPlatformLineBreak()); |
| 79 | 14 | yamlDumperOptions.setExplicitStart(true); |
| 80 | 14 | yamlDumperOptions.setExplicitEnd(true); |
| 81 | |
|
| 82 | |
|
| 83 | 14 | yaml = new Yaml(yamlDumperOptions); |
| 84 | 14 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public org.tap4j.representer.DumperOptions getOptions() { |
| 90 | 0 | return options; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 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 | |
|
| 112 | |
|
| 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 | |
|
| 127 | |
|
| 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 | |
|
| 170 | |
|
| 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 | |
|
| 188 | |
|
| 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 | |
|
| 205 | |
|
| 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 | |
|
| 231 | |
|
| 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 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
protected void printComment(PrintWriter pw, Comment comment) { |
| 251 | 6 | pw.append("# " + comment.getText()); |
| 252 | 6 | } |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 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 | |
} |