| 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 | | | package org.jcoderz.phoenix.report; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.util.ArrayList; |
| 39 | | | import java.util.HashMap; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.List; |
| 42 | | | import java.util.Map; |
| 43 | | | import java.util.logging.Logger; |
| 44 | | | |
| 45 | | | import javax.xml.bind.JAXBException; |
| 46 | | | |
| 47 | | | import org.jcoderz.phoenix.coverage.jaxb.ClassType; |
| 48 | | | import org.jcoderz.phoenix.coverage.jaxb.Coverage; |
| 49 | | | import org.jcoderz.phoenix.coverage.jaxb.LineType; |
| 50 | | | import org.jcoderz.phoenix.coverage.jaxb.MethodType; |
| 51 | | | import org.jcoderz.phoenix.coverage.jaxb.PackageType; |
| 52 | | | import org.jcoderz.phoenix.report.jaxb.Item; |
| 53 | | | import org.jcoderz.phoenix.report.jaxb.ObjectFactory; |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | @author |
| 59 | | | |
| 60 | | | public class CoberturaReportReader |
| 61 | | | extends AbstractReportReader |
| 62 | | | { |
| 63 | | | |
| 64 | | | public static final String JCOVERAGE_JAXB_CONTEXT_PATH |
| 65 | | | = "org.jcoderz.phoenix.coverage.jaxb"; |
| 66 | | | |
| 67 | 0 | | private static final String CLASSNAME |
| 68 | | | = CoberturaReportReader.class.getName(); |
| 69 | | | |
| 70 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 71 | | | |
| 72 | | | private Coverage mReportDocument; |
| 73 | | | |
| 74 | | | |
| 75 | | | CoberturaReportReader () |
| 76 | | | throws JAXBException |
| 77 | | | { |
| 78 | 0 | | super(JCOVERAGE_JAXB_CONTEXT_PATH); |
| 79 | 0 | | } |
| 80 | | | |
| 81 | | | {@inheritDoc} |
| 82 | | | public final void parse (File f) |
| 83 | | | throws JAXBException |
| 84 | | | { |
| 85 | | | try |
| 86 | | | { |
| 87 | 0 | (1) | mReportDocument = (Coverage) unmarshall(new FileInputStream(f)); |
| 88 | | | } |
| 89 | 0 | | catch (IOException e) |
| 90 | | | { |
| 91 | 0 | (2) | throw new JAXBException("Cannot read JCoverage report", e); |
| 92 | 0 | | } |
| 93 | 0 | | } |
| 94 | | | |
| 95 | | | {@inheritDoc} |
| 96 | | (3) | public final Map getItems () |
| 97 | | | throws JAXBException |
| 98 | | | { |
| 99 | 0 | | final Map itemMap = new HashMap(); |
| 100 | | | |
| 101 | 0 | | final String baseDir = mReportDocument.getSources().getSource().get(0) |
| 102 | | | + File.separator; |
| 103 | | | |
| 104 | 0 | | for (final Iterator pkgIterator = mReportDocument.getPackages() |
| 105 | 0 | | .getPackage().iterator(); pkgIterator.hasNext();) |
| 106 | | | { |
| 107 | 0 | | final PackageType currentPackage = (PackageType) pkgIterator.next(); |
| 108 | 0 | | for (final Iterator clazzIterator |
| 109 | | | = currentPackage.getClasses().getClazzes().iterator(); |
| 110 | 0 | | clazzIterator.hasNext();) |
| 111 | | | { |
| 112 | 0 | | final ClassType clazz = (ClassType) clazzIterator.next(); |
| 113 | 0 | | processClazz(itemMap, baseDir, clazz); |
| 114 | 0 | | } |
| 115 | 0 | | } |
| 116 | | | |
| 117 | 0 | | return itemMap; |
| 118 | | | } |
| 119 | | | |
| 120 | | | private void processClazz (Map itemMap, final String baseDir, |
| 121 | | | final ClassType clazz) |
| 122 | | | throws JAXBException |
| 123 | | | { |
| 124 | 0 | | logger.finer("Processing class '" + clazz.getName() + "'"); |
| 125 | | | |
| 126 | 0 | | final String javaFile = clazzname2Filename(clazz.getName()); |
| 127 | 0 | | final List itemList = new ArrayList(); |
| 128 | | | |
| 129 | 0 | | for (final Iterator methodIterator = clazz.getMethods().getMethod() |
| 130 | 0 | | .iterator(); methodIterator.hasNext();) |
| 131 | | | { |
| 132 | 0 | | final MethodType method = (MethodType) methodIterator.next(); |
| 133 | 0 | | for (final Iterator lineIterator = method.getLines().getLine() |
| 134 | 0 | | .iterator(); lineIterator.hasNext();) |
| 135 | | | { |
| 136 | 0 | | final LineType line = (LineType) lineIterator.next(); |
| 137 | | | |
| 138 | 0 | | final Item item = new ObjectFactory().createItem(); |
| 139 | 0 | | item.setOrigin(Origin.COVERAGE); |
| 140 | 0 | | item.setCounter(line.getHits()); |
| 141 | 0 | | item.setLine(line.getNumber()); |
| 142 | 0 | | item.setSeverity(Severity.COVERAGE); |
| 143 | 0 | (4) | item.setFindingType("coverage"); |
| 144 | | | |
| 145 | 0 | (5) | itemList.add(item); |
| 146 | 0 | | } |
| 147 | 0 | | } |
| 148 | 0 | | final ResourceInfo info = ResourceInfo.lookup( |
| 149 | | | normalizeFileName(baseDir + javaFile)); |
| 150 | | | |
| 151 | 0 | | if (info != null) |
| 152 | | | { |
| 153 | 0 | | if (itemMap.containsKey(info)) |
| 154 | | | { |
| 155 | 0 | | final List l = (List) itemMap.get(info); |
| 156 | 0 | (6) | l.addAll(itemList); |
| 157 | 0 | | } |
| 158 | | | else |
| 159 | | | { |
| 160 | 0 | (7) | itemMap.put(info, itemList); |
| 161 | | | } |
| 162 | | | } |
| 163 | | | else |
| 164 | | | { |
| 165 | 0 | | logger.finer( |
| 166 | | | "Ignoring findings for resource " + baseDir + javaFile); |
| 167 | | | } |
| 168 | 0 | | } |
| 169 | | | |
| 170 | | | private final String clazzname2Filename (String c) |
| 171 | | | { |
| 172 | 0 | | return c.replaceAll("\\.", "/") + ".java"; |
| 173 | | | } |
| 174 | | | |
| 175 | | | } |