Skip to content

Add Linux path separator support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions TestFileMapping.iml

This file was deleted.

31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@

<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>edu.rit.se.testsmells.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -38,6 +58,17 @@
<artifactId>opencsv</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/rit/se/testsmells/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/edu/rit/se/testsmells/MappingDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -76,9 +76,7 @@ private boolean isFileSyntacticallyValid(String filePath) {

public class FindJavaTestFilesVisitor extends SimpleFileVisitor<Path> {
@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;
Expand Down
57 changes: 30 additions & 27 deletions src/main/java/edu/rit/se/testsmells/TestFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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() {
Expand All @@ -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));
}
}
42 changes: 42 additions & 0 deletions src/test/java/edu/rit/se/testsmelss/TestFileTest.java
Original file line number Diff line number Diff line change
@@ -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]);
}
}