Skip to content
Permalink
Browse files
Merge pull request #691 from fredg02/JENKINS-16630
[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 ad64bcd61201a43d850fd5e6151d8503e659cffe
Showing with 26 additions and 1 deletion.
  1. +4 −1 core/src/main/java/hudson/Functions.java
  2. +22 −0 core/src/test/java/hudson/FunctionsTest.java
@@ -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;
@@ -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;
}
}
@@ -23,6 +23,8 @@
*/
package hudson;

import java.util.Locale;

import hudson.model.Action;
import static org.junit.Assert.*;
import org.junit.Test;
@@ -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.