Properly handle shortnames with comma-separated args
- Also added unit tests to make sure the behavior is as expected
This commit is contained in:
@@ -8,6 +8,8 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* DataSource implementation to read data from a file.
|
||||
@@ -38,14 +40,22 @@ public class FileDataSource implements DataSource {
|
||||
|
||||
private String glomName(boolean shortNames, String inputFileName, File file) {
|
||||
if (shortNames) {
|
||||
if (inputFileName != null && inputFileName.indexOf(',') == -1) {
|
||||
if (new File(inputFileName).isDirectory()) {
|
||||
return trimAnyPathSep(file.getPath().substring(inputFileName.length()));
|
||||
} else {
|
||||
if (inputFileName.indexOf(FILE_SEPARATOR.charAt(0)) == -1) {
|
||||
return inputFileName;
|
||||
if (inputFileName != null) {
|
||||
for (final String prefix : inputFileName.split(",")) {
|
||||
final Path prefPath = Paths.get(prefix).toAbsolutePath();
|
||||
final String prefPathString = prefPath.toString();
|
||||
final String absoluteFilePath = file.getAbsolutePath();
|
||||
|
||||
if (absoluteFilePath.startsWith(prefPathString)) {
|
||||
if (prefPath.toFile().isDirectory()) {
|
||||
return trimAnyPathSep(absoluteFilePath.substring(prefPathString.length()));
|
||||
} else {
|
||||
if (inputFileName.indexOf(FILE_SEPARATOR.charAt(0)) == -1) {
|
||||
return inputFileName;
|
||||
}
|
||||
return trimAnyPathSep(absoluteFilePath.substring(prefPathString.lastIndexOf(FILE_SEPARATOR)));
|
||||
}
|
||||
}
|
||||
return trimAnyPathSep(inputFileName.substring(inputFileName.lastIndexOf(FILE_SEPARATOR)));
|
||||
}
|
||||
} else {
|
||||
// if the 'master' file is not specified, just use the file name
|
||||
|
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.util.datasource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
public class FileDataSourceTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
private static final String SOMEFILE_DIR = "path/";
|
||||
private static final String SOMEFILE_TXT = "somefile.txt";
|
||||
private static final String SOMEFILE_TXT_FULL_PATH = SOMEFILE_DIR + SOMEFILE_TXT;
|
||||
|
||||
private FileDataSource ds;
|
||||
private File someFile;
|
||||
private File someFolder;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
someFolder = tempFolder.newFolder(SOMEFILE_DIR);
|
||||
someFile = tempFolder.newFile(SOMEFILE_TXT_FULL_PATH);
|
||||
ds = new FileDataSource(someFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortNamesSingleFile() {
|
||||
assertEquals(SOMEFILE_TXT, ds.getNiceFileName(true, someFile.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortNamesSingleDir() {
|
||||
assertEquals(SOMEFILE_TXT, ds.getNiceFileName(true, someFolder.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortNamesNullBase() {
|
||||
assertEquals(SOMEFILE_TXT, ds.getNiceFileName(true, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortNamesCommaSeparatedDirs() {
|
||||
// use 2 dirs, one relative (similar, but not resolving to the same location) and one absolute
|
||||
assertEquals(SOMEFILE_TXT, ds.getNiceFileName(true, SOMEFILE_DIR + "," + someFolder.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortNamesCommaSeparatedFiles() {
|
||||
// use 2 files, one relative (similar, but not resolving to the same location) and one absolute
|
||||
assertEquals(SOMEFILE_TXT, ds.getNiceFileName(true, SOMEFILE_TXT_FULL_PATH + "," + someFile.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortNamesCommaSeparatedMixed() {
|
||||
// use a file and a dir, one relative (similar, but not resolving to the same location) and one absolute
|
||||
assertEquals(SOMEFILE_TXT, ds.getNiceFileName(true, SOMEFILE_TXT_FULL_PATH + "," + someFolder.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongNamesSingleFile() throws IOException {
|
||||
assertEquals(someFile.getCanonicalFile().getAbsolutePath(), ds.getNiceFileName(false, someFile.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongNamesSingleDir() throws IOException {
|
||||
assertEquals(someFile.getCanonicalFile().getAbsolutePath(), ds.getNiceFileName(false, someFolder.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongNamesNullBase() throws IOException {
|
||||
assertEquals(someFile.getCanonicalFile().getAbsolutePath(), ds.getNiceFileName(false, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongNamesCommaSeparatedDirs() throws IOException {
|
||||
// use 2 dirs, one relative (similar, but not resolving to the same location) and one absolute
|
||||
assertEquals(someFile.getCanonicalFile().getAbsolutePath(),
|
||||
ds.getNiceFileName(false, SOMEFILE_DIR + "," + someFolder.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongNamesCommaSeparatedFiles() throws IOException {
|
||||
// use 2 files, one relative (similar, but not resolving to the same location) and one absolute
|
||||
assertEquals(someFile.getCanonicalFile().getAbsolutePath(),
|
||||
ds.getNiceFileName(false, SOMEFILE_TXT_FULL_PATH + "," + someFile.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongNamesCommaSeparatedMixed() throws IOException {
|
||||
// use a file and a dir, one relative (similar, but not resolving to the same location) and one absolute
|
||||
assertEquals(someFile.getCanonicalFile().getAbsolutePath(),
|
||||
ds.getNiceFileName(false, SOMEFILE_TXT_FULL_PATH + "," + someFolder.getAbsolutePath()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user