pmd: fixed #1068 CPD fails on broken symbolic links

This commit is contained in:
Andreas Dangel
2013-03-01 20:17:45 +01:00
parent 3feda38cc3
commit a00532595a
6 changed files with 85 additions and 1 deletions

View File

@ -1,3 +1,9 @@
????? ??, 2013 - 5.0.3:
Fixed bug 1068: CPD fails on broken symbolic links
February 3, 2013 - 5.0.2: February 3, 2013 - 5.0.2:
Fixed bug 878: False positive: UnusedFormalParameter for abstract methods Fixed bug 878: False positive: UnusedFormalParameter for abstract methods

View File

@ -93,11 +93,16 @@ public class CPD {
current.add(signature); current.add(signature);
} }
if (!file.getCanonicalPath().equals(new File(file.getAbsolutePath()).getCanonicalPath())) { if (!file.getCanonicalPath().equals(file.getAbsolutePath())) {
System.err.println("Skipping " + file + " since it appears to be a symlink"); System.err.println("Skipping " + file + " since it appears to be a symlink");
return; return;
} }
if (!file.exists()) {
System.err.println("Skipping " + file + " since it doesn't exist (broken symlink?)");
return;
}
listener.addedFile(fileCount, file); listener.addedFile(fileCount, file);
SourceCode sourceCode = configuration.sourceCodeFor(file); SourceCode sourceCode = configuration.sourceCodeFor(file);
configuration.tokenizer().tokenize(sourceCode, tokens); configuration.tokenizer().tokenize(sourceCode, tokens);

View File

@ -0,0 +1,71 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.File;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link CPD}
*/
public class CPDTest {
private static final String BASE_TEST_RESOURCE_PATH = "src/test/resources/net/sourceforge/pmd/cpd/files/";
private CPD cpd;
@Before
public void setup() {
CPDConfiguration theConfiguration = new CPDConfiguration(new String[] {"--language", "java",
"--minimum-tokens", "10"});
cpd = new CPD(theConfiguration);
}
/**
* A broken symlink (which is basically a not existing file), should be skipped.
* @throws Exception any error
*/
@Test
public void testFileSectionWithBrokenSymlinks() throws Exception {
cpd.setCpdListener(new NoFileAssertListener(0));
cpd.add(new File(BASE_TEST_RESOURCE_PATH, "this-is-a-broken-sym-link-for-test"));
}
/**
* A file should be added only once - even if it was found twice, because of a sym link.
* @throws Exception any error
*/
@Test
public void testFileAddedAsSymlinkAndReal() throws Exception {
cpd.setCpdListener(new NoFileAssertListener(1));
cpd.add(new File(BASE_TEST_RESOURCE_PATH, "real-file.txt"));
cpd.add(new File(BASE_TEST_RESOURCE_PATH, "symlink-for-real-file.txt"));
}
/**
* Simple listener that fails, if to many files were added and not skipped.
*/
private static class NoFileAssertListener implements CPDListener {
private int maximumFilesAllowed;
private int files;
public NoFileAssertListener(int maximumFilesAllowed) {
this.maximumFilesAllowed = maximumFilesAllowed;
this.files = 0;
}
public void addedFile(int fileCount, File file) {
files++;
if (files > maximumFilesAllowed) {
Assert.fail("File was added! - " + file);
}
}
public void phaseUpdate(int phase) {
// not needed for this test
}
}
}

View File

@ -0,0 +1 @@
real-file.txt

View File

@ -0,0 +1 @@
broken-sym-link