use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.
the class CpdReportTest method testWriteNonHtml.
public void testWriteNonHtml() throws Exception {
File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml");
CpdReport mojo = (CpdReport) lookupMojo("cpd", testPom);
assertNotNull(mojo);
TokenEntry tFirstEntry = new TokenEntry("public java", "MyClass.java", 2);
TokenEntry tSecondEntry = new TokenEntry("public java", "MyClass3.java", 2);
String duplicatedCodeFragment = "// ----- duplicated code example -----";
SourceCode sourceCodeFirst = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass.java"));
SourceCode sourceCodeSecond = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass3.java"));
List<Match> tList = new ArrayList<>();
Mark tFirstMark = new Mark(tFirstEntry);
tFirstMark.setSourceCode(sourceCodeFirst);
tFirstMark.setLineCount(1);
Mark tSecondMark = new Mark(tSecondEntry);
tSecondMark.setSourceCode(sourceCodeSecond);
tSecondMark.setLineCount(1);
Match tMatch = new Match(2, tFirstMark, tSecondMark);
tList.add(tMatch);
CPDConfiguration cpdConfiguration = new CPDConfiguration();
cpdConfiguration.setMinimumTileSize(100);
cpdConfiguration.setLanguage(new JavaLanguage());
cpdConfiguration.setEncoding("UTF-8");
CPD tCpd = new MockCpd(cpdConfiguration, tList.iterator());
tCpd.go();
mojo.writeNonHtml(tCpd);
File tReport = new File("target/test/unit/default-configuration/target/cpd.xml");
// parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document pmdCpdDocument = builder.parse(tReport);
assertNotNull(pmdCpdDocument);
String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"));
assertTrue(lowerCaseContains(str, "MyClass.java"));
assertTrue(lowerCaseContains(str, "MyClass3.java"));
assertTrue(lowerCaseContains(str, duplicatedCodeFragment));
}
use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.
the class CpdReport method executeCpd.
private void executeCpd() throws MavenReportException {
if (cpd != null) {
// CPD has already been run
getLog().debug("CPD has already been run - skipping redundant execution.");
return;
}
Properties p = new Properties();
if (ignoreLiterals) {
p.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
}
if (ignoreIdentifiers) {
p.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
}
try {
if (filesToProcess == null) {
filesToProcess = getFilesToProcess();
}
try {
excludeDuplicationsFromFile.loadExcludeFromFailuresData(excludeFromFailureFile);
} catch (MojoExecutionException e) {
throw new MavenReportException("Error loading exclusions", e);
}
String encoding = determineEncoding(!filesToProcess.isEmpty());
Language cpdLanguage;
if ("java".equals(language) || null == language) {
cpdLanguage = new JavaLanguage(p);
} else if ("javascript".equals(language)) {
cpdLanguage = new EcmascriptLanguage();
} else if ("jsp".equals(language)) {
cpdLanguage = new JSPLanguage();
} else {
cpdLanguage = LanguageFactory.createLanguage(language, p);
}
CPDConfiguration cpdConfiguration = new CPDConfiguration();
cpdConfiguration.setMinimumTileSize(minimumTokens);
cpdConfiguration.setLanguage(cpdLanguage);
cpdConfiguration.setSourceEncoding(encoding);
cpd = new CPD(cpdConfiguration);
for (File file : filesToProcess.keySet()) {
cpd.add(file);
}
} catch (UnsupportedEncodingException e) {
throw new MavenReportException("Encoding '" + getSourceEncoding() + "' is not supported.", e);
} catch (IOException e) {
throw new MavenReportException(e.getMessage(), e);
}
getLog().debug("Executing CPD...");
cpd.go();
getLog().debug("CPD finished.");
// so the "check" goals can check for violations
if (isXml()) {
writeNonHtml(cpd);
}
}
use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.
the class CpdReport method executeCpd.
private void executeCpd() throws MavenReportException {
if (cpd != null) {
// CPD has already been run
getLog().debug("CPD has already been run - skipping redundant execution.");
return;
}
Properties p = new Properties();
if (ignoreLiterals) {
p.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
}
if (ignoreIdentifiers) {
p.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
}
try {
if (filesToProcess == null) {
filesToProcess = getFilesToProcess();
}
try {
excludeDuplicationsFromFile.loadExcludeFromFailuresData(excludeFromFailureFile);
} catch (MojoExecutionException e) {
throw new MavenReportException("Error loading exclusions", e);
}
String encoding = determineEncoding(!filesToProcess.isEmpty());
Language cpdLanguage;
if ("java".equals(language) || null == language) {
cpdLanguage = new JavaLanguage(p);
} else if ("javascript".equals(language)) {
cpdLanguage = new EcmascriptLanguage();
} else if ("jsp".equals(language)) {
cpdLanguage = new JSPLanguage();
} else {
cpdLanguage = LanguageFactory.createLanguage(language, p);
}
CPDConfiguration cpdConfiguration = new CPDConfiguration();
cpdConfiguration.setMinimumTileSize(minimumTokens);
cpdConfiguration.setLanguage(cpdLanguage);
cpdConfiguration.setSourceEncoding(encoding);
cpd = new CPD(cpdConfiguration);
for (File file : filesToProcess.keySet()) {
cpd.add(file);
}
} catch (UnsupportedEncodingException e) {
throw new MavenReportException("Encoding '" + getSourceEncoding() + "' is not supported.", e);
} catch (IOException e) {
throw new MavenReportException(e.getMessage(), e);
}
getLog().debug("Executing CPD...");
cpd.go();
getLog().debug("CPD finished.");
// so the "check" goals can check for violations
if (isXml()) {
writeNonHtml(cpd);
}
}
use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.
the class CpdReportTest method testWriteNonHtml.
public void testWriteNonHtml() throws Exception {
File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml");
CpdReport mojo = (CpdReport) lookupMojo("cpd", testPom);
assertNotNull(mojo);
TokenEntry tFirstEntry = new TokenEntry("public java", "MyClass.java", 2);
TokenEntry tSecondEntry = new TokenEntry("public java", "MyClass3.java", 2);
String duplicatedCodeFragment = "// ----- duplicated code example -----";
SourceCode sourceCodeFirst = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass.java"));
SourceCode sourceCodeSecond = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass3.java"));
List<Match> tList = new ArrayList<>();
Mark tFirstMark = new Mark(tFirstEntry);
tFirstMark.setSourceCode(sourceCodeFirst);
tFirstMark.setLineCount(1);
Mark tSecondMark = new Mark(tSecondEntry);
tSecondMark.setSourceCode(sourceCodeSecond);
tSecondMark.setLineCount(1);
Match tMatch = new Match(2, tFirstMark, tSecondMark);
tList.add(tMatch);
CPDConfiguration cpdConfiguration = new CPDConfiguration();
cpdConfiguration.setMinimumTileSize(100);
cpdConfiguration.setLanguage(new JavaLanguage());
cpdConfiguration.setEncoding("UTF-8");
CPD tCpd = new MockCpd(cpdConfiguration, tList.iterator());
tCpd.go();
mojo.writeNonHtml(tCpd);
File tReport = new File("target/test/unit/default-configuration/target/cpd.xml");
// parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document pmdCpdDocument = builder.parse(tReport);
assertNotNull(pmdCpdDocument);
String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"));
assertTrue(lowerCaseContains(str, "MyClass.java"));
assertTrue(lowerCaseContains(str, "MyClass3.java"));
assertTrue(lowerCaseContains(str, duplicatedCodeFragment));
}
use of net.sourceforge.pmd.cpd.CPD in project pmd-eclipse-plugin by pmd.
the class DetectCutAndPasteCmd method execute.
/**
* @see name.herlin.command.AbstractProcessableCommand#execute()
*/
@Override
public void execute() throws CommandException {
try {
List<File> files = findCandidateFiles();
if (files.isEmpty()) {
logInfo("No files found for specified language.");
} else {
logInfo("Found " + files.size() + " files for the specified language. Performing CPD.");
}
setStepCount(files.size());
beginTask("Finding suspect Cut And Paste", getStepCount() * 2);
if (!isCanceled()) {
final CPD cpd = detectCutAndPaste(files);
if (!isCanceled()) {
if (createReport) {
renderReport(cpd.getMatches());
}
notifyListeners(cpd);
}
}
} catch (CoreException e) {
LOG.debug("Core Exception: " + e.getMessage(), e);
throw new CommandException(e);
} catch (PropertiesException e) {
LOG.debug("Properties Exception: " + e.getMessage(), e);
throw new CommandException(e);
} finally {
setTerminated(true);
}
}
Aggregations