Skip to content

Commit

Permalink
[JENKINS-21419] Can't open sloccount table portlet
Browse files Browse the repository at this point in the history
- Links to specific folders in folder details don't work.
- All Windows backslashes are now replaced by Unix slashes at the very beginning. Only unix slashes are considered in the rest of the code, which significantly simplifies it and prevents possible errors.
- SloccountReport.extractFolder() is now used in FolderFileFilter.include() to fix the same issue that was present in extractFolder().
- Constant DIRECTORY_SEPARATOR constant is used instead of hardcoded "/".
  • Loading branch information
mixalturek committed Jan 18, 2014
1 parent 2174a03 commit 63baeef
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
10 changes: 3 additions & 7 deletions src/main/java/hudson/plugins/sloccount/SloccountResult.java
Expand Up @@ -71,7 +71,7 @@ private void convertLegacyData() {

if(report != null) {
for(Language language : report.getLanguages()){
languages.add(new SloccountLanguageStatistics(language.getName(),
languages.add(new SloccountLanguageStatistics(language.getName(),
language.getLineCount(), language.getFileCount()));
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public SloccountResult getLanguageResult(String language){
}

public SloccountResult getFolderResult(String jumbledFolder){
String folder = jumbledFolder.replace("|", System.getProperty("file.separator"));
String folder = jumbledFolder.replace("|", SloccountReport.DIRECTORY_SEPARATOR);
SloccountReport filtered = new SloccountReport(this.getReport(), new FolderFileFilter(folder));
return new BreadCrumbResult(filtered, this.owner, folder);
}
Expand Down Expand Up @@ -157,11 +157,7 @@ public FolderFileFilter(String folder){
}

public boolean include(File file) {
String separator = System.getProperty("file.separator");

int index = file.getName().lastIndexOf(separator);
String fileFolder = file.getName().substring(0, index);

String fileFolder = SloccountReport.extractFolder(file.getName());
return this.folder.equals(fileFolder);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/sloccount/model/Folder.java
Expand Up @@ -23,6 +23,6 @@ public void simplifyName(String rootPath){
}

public String getUrlName(){
return this.name.replace(System.getProperty("file.separator"), "|");
return this.name.replace(SloccountReport.DIRECTORY_SEPARATOR, "|");
}
}
37 changes: 15 additions & 22 deletions src/main/java/hudson/plugins/sloccount/model/SloccountReport.java
Expand Up @@ -14,6 +14,9 @@ public class SloccountReport extends FileContainer implements SloccountReportInt
/** Serial version UID. */
private static final long serialVersionUID = 0L;

/** Only Unix directory separator is used in the code. */
public static final String DIRECTORY_SEPARATOR = "/";

private Map<String, Folder> folders = new LinkedHashMap<String, Folder>();
private Map<String, Language> languages = new LinkedHashMap<String, Language>();

Expand All @@ -34,8 +37,11 @@ public SloccountReport(SloccountReport old, FileFilter filter){
}

public void add(String filePath, String languageName, int lineCount){
// Get rid of Microsoft's incompatibility once and forever
filePath = filePath.replace("\\", DIRECTORY_SEPARATOR);

String folderPath = extractFolder(filePath);

File file = new File(filePath, languageName, lineCount);
this.addFile(file);

Expand All @@ -55,33 +61,26 @@ public void add(String filePath, String languageName, int lineCount){
}

/**
* Extract directory part of a path. The method searches the directory
* separator from right in the following order: unix '/', windows '\'.
* Extract directory part of a path. The method searches first directory
* separator from right.
*
* Examples of input and output:
* (empty string) - (empty string)
* file.java - (empty string)
* /test/file.java - /test
* /cygdrive/c/test/file.java - /cygdrive/c/test
* c:\test\file.java - c:\test
* c:/test/file.java - c:/test
* /test/ - /test
* /test - (empty string) ... is it file or directory?
*
* @param filePath
* the path containing folders and file name
* the path containing folders and file name, Unix separators '/'
* are expected
* @return the path without the file name; if no separator is found in
* the input path an empty string will be returned
*/
public static String extractFolder(String filePath){
// Try Unix separator
int index = filePath.lastIndexOf("/");

if(index != -1) {
return filePath.substring(0, index);
}

// Try Windows separator
index = filePath.lastIndexOf("\\");
int index = filePath.lastIndexOf(DIRECTORY_SEPARATOR);

if(index != -1) {
return filePath.substring(0, index);
Expand Down Expand Up @@ -142,7 +141,7 @@ public String getRootFolder(){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < this.rootFolderPath.length; i++){
if(i > 0){
builder.append("/");
builder.append(DIRECTORY_SEPARATOR);
}
builder.append(this.rootFolderPath[i]);
}
Expand All @@ -151,13 +150,7 @@ public String getRootFolder(){
}

private void updateRootFolderPath(String newFolderName){
// Unix directory separator
String[] newFolderPath = newFolderName.split("/");

if(newFolderPath.length == 1){
// Windows directory separator '\'
newFolderPath = newFolderName.split("\\\\");
}
String[] newFolderPath = newFolderName.split(DIRECTORY_SEPARATOR);

if(this.rootFolderPath == null){
this.rootFolderPath = newFolderPath;
Expand Down
Expand Up @@ -20,9 +20,6 @@ public void testExtractFolder() {
Assert.assertEquals("c:/test",
SloccountReport.extractFolder("c:/test/file.java"));

Assert.assertEquals("c:\\test",
SloccountReport.extractFolder("c:\\test\\file.java"));

Assert.assertEquals("",
SloccountReport.extractFolder("file.java"));

Expand All @@ -32,9 +29,6 @@ public void testExtractFolder() {
Assert.assertEquals("test",
SloccountReport.extractFolder("test/file.java"));

Assert.assertEquals("test",
SloccountReport.extractFolder("test\\file.java"));

// It searches the separator from right
Assert.assertEquals("/test",
SloccountReport.extractFolder("/test/"));
Expand Down

0 comments on commit 63baeef

Please sign in to comment.