Skip to content

Commit

Permalink
Merge pull request #691 from fredg02/JENKINS-16630
Browse files Browse the repository at this point in the history
[FIXED JENKINS-16630] Human readable file size method returns ",00" for files with byte length 0
  • Loading branch information
kutzi committed Feb 17, 2013
2 parents 17fabea + 4bf431c commit ad64bcd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/Functions.java
Expand Up @@ -1548,6 +1548,9 @@ public String getUserAvatar(User user, String avatarSize) {
*/
public static String humanReadableByteSize(long size){
String measure = "B";
if(size < 1024){
return size + " " + measure;
}
Double number = new Double(size);
if(number>=1024){
number = number/1024;
Expand All @@ -1561,7 +1564,7 @@ public static String humanReadableByteSize(long size){
}
}
}
DecimalFormat format = new DecimalFormat("##.00");
DecimalFormat format = new DecimalFormat("#0.00");
return format.format(number) + " " + measure;
}
}
22 changes: 22 additions & 0 deletions core/src/test/java/hudson/FunctionsTest.java
Expand Up @@ -23,6 +23,8 @@
*/
package hudson;

import java.util.Locale;

import hudson.model.Action;
import static org.junit.Assert.*;
import org.junit.Test;
Expand Down Expand Up @@ -117,4 +119,24 @@ private static StaplerRequest createMockRequest(String contextPath) {
when(req.getContextPath()).thenReturn(contextPath);
return req;
}

@Test
@Bug(16630)
public void testHumanReadableFileSize(){
Locale defaultLocale = Locale.getDefault();
try{
Locale.setDefault(Locale.ENGLISH);
assertEquals("0 B", Functions.humanReadableByteSize(0));
assertEquals("1023 B", Functions.humanReadableByteSize(1023));
assertEquals("1.00 KB", Functions.humanReadableByteSize(1024));
assertEquals("1.50 KB", Functions.humanReadableByteSize(1536));
assertEquals("20.00 KB", Functions.humanReadableByteSize(20480));
assertEquals("1023.00 KB", Functions.humanReadableByteSize(1047552));
assertEquals("1.00 MB", Functions.humanReadableByteSize(1048576));
assertEquals("1.50 GB", Functions.humanReadableByteSize(1610612700));
}finally{
Locale.setDefault(defaultLocale);
}
}

}

0 comments on commit ad64bcd

Please sign in to comment.