Skip to content

Commit

Permalink
cobertura is back...
Browse files Browse the repository at this point in the history
minor code changes
  • Loading branch information
johanneszink committed Nov 12, 2014
1 parent 7645715 commit 7b9ed3f
Show file tree
Hide file tree
Showing 24 changed files with 1,303 additions and 921 deletions.
479 changes: 243 additions & 236 deletions pom.xml

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions src/main/java/org/sonar/plugins/scala/ScalaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*/
package org.sonar.plugins.scala;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.sonar.api.Extension;
import org.sonar.api.SonarPlugin;
import org.sonar.plugins.scala.cobertura.CoberturaSensor;
import org.sonar.plugins.scala.colorization.ScalaColorizerFormat;
import org.sonar.plugins.scala.language.Scala;
import org.sonar.plugins.scala.sensor.BaseMetricsSensor;
Expand All @@ -38,14 +38,18 @@
*/
public class ScalaPlugin extends SonarPlugin {

public List<Class<? extends Extension>> getExtensions() {
final List<Class<? extends Extension>> extensions = new ArrayList<Class<? extends Extension>>();
extensions.add(Scala.class);
extensions.add(ScalaColorizerFormat.class);
extensions.add(BaseMetricsSensor.class);
extensions.add(SurefireSensor.class);

@SuppressWarnings({"rawtypes", "unchecked" })
@Override
public List getExtensions() {
return Arrays.asList(

return extensions;
Scala.class,
ScalaColorizerFormat.class,
BaseMetricsSensor.class,
SurefireSensor.class,
CoberturaSensor.class
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2011 - 2014 All contributors
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.scala.cobertura;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.CoverageExtension;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.resources.Project;
import org.sonar.plugins.cobertura.api.AbstractCoberturaParser;
import org.sonar.plugins.cobertura.api.CoberturaUtils;
import org.sonar.plugins.scala.language.Scala;

public class CoberturaSensor implements Sensor, CoverageExtension {
private static final Logger LOG = LoggerFactory.getLogger(CoberturaSensor.class);

private final FileSystem fileSystem;

private static final AbstractCoberturaParser COBERTURA_PARSER = new ScalaCoberturaParser();

public CoberturaSensor(FileSystem fileSystem) {
this.fileSystem = fileSystem;
}

public boolean shouldExecuteOnProject(Project project) {
if(fileSystem.languages().contains(Scala.KEY)){
LOG.info("CoberturaSensor will be executed");
return true;
} else {
LOG.info("CoberturaSensor will NOT be executed");
return false;
}
}

public void analyse(Project project, SensorContext context) {
File report = CoberturaUtils.getReport(project);
if (report != null) {
parseReport(report, context);
}
}

protected void parseReport(File xmlFile, final SensorContext context) {
LOG.info("parsing {}", xmlFile);
COBERTURA_PARSER.parseReport(xmlFile, context);
}

@Override
public String toString() {
return "Scala CoberturaSensor";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2011 - 2014 All contributors
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.scala.cobertura;

import org.sonar.plugins.cobertura.api.AbstractCoberturaParser;
import org.sonar.api.resources.Resource;
import org.sonar.plugins.scala.language.ScalaFile;
import java.io.File;

public class ScalaCoberturaParser extends AbstractCoberturaParser {
@Override
protected Resource getResource(String fileName) {
// TODO update the sbt scct plugin to provide the correct fully qualified class name.
if (fileName.startsWith("src.main.scala."))
fileName = fileName.replace("src.main.scala.", "");
else if (fileName.startsWith("app."))
fileName = fileName.replace("app.", "");
int packageTerminator = fileName.lastIndexOf('.');
if (packageTerminator < 0) {
return new ScalaFile(null, fileName, false);
} else {
String packageName = fileName.substring(0, packageTerminator);
String className = fileName.substring(packageTerminator + 1, fileName.length());
return new ScalaFile(packageName, className, false);
}
}
}
7 changes: 2 additions & 5 deletions src/main/java/org/sonar/plugins/scala/language/Scala.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.sonar.plugins.scala.language;

import org.sonar.api.config.Settings;
import org.sonar.api.resources.AbstractLanguage;

/**
Expand All @@ -33,12 +32,10 @@ public class Scala extends AbstractLanguage {
public static final String KEY = "scala";
public static final String NAME = "Scala";

private final Settings settings;

public static final Scala INSTANCE = new Scala();

public Scala(Settings settings) {
public Scala() {
super(KEY, NAME);
this.settings = settings;
}

public String[] getFileSuffixes() {
Expand Down
167 changes: 167 additions & 0 deletions src/main/java/org/sonar/plugins/scala/language/ScalaFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2011 - 2014 All contributors
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.scala.language;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.WildcardPattern;

/**
* This class implements a Scala source file for Sonar.
*
* @author Felix Müller
* @since 0.1
*/
public class ScalaFile extends Resource {

private final boolean isUnitTest;

private final String filename;

private final String longName;

private final ScalaPackage parent;

public ScalaFile(String packageKey, String className, boolean isUnitTest) {
super();
this.isUnitTest = isUnitTest;
filename = className.trim();

String key;
if (StringUtils.isBlank(packageKey)) {
packageKey = ScalaPackage.DEFAULT_PACKAGE_NAME;
key = new StringBuilder().append(packageKey).append(".").append(this.filename).toString();
longName = filename;
} else {
packageKey = packageKey.trim();
key = new StringBuilder().append(packageKey).append(".").append(this.filename).toString();
longName = key;
}
parent = new ScalaPackage(packageKey);
setKey(key);
}

@Override
public String getName() {
return filename;
}

@Override
public String getLongName() {
return longName;
}

@Override
public String getDescription() {
return null;
}

@Override
public Language getLanguage() {
return Scala.INSTANCE;
}

@Override
public String getScope() {
return Scopes.FILE;
}

@Override
public String getQualifier() {
return isUnitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
}

@Override
public ScalaPackage getParent() {
return parent;
}

@Override
public boolean matchFilePattern(String antPattern) {
final String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, ".");
final WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, ".");
return matcher.match(getKey());
}

public boolean isUnitTest() {
return isUnitTest;
}

/**
* Shortcut for {@link #fromInputFile(InputFile, boolean)} for source files.
*/
public static ScalaFile fromInputFile(InputFile inputFile) {
return ScalaFile.fromInputFile(inputFile, false);
}

/**
* Creates a {@link ScalaFile} from a file in the source directories.
*
* @param inputFile
* the file object with relative path
* @param isUnitTest
* whether it is a unit test file or a source file
* @return the {@link ScalaFile} created if exists, null otherwise
*/
public static ScalaFile fromInputFile(InputFile inputFile, boolean isUnitTest) {
if (inputFile == null || inputFile.getFile() == null || inputFile.getRelativePath() == null) {
return null;
}

String packageName = PackageResolver.resolvePackageNameOfFile(inputFile.getFile().getAbsolutePath());
String className = resolveClassName(inputFile);

if (isPackageObjectInFirstLevelPackage(packageName, className)) {
String lastFolderName = extractLastFolderName(inputFile);
return new ScalaFile(StringUtils.EMPTY, lastFolderName + "." + className, isUnitTest);
}

return new ScalaFile(packageName, className, isUnitTest);
}

private static boolean isPackageObjectInFirstLevelPackage(String packageName, String className) {
return "<empty>".equalsIgnoreCase(packageName) && "package".equalsIgnoreCase(className);
}

private static String extractLastFolderName(InputFile inputFile) {
String absolutePath = inputFile.getFile().getAbsolutePath();
int lastPathSeparator = absolutePath.lastIndexOf("/");
return absolutePath.substring(absolutePath.lastIndexOf("/", lastPathSeparator - 1) + 1, lastPathSeparator);
}

private static String resolveClassName(InputFile inputFile) {
String classname = inputFile.getRelativePath();
if (inputFile.getRelativePath().indexOf('/') >= 0) {
classname = StringUtils.substringAfterLast(inputFile.getRelativePath(), "/");
}
return StringUtils.substringBeforeLast(classname, ".");
}

@Override
public String toString() {
return new ToStringBuilder(this).append("key", getKey()).append("fileName", filename)
.append("isUnitTest", isUnitTest).append("longName", longName).append("parent", parent).toString();
}
}
Loading

0 comments on commit 7b9ed3f

Please sign in to comment.