use of net.sourceforge.pmd.cpd.TokenEntry in project sonarqube by SonarSource.
the class XooTokenizerTest method testExecution.
@Test
public void testExecution() throws IOException {
File source = new File(baseDir, "src/foo.xoo");
FileUtils.write(source, "token1 token2 token3\ntoken4");
DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
fileSystem.add(inputFile);
XooTokenizer tokenizer = new XooTokenizer(fileSystem);
SourceCode sourceCode = mock(SourceCode.class);
when(sourceCode.getFileName()).thenReturn(inputFile.absolutePath());
Tokens cpdTokens = new Tokens();
tokenizer.tokenize(sourceCode, cpdTokens);
// 4 tokens + EOF
assertThat(cpdTokens.getTokens()).hasSize(5);
assertThat(cpdTokens.getTokens().get(3)).isEqualTo(new TokenEntry("token4", "src/foo.xoo", 2));
}
use of net.sourceforge.pmd.cpd.TokenEntry in project pmd by pmd.
the class AbstractTokenizerTest method tokenizeTest.
protected void tokenizeTest() throws IOException {
Tokens tokens = new Tokens();
tokenizer.tokenize(sourceCode, tokens);
List<TokenEntry> entries = tokens.getTokens();
assertEquals(expectedTokenCount, entries.size());
}
use of net.sourceforge.pmd.cpd.TokenEntry 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.TokenEntry in project pmd-eclipse-plugin by pmd.
the class CPDViewDoubleClickEventListener method doubleClick.
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.
* jface.viewers.DoubleClickEvent)
*/
public void doubleClick(DoubleClickEvent event) {
final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
final Object object = selection.getFirstElement();
final TreeNode node = (TreeNode) object;
final Object value = node.getValue();
final TreeViewer treeViewer = view.getTreeViewer();
if (value instanceof Match) {
if (treeViewer.getExpandedState(node)) {
// the node is expanded, so collapse
treeViewer.collapseToLevel(node, TreeViewer.ALL_LEVELS);
} else {
// the node is collapsed, so expand
treeViewer.expandToLevel(node, 1);
}
} else if (value instanceof TokenEntry) {
final TokenEntry entry = (TokenEntry) value;
final Match match = (Match) node.getParent().getValue();
highlightText(match, entry);
}
}
use of net.sourceforge.pmd.cpd.TokenEntry in project pmd-eclipse-plugin by pmd.
the class CPDViewLabelProvider method getColumnImage.
/*
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
*/
public Image getColumnImage(Object element, int columnIndex) {
Image image = null;
final TreeNode node = (TreeNode) element;
final Object value = node.getValue();
// if the Element is a Match or TokenEntry
switch(columnIndex) {
case 1:
if (value instanceof Match) {
image = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
} else if (value instanceof TokenEntry) {
image = PlatformUI.getWorkbench().getSharedImages().getImage(SharedImages.IMG_OPEN_MARKER);
}
break;
default:
}
return image;
}
Aggregations