pmd: fix #1080 net.sourceforge.pmd.cpd.CPDTest test failing

The symlinks are simply removed now from the source code and
created by the test, when needed.
The test runs only under unix systems, not under windows.
This commit is contained in:
Andreas Dangel 2013-04-14 17:26:50 +02:00
parent a5efd66b73
commit 2e85f1f935
5 changed files with 52 additions and 13 deletions

View File

@ -1,5 +1,6 @@
????? ??, 2013 - 5.0.4:
Fixed bug 1080: net.sourceforge.pmd.cpd.CPDTest test failing
Fixed bug 1081: Regression: CPD skipping all files when using relative paths
Fixed bug 1082: CPD performance issue on larger projects

View File

@ -616,6 +616,12 @@
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>

View File

@ -5,6 +5,7 @@ package net.sourceforge.pmd.cpd;
import java.io.File;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -14,14 +15,43 @@ import org.junit.Test;
*/
public class CPDTest {
private static final String BASE_TEST_RESOURCE_PATH = "src/test/resources/net/sourceforge/pmd/cpd/files/";
private static final String BASE_TEST_RESOURCE_PATH = "target/test-classes/net/sourceforge/pmd/cpd/files/";
private CPD cpd;
private boolean canTestSymLinks = false;
@Before
public void setup() {
public void setup() throws Exception {
CPDConfiguration theConfiguration = new CPDConfiguration(new String[] {"--language", "java",
"--minimum-tokens", "10"});
cpd = new CPD(theConfiguration);
// Symlinks are not well supported under Windows - so the tests are simply not executed here.
canTestSymLinks = SystemUtils.IS_OS_UNIX;
prepareSymLinks();
if (!canTestSymLinks) {
System.err.println("*** Skipping unit tests with symlinks.");
}
}
/**
* As java doesn't support symlinks in zip files, maven does not, too.
* So, we are creating the symlinks manually here before the test.
* @throws Exception any error
*/
private void prepareSymLinks() throws Exception {
if (canTestSymLinks) {
Runtime runtime = Runtime.getRuntime();
if (!new File(BASE_TEST_RESOURCE_PATH, "symlink-for-real-file.txt").exists()) {
runtime.exec(new String[] {"ln", "-s", "real-file.txt",
BASE_TEST_RESOURCE_PATH + "symlink-for-real-file.txt"}).waitFor();
}
if (!new File(BASE_TEST_RESOURCE_PATH, "this-is-a-broken-sym-link-for-test").exists()) {
runtime.exec(new String[] {"ln", "-s", "broken-sym-link",
BASE_TEST_RESOURCE_PATH + "this-is-a-broken-sym-link-for-test"}).waitFor();
}
}
}
/**
@ -30,12 +60,14 @@ public class CPDTest {
*/
@Test
public void testFileSectionWithBrokenSymlinks() throws Exception {
if (canTestSymLinks) {
NoFileAssertListener listener = new NoFileAssertListener(0);
cpd.setCpdListener(listener);
cpd.add(new File(BASE_TEST_RESOURCE_PATH, "this-is-a-broken-sym-link-for-test"));
listener.verify();
}
}
/**
* A file should be added only once - even if it was found twice, because of a sym link.
@ -43,6 +75,7 @@ public class CPDTest {
*/
@Test
public void testFileAddedAsSymlinkAndReal() throws Exception {
if (canTestSymLinks) {
NoFileAssertListener listener = new NoFileAssertListener(1);
cpd.setCpdListener(listener);
@ -50,6 +83,7 @@ public class CPDTest {
cpd.add(new File(BASE_TEST_RESOURCE_PATH, "symlink-for-real-file.txt"));
listener.verify();
}
}
/**
* Add a file with a relative path - should still be added and not be detected as a sym link.