From 76129cf584adb517edc1bd6146940597c4c17abe Mon Sep 17 00:00:00 2001 From: Victor Guerra Veloso Date: Wed, 26 Aug 2020 01:27:12 -0300 Subject: [PATCH] Add Linux path separator support --- TestFileMapping.iml | 29 ---------- pom.xml | 31 ++++++++++ src/main/java/edu/rit/se/testsmells/Main.java | 2 +- .../rit/se/testsmells/MappingDetector.java | 6 +- .../java/edu/rit/se/testsmells/TestFile.java | 57 ++++++++++--------- .../edu/rit/se/testsmelss/TestFileTest.java | 42 ++++++++++++++ 6 files changed, 106 insertions(+), 61 deletions(-) delete mode 100644 TestFileMapping.iml create mode 100644 src/test/java/edu/rit/se/testsmelss/TestFileTest.java diff --git a/TestFileMapping.iml b/TestFileMapping.iml deleted file mode 100644 index d74c08b..0000000 --- a/TestFileMapping.iml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 218dc31..a059bad 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,26 @@ + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + true + lib/ + edu.rit.se.testsmells.Main + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + org.apache.maven.plugins maven-compiler-plugin @@ -38,6 +58,17 @@ opencsv 3.9 + + org.apache.commons + commons-lang3 + 3.11 + + + org.junit.jupiter + junit-jupiter + RELEASE + test + diff --git a/src/main/java/edu/rit/se/testsmells/Main.java b/src/main/java/edu/rit/se/testsmells/Main.java index cf57c94..6f27980 100644 --- a/src/main/java/edu/rit/se/testsmells/Main.java +++ b/src/main/java/edu/rit/se/testsmells/Main.java @@ -56,7 +56,7 @@ public static void main(String[] args) throws IOException { columnValues = new ArrayList<>(); columnValues.add(0,testFiles.get(i).getAppName()); columnValues.add(1,testFiles.get(i).getTagName()); - columnValues.add(2,testFiles.get(i).getFilePath()); + columnValues.add(2,testFiles.get(i).getUnixFilePath()); columnValues.add(3,testFiles.get(i).getProductionFilePath()); columnValues.add(4,testFiles.get(i).getRelativeTestFilePath()); columnValues.add(5,testFiles.get(i).getRelativeProductionFilePath()); diff --git a/src/main/java/edu/rit/se/testsmells/MappingDetector.java b/src/main/java/edu/rit/se/testsmells/MappingDetector.java index c62d5d5..271c9e7 100644 --- a/src/main/java/edu/rit/se/testsmells/MappingDetector.java +++ b/src/main/java/edu/rit/se/testsmells/MappingDetector.java @@ -31,7 +31,7 @@ public TestFile detectMapping(String testFilePath) throws IOException { int index = testFile.getFileName().toLowerCase().lastIndexOf("test"); if (index == 0) { //the name of the test file starts with the name 'test' - productionFileName = testFile.getFileName().substring(4, testFile.getFileName().length()); + productionFileName = testFile.getFileName().substring(4); } else { //the name of the test file ends with the name 'test' productionFileName = testFile.getFileName().substring(0, testFile.getFileName().toLowerCase().lastIndexOf("test")) + ".java"; @@ -76,9 +76,7 @@ private boolean isFileSyntacticallyValid(String filePath) { public class FindJavaTestFilesVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, - BasicFileAttributes attrs) - throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if (file.getFileName().toString().toLowerCase().equals(productionFileName.toLowerCase())) { productionFilePath = file.toAbsolutePath().toString(); return FileVisitResult.TERMINATE; diff --git a/src/main/java/edu/rit/se/testsmells/TestFile.java b/src/main/java/edu/rit/se/testsmells/TestFile.java index 6c28398..5f93fbf 100644 --- a/src/main/java/edu/rit/se/testsmells/TestFile.java +++ b/src/main/java/edu/rit/se/testsmells/TestFile.java @@ -2,15 +2,23 @@ import org.apache.commons.lang3.StringUtils; +import java.io.File; +import java.util.regex.Pattern; + public class TestFile { private String filePath, productionFilePath; - String[] data; + private String[] data; + + public TestFile(String filePath) { + this.filePath = filePath; + data = splitFilepath(filePath); + } public String getFileName() { return data[data.length - 1]; } - public String getFilePath() { + public String getUnixFilePath() { return filePath; } @@ -23,11 +31,7 @@ public void setProductionFilePath(String productionFilePath) { } public String getProjectRootFolder() { - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < 5; i++) { - stringBuilder.append(data[i] + "\\"); - } - return stringBuilder.toString(); + return get5LevelsPath(data).toString(); } public String getAppName() { @@ -38,32 +42,31 @@ public String getTagName() { return data[4]; } - public TestFile(String filePath) { - this.filePath = filePath; - data = filePath.split("\\\\"); + public String getRelativeTestFilePath() { + return getUnixFilePath(filePath); } - public String getRelativeTestFilePath(){ - String[] splitString = filePath.split("\\\\"); - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < 5; i++) { - stringBuilder.append(splitString[i] + "\\"); + public String getRelativeProductionFilePath() { + if (!StringUtils.isEmpty(productionFilePath)) { + return getUnixFilePath(productionFilePath); } - return filePath.substring(stringBuilder.toString().length()).replace("\\","/"); + return ""; } - public String getRelativeProductionFilePath(){ - if (!StringUtils.isEmpty(productionFilePath)){ - String[] splitString = productionFilePath.split("\\\\"); - StringBuilder stringBuilder = new StringBuilder(); - for (int i = 0; i < 5; i++) { - stringBuilder.append(splitString[i] + "\\"); - } - return productionFilePath.substring(stringBuilder.toString().length()).replace("\\","/"); - } - else{ - return ""; + private String getUnixFilePath(String filePath) { + String[] splitString = splitFilepath(filePath); + return filePath.substring(get5LevelsPath(splitString).toString().length()).replace(File.separator, "/"); + } + + private StringBuilder get5LevelsPath(String[] splitString) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < 5; i++) { + stringBuilder.append(splitString[i]).append(File.separator); } + return stringBuilder; + } + public String[] splitFilepath(String filePath) { + return filePath.split(Pattern.quote(File.separator)); } } diff --git a/src/test/java/edu/rit/se/testsmelss/TestFileTest.java b/src/test/java/edu/rit/se/testsmelss/TestFileTest.java new file mode 100644 index 0000000..4b2fdca --- /dev/null +++ b/src/test/java/edu/rit/se/testsmelss/TestFileTest.java @@ -0,0 +1,42 @@ +package edu.rit.se.testsmelss; + +import edu.rit.se.testsmells.TestFile; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestFileTest { + String fname; + TestFile sut; + + @Test + @EnabledOnOs({OS.WINDOWS}) + void splitFilepath() { + fname = "C:\\textfiles\\db\\query\\query.txt"; + sut = new TestFile(fname); + String[] splitted = sut.splitFilepath(fname); + assertEquals(5, splitted.length); + assertEquals("C:", splitted[0]); + assertEquals("textfiles", splitted[1]); + assertEquals("db", splitted[2]); + assertEquals("query", splitted[3]); + assertEquals("query.txt", splitted[4]); + } + + @Test + @EnabledOnOs({OS.LINUX}) + void splitUNIXFilepath() { + fname = "relativePath/textfiles/db/query/query.txt"; + sut = new TestFile(fname); + String[] splitted = sut.splitFilepath(fname); + assertEquals(5, splitted.length); + assertEquals("relativePath", splitted[0]); + assertEquals("textfiles", splitted[1]); + assertEquals("db", splitted[2]); + assertEquals("query", splitted[3]); + assertEquals("query.txt", splitted[4]); + } +}