001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.xoo.lang;
021
022 import com.google.common.base.Splitter;
023 import org.apache.commons.io.FileUtils;
024 import org.apache.commons.lang.StringUtils;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027 import org.sonar.api.batch.fs.FilePredicates;
028 import org.sonar.api.batch.fs.FileSystem;
029 import org.sonar.api.batch.fs.InputFile;
030 import org.sonar.api.batch.sensor.Sensor;
031 import org.sonar.api.batch.sensor.SensorContext;
032 import org.sonar.api.batch.sensor.SensorDescriptor;
033 import org.sonar.xoo.Xoo;
034
035 import java.io.File;
036 import java.io.IOException;
037 import java.util.ArrayList;
038 import java.util.Iterator;
039 import java.util.List;
040
041 /**
042 * Parse files *.xoo.coveragePerTest
043 */
044 public class CoveragePerTestSensor implements Sensor {
045
046 private static final Logger LOG = LoggerFactory.getLogger(CoveragePerTestSensor.class);
047
048 private static final String COVER_PER_TEST_EXTENSION = ".coveragePerTest";
049
050 private void processCoveragePerTest(InputFile inputFile, SensorContext context) {
051 File ioFile = inputFile.file();
052 File coverPerTestFile = new File(ioFile.getParentFile(), ioFile.getName() + COVER_PER_TEST_EXTENSION);
053 if (coverPerTestFile.exists()) {
054 LOG.debug("Processing " + coverPerTestFile.getAbsolutePath());
055 try {
056 List<String> lines = FileUtils.readLines(coverPerTestFile, context.fileSystem().encoding().name());
057 int lineNumber = 0;
058 for (String line : lines) {
059 lineNumber++;
060 if (StringUtils.isBlank(line) || line.startsWith("#")) {
061 continue;
062 }
063 processLine(coverPerTestFile, lineNumber, context, line, inputFile);
064 }
065 } catch (IOException e) {
066 throw new IllegalStateException(e);
067 }
068 }
069 }
070
071 private void processLine(File coverPerTest, int lineNumber, SensorContext context, String line, InputFile testFile) {
072 try {
073 Iterator<String> split = Splitter.on(":").split(line).iterator();
074 String testCaseName = split.next();
075 String mainFileRelativePath = split.next();
076 FileSystem fs = context.fileSystem();
077 InputFile mainFile = fs.inputFile(fs.predicates().hasRelativePath(mainFileRelativePath));
078 if (mainFile == null) {
079 throw new IllegalStateException("Unable to find file " + mainFileRelativePath);
080 }
081 List<Integer> coveredLines = new ArrayList<Integer>();
082 Iterator<String> lines = Splitter.on(",").split(split.next()).iterator();
083 while (lines.hasNext()) {
084 coveredLines.add(Integer.parseInt(lines.next()));
085 }
086 context.newTestCaseCoverage()
087 .testFile(testFile)
088 .testName(testCaseName)
089 .cover(mainFile)
090 .onLines(coveredLines)
091 .save();
092 } catch (Exception e) {
093 throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverPerTest.getAbsolutePath(), e);
094 }
095 }
096
097 @Override
098 public void describe(SensorDescriptor descriptor) {
099 descriptor
100 .name("Xoo Coverage Per Test Sensor")
101 .workOnLanguages(Xoo.KEY)
102 .workOnFileTypes(InputFile.Type.TEST);
103 }
104
105 @Override
106 public void execute(SensorContext context) {
107 FileSystem fs = context.fileSystem();
108 FilePredicates p = fs.predicates();
109 for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.TEST)))) {
110 processCoveragePerTest(file, context);
111 }
112 }
113 }