Add unit test for inputfilepath parameter

This commit is contained in:
Andreas Dangel
2016-06-12 19:38:54 +02:00
parent 47e105bd36
commit bee5991121
7 changed files with 64 additions and 4 deletions

View File

@ -431,11 +431,9 @@ public class PMD {
} else {
String filePaths = FileUtils.readFileToString(new File(inputFilePath));
filePaths = StringUtils.trimToEmpty(filePaths);
filePaths = filePaths.replaceAll("\\r?\\n", ",");
filePaths = filePaths.replaceAll(",+", ",");
if (null == filePaths){
LOG.log(Level.SEVERE, "Problem with Input File Path", inputFilePath);
throw new RuntimeException("Problem with Input File Path: " + inputFilePath);
}
files.addAll(FileUtil.collectFiles(filePaths, fileSelector));
}
} catch (IOException ex) {

View File

@ -314,6 +314,12 @@ public class PMDConfiguration extends AbstractConfiguration {
return inputFilePath;
}
/**
* The input file path points to a single file, which contains a
* comma-separated list of source file names to process.
*
* @param inputFilePath path to the file
*/
public void setInputFilePath(String inputFilePath) {
this.inputFilePath = inputFilePath;
}

View File

@ -0,0 +1,50 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cli;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.util.datasource.DataSource;
public class PMDTest {
@Test
public void testGetApplicableFiles() {
Set<Language> languages = new HashSet<>();
languages.add(new DummyLanguageModule());
PMDConfiguration configuration = new PMDConfiguration();
configuration.setInputFilePath("src/test/resources/net/sourceforge/pmd/cli/filelist.txt");
List<DataSource> applicableFiles = PMD.getApplicableFiles(configuration, languages);
Assert.assertEquals(2, applicableFiles.size());
Assert.assertTrue(applicableFiles.get(0).getNiceFileName(false, "").endsWith("somefile.dummy"));
Assert.assertTrue(applicableFiles.get(1).getNiceFileName(false, "").endsWith("anotherfile.dummy"));
}
@Test
public void testGetApplicableFilesMultipleLines() {
Set<Language> languages = new HashSet<>();
languages.add(new DummyLanguageModule());
PMDConfiguration configuration = new PMDConfiguration();
configuration.setInputFilePath("src/test/resources/net/sourceforge/pmd/cli/filelist2.txt");
List<DataSource> applicableFiles = PMD.getApplicableFiles(configuration, languages);
Assert.assertEquals(3, applicableFiles.size());
Assert.assertTrue(applicableFiles.get(0).getNiceFileName(false, "").endsWith("somefile.dummy"));
Assert.assertTrue(applicableFiles.get(1).getNiceFileName(false, "").endsWith("anotherfile.dummy"));
Assert.assertTrue(applicableFiles.get(2).getNiceFileName(false, "").endsWith("somefile.dummy"));
}
}

View File

@ -0,0 +1 @@
src/test/resources/net/sourceforge/pmd/cli/src/somefile.dummy,src/test/resources/net/sourceforge/pmd/cli/src/anotherfile.dummy

View File

@ -0,0 +1,3 @@
src/test/resources/net/sourceforge/pmd/cli/src/somefile.dummy,
src/test/resources/net/sourceforge/pmd/cli/src/anotherfile.dummy
src/test/resources/net/sourceforge/pmd/cli/src/somefile.dummy

View File

@ -0,0 +1 @@
Another file for testing

View File

@ -0,0 +1 @@
Some file for testing