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.api.batch.sensor.test.TestCaseExecution;
034    import org.sonar.xoo.Xoo;
035    
036    import java.io.File;
037    import java.io.IOException;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * Parse files *.xoo.testplan
043     */
044    public class TestCaseSensor implements Sensor {
045    
046      private static final Logger LOG = LoggerFactory.getLogger(TestCaseSensor.class);
047    
048      private static final String TESTPLAN_EXTENSION = ".testplan";
049    
050      private void processFileTestPlan(InputFile inputFile, SensorContext context) {
051        File ioFile = inputFile.file();
052        File testPlanFile = new File(ioFile.getParentFile(), ioFile.getName() + TESTPLAN_EXTENSION);
053        if (testPlanFile.exists()) {
054          LOG.debug("Processing " + testPlanFile.getAbsolutePath());
055          try {
056            List<String> lines = FileUtils.readLines(testPlanFile, 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(testPlanFile, lineNumber, line, context, inputFile);
064            }
065          } catch (IOException e) {
066            throw new IllegalStateException(e);
067          }
068        }
069      }
070    
071      private void processLine(File testplanFile, int lineNumber, String line, SensorContext context, InputFile testFile) {
072        try {
073          Iterator<String> split = Splitter.on(":").split(line).iterator();
074          String name = split.next();
075          String type = split.next();
076          String status = split.next();
077          String message = split.next();
078          String stack = split.next();
079          String durationStr = StringUtils.trimToNull(split.next());
080          TestCaseExecution test = context.newTestCaseExecution()
081            .inTestFile(testFile)
082            .name(name)
083            .ofType(TestCaseExecution.Type.valueOf(type))
084            .status(TestCaseExecution.Status.valueOf(status))
085            .message(StringUtils.trimToNull(message))
086            .stackTrace(StringUtils.trimToNull(stack));
087          if (durationStr != null) {
088            test.durationInMs(Long.parseLong(durationStr));
089          }
090          test.save();
091        } catch (Exception e) {
092          throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testplanFile.getAbsolutePath(), e);
093        }
094      }
095    
096      @Override
097      public void describe(SensorDescriptor descriptor) {
098        descriptor
099          .name("Xoo TestPlan Sensor")
100          .workOnLanguages(Xoo.KEY)
101          .workOnFileTypes(InputFile.Type.TEST);
102      }
103    
104      @Override
105      public void execute(SensorContext context) {
106        FileSystem fs = context.fileSystem();
107        FilePredicates p = fs.predicates();
108        for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.TEST)))) {
109          processFileTestPlan(file, context);
110        }
111      }
112    }