-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a sourcesPaths array tag that will add multiple Sources to the … #43
Open
pfrank13
wants to merge
6
commits into
QualInsight:master
Choose a base branch
from
pfrank13:gh-42
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
601961a
Added a sourcesPaths array tag that will add multiple Sources to the …
f65c419
gh #42: working in JAXB to render sonarcube info
ddd423f
gh #42 Added CoberturaToSonarQubeReportConverter abstraction
40e48f6
gh #42, hardcoded impl of JaxbCoberturaToSonarQubeReportConverter
b756018
gh #42 xsd update and inital impl on converter
a68ffad
gh #42 Complete support for multiple sourcesPaths
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...va/com/qualinsight/mojo/cobertura/transformation/CoberturaToSonarQubeReportConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* qualinsight-mojo-cobertura | ||
* Copyright (c) 2015-2017, QualInsight | ||
* http://www.qualinsight.com/ | ||
* | ||
* 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, you can retrieve a copy | ||
* from <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.qualinsight.mojo.cobertura.transformation; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* @author pfrank | ||
*/ | ||
public interface CoberturaToSonarQubeReportConverter { | ||
void convertReport(final String[] sourcesPaths, final String projectPath) throws MojoExecutionException; | ||
} |
150 changes: 150 additions & 0 deletions
150
...om/qualinsight/mojo/cobertura/transformation/JaxbCoberturaToSonarQubeReportConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* qualinsight-mojo-cobertura | ||
* Copyright (c) 2015-2017, QualInsight | ||
* http://www.qualinsight.com/ | ||
* | ||
* 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, you can retrieve a copy | ||
* from <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.qualinsight.mojo.cobertura.transformation; | ||
|
||
import com.qualinsight.mojo.sonar.model.CoverageType; | ||
import com.qualinsight.mojo.sonar.model.FileType; | ||
import com.qualinsight.mojo.sonar.model.LineToCoverType; | ||
import com.qualinsight.mojo.sonar.model.ObjectFactory; | ||
|
||
import net.sourceforge.cobertura.coveragedata.ClassData; | ||
import net.sourceforge.cobertura.coveragedata.CoverageData; | ||
import net.sourceforge.cobertura.coveragedata.LineData; | ||
import net.sourceforge.cobertura.coveragedata.ProjectData; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.logging.Log; | ||
|
||
import java.io.File; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBElement; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Marshaller; | ||
|
||
/** | ||
* @author pfrank | ||
*/ | ||
public class JaxbCoberturaToSonarQubeReportConverter implements CoberturaToSonarQubeReportConverter { | ||
public static final String SONAR_JAXB_PACKAGE = "com.qualinsight.mojo.sonar.model"; | ||
private final Log log; | ||
private final ProjectData projectData; | ||
private final File destinationSonarXmlFile; | ||
private JAXBContext jaxbContext; | ||
|
||
public JaxbCoberturaToSonarQubeReportConverter(final Log log, final ProjectData projectData, final File destinationSonarXmlFile){ | ||
if(log == null){ | ||
throw new IllegalArgumentException("Log cannot be null"); | ||
} | ||
if(projectData == null){ | ||
throw new IllegalArgumentException("ProjectData cannot be null"); | ||
} | ||
if(destinationSonarXmlFile == null){ | ||
throw new IllegalArgumentException("File destinationSonarXmlFile cannot be null."); | ||
} | ||
|
||
this.log = log; | ||
this.projectData = projectData; | ||
this.destinationSonarXmlFile = destinationSonarXmlFile; | ||
try { | ||
this.jaxbContext = JAXBContext.newInstance(SONAR_JAXB_PACKAGE); | ||
}catch(JAXBException je){ | ||
throw new IllegalStateException(je); | ||
} | ||
} | ||
|
||
@Override | ||
public void convertReport( | ||
final String[] sourcesPaths, | ||
final String projectPath) throws MojoExecutionException { | ||
try { | ||
//There is no real lookup but the project doesn't have Tuple or KeyValuePair | ||
final Map<String, String> sonarSourcesPathPrefixMap = new HashMap<String, String>(); | ||
for(String sourcesPath : sourcesPaths){ | ||
sonarSourcesPathPrefixMap.put(sourcesPath, sourcesPath.substring(projectPath.length())); | ||
} | ||
|
||
final Marshaller marshaller = jaxbContext.createMarshaller(); | ||
final ObjectFactory factory = new ObjectFactory(); | ||
|
||
final CoverageType coverageType = new CoverageType(); | ||
coverageType.setVersion(1); | ||
|
||
@SuppressWarnings("unchecked") | ||
final Collection<ClassData> classData = (Collection<ClassData>) projectData.getClasses(); | ||
for(ClassData classDatum : classData){ | ||
final FileType fileType = new FileType(); | ||
coverageType.getFile().add(fileType); | ||
|
||
//Important to set this correctly for HTML reports to be able to link to source code files properly | ||
fileType.setPath(findPrefix(sonarSourcesPathPrefixMap, classDatum.getSourceFileName()) + classDatum.getSourceFileName()); | ||
|
||
LineData lineData; | ||
LineToCoverType lineToCoverType; | ||
for(CoverageData line : classDatum.getLines()){ | ||
lineData = (LineData)line; | ||
lineToCoverType = new LineToCoverType(); | ||
fileType.getLineToCover().add(lineToCoverType); | ||
|
||
lineToCoverType.setLineNumber(lineData.getLineNumber()); | ||
lineToCoverType.setCoveredBranches(lineData.getNumberOfCoveredBranches()); | ||
lineToCoverType.setBranchesToCover(lineData.getNumberOfValidBranches()); | ||
lineToCoverType.setCovered(lineData.isCovered()); | ||
} | ||
} | ||
|
||
final JAXBElement<CoverageType> coverageTypeJAXBElement = factory.createCoverage(coverageType); | ||
marshaller.marshal(coverageTypeJAXBElement, destinationSonarXmlFile); | ||
}catch(JAXBException je){ | ||
throw new MojoExecutionException("Marshalling problem", je); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Find the correct file prefix path for the given sourceFileName, choose the first one if no file exists that matches | ||
* the prefixes present in the project | ||
* | ||
* @param sonarSourcesPathPrefixMap | ||
* @param sourceFileName | ||
* @return | ||
*/ | ||
String findPrefix(final Map<String, String> sonarSourcesPathPrefixMap, final String sourceFileName){ | ||
String defaultPrefix = null; | ||
File fileToCheck; | ||
for(Map.Entry<String, String> sonarSourcesPathPrefixEntry : sonarSourcesPathPrefixMap.entrySet()){ | ||
fileToCheck = new File(sonarSourcesPathPrefixEntry.getKey() + sourceFileName); | ||
if(fileToCheck.exists()){ | ||
return sonarSourcesPathPrefixEntry.getValue(); | ||
}else { | ||
//Should we even bother defaulting? | ||
//Default to the first encountered prefix | ||
if(defaultPrefix == null){ | ||
defaultPrefix = sonarSourcesPathPrefixEntry.getValue(); | ||
} | ||
} | ||
} | ||
|
||
return defaultPrefix; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entire defaulting thing here can potentially be removed and just returning a default "" and just know that the file name is not going to resolve because we don't know what it is.