1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
// Copyright (c) 2008 Fabien DUMINY (fduminy@jnode.org)
// Modified by Levente S\u00e1ntha (lsantha@jnode.org)
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA. */
package gnu.testlet.runner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
/**
* XML Writer for mauve reports.
*
* @author fabien
*
*/
public class XMLReportWriter implements XMLReportConstants {
private static final String INDENT = " ";
private static final String NOT_APPLIABLE = "n/a";
/**
* Do we write the xml report in compact mode ?
* The compact mode is producing unformatted xml, which means that there
* is no indentations nor carriage returns (except in <code>log</code>
* attributes because they can contain stacktraces).
*/
private final boolean compactMode;
/**
* Constructor to write a formatted xml report.
*/
public XMLReportWriter() {
// by default, not in compact mode
this(false);
}
/**
* Constructor to write an xml report.
* @param compactMode true to write the xml report in compact mode.
*/
public XMLReportWriter(boolean compactMode) {
this.compactMode = compactMode;
}
/**
* Write the given result in xml format.
*
* @param result
* @param output
* @throws FileNotFoundException
*/
public void write(RunResult result, File output) throws FileNotFoundException {
PrintWriter ps = null;
try {
ps = new PrintWriter(new FileOutputStream(output));
write(result, ps);
} finally {
if (ps != null) {
ps.close();
}
}
}
/**
* Write the given result in xml format.
*
* @param result
* @param ps
*/
public void write(RunResult result, PrintWriter ps) {
int level = 0;
runResult(ps, level, result);
level++;
for (Iterator itPackage = result.getPackageIterator(); itPackage.hasNext(); ) {
PackageResult pkg = (PackageResult) itPackage.next();
packageResult(ps, level, pkg);
level++;
for (Iterator itClass = pkg.getClassIterator(); itClass.hasNext(); ) {
ClassResult cls = (ClassResult) itClass.next();
classResult(ps, level, cls);
level++;
for (Iterator itTest = cls.getTestIterator(); itTest.hasNext(); ) {
TestResult test = (TestResult) itTest.next();
test(ps, level, test);
level++;
for (Iterator itCheck = test.getCheckIterator(); itCheck.hasNext(); ) {
CheckResult check = (CheckResult) itCheck.next();
check(ps, level, check);
}
level--;
endTag(ps, level, TEST_RESULT);
}
level--;
endTag(ps, level, CLASS_RESULT);
}
level--;
endTag(ps, level, PACKAGE_RESULT);
}
level--;
endTag(ps, level, RUN_RESULT);
ps.flush();
}
/**
* Write a check tag.
* @param ps
* @param level
* @param check
*/
private void check(PrintWriter ps, int level, CheckResult check) {
String log = getNullIfBlank(check.getLog());
boolean closeTag = (log == null);
beginTag(ps, level, CHECK_RESULT, closeTag, new Object[]{CHECK_NUMBER, Integer.valueOf(check.getNumber()),
CHECK_POINT, check.getCheckPoint(),
CHECK_PASSED, Boolean.valueOf(check.getPassed()),
CHECK_EXPECTED, check.getExpected(),
CHECK_ACTUAL, check.getActual()});
if (!closeTag) {
text(ps, level + 1, CHECK_LOG, log);
endTag(ps, level, CHECK_RESULT);
}
}
/**
* Write a test tag.
* @param ps
* @param level
* @param test
*/
private void test(PrintWriter ps, int level, TestResult test) {
beginTag(ps, level, TEST_RESULT, false, new Object[]{TEST_NAME, test.getName()});
text(ps, level + 1, TEST_ERROR, test.getFailedMessage());
}
/**
* Write a classresult tag.
* @param ps
* @param level
* @param cr
*/
private void classResult(PrintWriter ps, int level, ClassResult cr) {
beginTag(ps, level, CLASS_RESULT, false, new Object[]{CLASS_NAME, cr.getName()});
}
/**
* Write a package result tag.
* @param ps
* @param level
* @param pr
*/
private void packageResult(PrintWriter ps, int level, PackageResult pr) {
beginTag(ps, level, PACKAGE_RESULT, false, new Object[]{PACKAGE_NAME, pr.getName()});
}
/**
* Write a run result tag.
* @param ps
* @param level
* @param rr
*/
private void runResult(PrintWriter ps, int level, RunResult rr) {
beginTag(ps, level, RUN_RESULT, false, new Object[]{RUN_NAME, rr.getName()});
String[] propertyNames = rr.getSystemPropertyNames();
int subLevel = level + 1;
for (int i = 0; i < propertyNames.length; i++) {
String name = propertyNames[i];
String value = rr.getSystemProperty(name);
beginTag(ps, subLevel, PROPERTY, true, new Object[]{PROPERTY_NAME, name, PROPERTY_VALUE, value});
}
}
/**
* Write a tag with the provided content (text parameter), if any.
* @param ps
* @param level
* @param tag
* @param text
* @return
*/
private PrintWriter text(PrintWriter ps, int level, String tag, String text) {
text = getNullIfBlank(text);
if (text != null) {
beginTag(ps, level, tag, false, new Object[0]);
ps.append(protect(text));
appendCarriageReturn(ps);
endTag(ps, level, tag);
}
return ps;
}
/**
* Write the begin of a tag with the given attributes and optionally close the tag.
* @param ps
* @param level The level of indentation of the tag (ignored if {@link XMLReportWriter#compactMode} is true).
* @param tag The name of the tag.
* @param closeTag true to also close the tag.
* @param attributes The attributes of the tag.
* @return
*/
private PrintWriter beginTag(PrintWriter ps, int level, String tag, boolean closeTag, Object[] attributes) {
tag(ps, level, tag, true);
for (int i = 0; i < attributes.length; i += 2) {
String value = getNullIfBlank(attributes[i + 1]);
if (value != null) {
ps.append(' ').append(String.valueOf(attributes[i]));
ps.append("=\"").append(protect(value)).append('\"');
}
}
ps.append(closeTag ? "/>" : ">");
appendCarriageReturn(ps);
return ps;
}
/**
* Replace all characters with the appropriate xml escape sequences when needed.
* @param text
* @return
*/
public static String protect(String text) {
if (text == null) {
return text;
}
final int size = text.length();
final StringBuilder sb = new StringBuilder(size);
boolean changed = false;
for (int i = 0; i < size; i++) {
final char c = text.charAt(i);
switch (c) {
case '&' :
sb.append("&");
changed = true;
break;
case '<' :
sb.append("<");
changed = true;
break;
case '>' :
sb.append(">");
changed = true;
break;
case '\'' :
sb.append("'");
changed = true;
break;
case '"' :
sb.append(""");
changed = true;
break;
default:
sb.append(c);
}
}
return changed ? sb.toString() : text;
}
/**
* Write the end of a tag.
* @param ps
* @param level
* @param tag
* @return
*/
private PrintWriter endTag(PrintWriter ps, int level, String tag) {
return tag(ps, level, tag, false);
}
/**
* Write the end or the begin of a tag.
* @param ps
* @param level
* @param tag
* @param begin
* @return
*/
private PrintWriter tag(PrintWriter ps, int level, String tag, boolean begin) {
indent(ps, level).append(begin ? "<" : "</").append(tag);
if (!begin) {
ps.append('>');
appendCarriageReturn(ps);
}
return ps;
}
/**
* Write a carriage return if {@link XMLReportWriter#compactMode} is false.
* @param pw
* @return
*/
private PrintWriter appendCarriageReturn(PrintWriter pw) {
if (!compactMode) {
pw.append('\n');
}
return pw;
}
/**
* Write an indentation if {@link XMLReportWriter#compactMode} is false.
* @param ps
* @param level
* @return
*/
private PrintWriter indent(PrintWriter ps, int level) {
if (!compactMode) {
for (int i = 0; i < level; i++) {
ps.print(INDENT);
}
}
return ps;
}
/**
* Return null if the given string is blank.
* @param text
* @return
*/
private String getNullIfBlank(Object text) {
String result = null;
if (text != null) {
result = text.toString().trim();
// We assume here that the corresponding attribute
// is defaulted to NOT_APPLIABLE when it's null.
//
// It's the case for CheckResult.getExpected() and
// CheckResult.getActual())
if (result.isEmpty() || NOT_APPLIABLE.equals(result)) {
result = null;
}
}
return result;
}
}
|