1 package net.sf.mavenhist.extractor.surefire;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 import net.sf.mavenhist.extractor.ExtractionException;
9 import net.sf.mavenhist.extractor.IValueExtractor;
10 import net.sf.mavenhist.extractor.MultiMetric;
11
12 /**
13 * We have to use regular expression parsing since there is not TEST-suite.xml any more and the
14 * output format of the site is invalid XML. Only for Maven 2.
15 */
16 public abstract class AbstractTestExtractor implements IValueExtractor {
17
18 private int tableColumnInclSkipped;
19 private int tableColumnWithoutSkipped;
20
21 public AbstractTestExtractor(int tableColumnInclSkipped, int tableColumnWithoutSkipped) {
22 this.tableColumnInclSkipped = tableColumnInclSkipped;
23 this.tableColumnWithoutSkipped = tableColumnWithoutSkipped;
24 }
25
26 /**
27 * {@inheritDoc}
28 */
29 public final MultiMetric[] extract(InputStream inputStream)
30 throws ExtractionException {
31
32 StringBuffer buffer;
33 try {
34 buffer = getString(inputStream);
35 } catch (IOException e) {
36 throw new ExtractionException("Unable to read the given stream.", e);
37 }
38
39 String valueRegExpPattern = "<td>([^<%]+)%?</td>";
40 String skippedRegExpPattern ="<th>Skipped</th>";
41
42 Pattern valuePattern = Pattern.compile(valueRegExpPattern);
43 Matcher valueMatcher = valuePattern.matcher(buffer);
44
45 int numberOfMatches = 0;
46 String valueInclSkipped = null;
47 String valueWithoutSkipped = null;
48 while (valueMatcher.find() && (valueInclSkipped == null || valueWithoutSkipped == null)) {
49 if (tableColumnInclSkipped == numberOfMatches) {
50 valueInclSkipped = valueMatcher.group(1);
51 }
52 if (tableColumnWithoutSkipped == numberOfMatches) {
53 valueWithoutSkipped = valueMatcher.group(1);
54 }
55 numberOfMatches++;
56 }
57
58 Pattern skippedPattern = Pattern.compile(skippedRegExpPattern);
59 Matcher skippedMatcher = skippedPattern.matcher(buffer);
60
61
62
63 if (skippedMatcher.find()) {
64 if (valueInclSkipped != null) {
65 return new MultiMetric[] { new MultiMetric(null, reformatValue(valueInclSkipped)) };
66 }
67 }
68
69
70 if (valueWithoutSkipped != null) {
71 return new MultiMetric[] { new MultiMetric(null, reformatValue(valueWithoutSkipped)) };
72 }
73
74
75 throw new ExtractionException("No value found for extractor: " + getName(), null);
76 }
77
78 /**
79 * Reformats a string if needed. By default returns the input <code>String</code>.
80 * @param toFormat string that will be formatted
81 * @return formatted string
82 */
83 public String reformatValue(String toFormat) {
84 return toFormat;
85 }
86
87 /**
88 * Gets a <code>String</code> from an <code>InputStream</code>.
89 *
90 * @param fileContents
91 * to read
92 * @return filled <code>String</code>
93 * @throws IOException
94 * stream is not readable
95 */
96 private StringBuffer getString(InputStream fileContents) throws IOException {
97 StringBuffer stringBuffer = new StringBuffer();
98 while (true) {
99 int read = fileContents.read();
100 if (read != -1) {
101 stringBuffer.append((char) read);
102 } else {
103 break;
104 }
105 }
106 return stringBuffer;
107 }
108 }