Skip to content

Commit

Permalink
CPD
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Nov 5, 2016
1 parent 23fa562 commit 7a4e651
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ It supports:
* [_AndoidLint_](http://developer.android.com/tools/help/lint.html)
* [_Checkstyle_](http://checkstyle.sourceforge.net/) ([_ESLint_](https://github.com/sindresorhus/grunt-eslint) with `format: 'checkstyle'`)
* [_CodeNarc_](http://codenarc.sourceforge.net/)
* [_CPD_](http://pmd.sourceforge.net/pmd-4.3.0/cpd.html)
* [_CPPLint_](https://github.com/theandrewdavis/cpplint)
* [_CPPCheck_](http://cppcheck.sourceforge.net/)
* [_CSSLint_](https://github.com/CSSLint/csslint)
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/CPDParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.collect.Lists.newArrayList;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.CODENARC;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class CPDParser implements ViolationsParser {

private SEVERITY getSeverity(Integer from) {
if (from < 100) {
return INFO;
}
if (from < 1000) {
return WARN;
}
return ERROR;
}

@Override
public List<Violation> parseFile(String string) throws Exception {
List<Violation> violations = newArrayList();
try (InputStream input = new ByteArrayInputStream(string.getBytes())) {

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlr = factory.createXMLStreamReader(input);

List<String> files = newArrayList();
List<Integer> filesLine = newArrayList();
Integer tokens = null;
while (xmlr.hasNext()) {
int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("duplication")) {
tokens = getIntegerAttribute(xmlr, "tokens");
}
if (xmlr.getLocalName().equals("file")) {
files.add(getAttribute(xmlr, "path"));
filesLine.add(getIntegerAttribute(xmlr, "line"));
}
if (xmlr.getLocalName().equals("codefragment")) {
String codefragment = xmlr.getElementText().trim();
for (int i = 0; i < filesLine.size(); i++) {
String file = files.get(i);
Integer line = filesLine.get(i);
Violation violation = violationBuilder()//
.setReporter(CODENARC)//
.setFile(file)//
.setMessage(codefragment)//
.setRule("DUPLICATION")//
.setSeverity(getSeverity(tokens))//
.setStartLine(line)//
.build();
violations.add(violation);
}
files.clear();
filesLine.clear();
}
}
}
}
return violations;
}

}
2 changes: 2 additions & 0 deletions src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.parsers.AndroidLintParser;
import se.bjurr.violations.lib.parsers.CPDParser;
import se.bjurr.violations.lib.parsers.CPPCheckParser;
import se.bjurr.violations.lib.parsers.CSSLintParser;
import se.bjurr.violations.lib.parsers.CheckStyleParser;
Expand All @@ -35,6 +36,7 @@ public enum Reporter {
ANDROIDLINT(new AndroidLintParser()), //
CHECKSTYLE(new CheckStyleParser()), //
CODENARC(new CodeNarcParser()), //
CPD(new CPDParser()), //
CPPCHECK(new CPPCheckParser()), //
CPPLINT(new CppLintParser()), //
CSSLINT(new CSSLintParser()), //
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/se/bjurr/violations/lib/CPDTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package se.bjurr.violations.lib;

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.reports.Reporter.CPD;

import java.util.List;

import org.junit.Test;

import se.bjurr.violations.lib.model.Violation;

public class CPDTest {

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/cpd/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(CPD) //
.violations();

assertThat(actual)//
.hasSize(2);

assertThat(actual.get(0).getMessage())//
.startsWith("$request->setBo");
assertThat(actual.get(0).getFile())//
.isEqualTo("/home/goetas/gits/webservices/src/goetas/webservices/bindings/soap12/transport/http/Http.php");
assertThat(actual.get(0).getSeverity())//
.isEqualTo(INFO);
assertThat(actual.get(0).getRule().get())//
.isEqualTo("DUPLICATION");
assertThat(actual.get(0).getStartLine())//
.isEqualTo(36);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(36);

assertThat(actual.get(1).getMessage())//
.startsWith("$request->setBo");
}
}
20 changes: 20 additions & 0 deletions src/test/resources/cpd/cpd-report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<pmd-cpd>
<duplication lines="12" tokens="38">
<file path="/home/goetas/gits/webservices/src/goetas/webservices/bindings/soap12/transport/http/Http.php" line="36"/>
<file path="/home/goetas/gits/webservices/src/goetas/webservices/bindings/soap/transport/http/Http.php" line="41"/>
<codefragment> $request-&gt;setBody($xml, 'application/soap+xml; charset=utf-8; action="' . $soapAction . '"');

if ($this-&gt;debugUri){
$request-&gt;setUrl($this-&gt;debugUri);
}
$response = $this-&gt;client-&gt;send($request);

if(!$response-&gt;isSuccessful()){
throw new TransportException($response-&gt;getReasonPhrase(), $response-&gt;getStatusCode());
}

return $response-&gt;getBody(true);
</codefragment>
</duplication>
</pmd-cpd>

0 comments on commit 7a4e651

Please sign in to comment.