1   package com.atlassian.core.util;
2   
3   import junit.framework.TestCase;
4   import com.atlassian.core.util.FileSize;
5   
6   public class TestFileSize extends TestCase
7   {
8       public TestFileSize(String s)
9       {
10          super(s);
11      }
12  
13      public void testLonglong()
14      {
15          assertEquals(FileSize.format(100000), FileSize.format(new Long(100000)));
16      }
17  
18      public void testFormat()
19      {
20          assertEquals("0.5 kB", FileSize.format(512));
21          assertEquals("1.0 kB", FileSize.format(1024));
22          assertEquals("2 kB", FileSize.format(2048));
23          assertEquals("400 kB", FileSize.format(1024 * 400));
24          assertEquals("1024 kB", FileSize.format(1024 * 1024));
25          assertEquals("1.20 MB", FileSize.format((long) (1024 * 1024 * 1.2)));
26          assertEquals("20.00 MB", FileSize.format(1024 * 1024 * 20));
27      }
28  
29      public void testFormatLessThan1KB()
30      {
31          // The javadoc says that < 1K will be format to KB to one decimal place.
32          // 460/1024 = 0.44921875
33          assertEquals("0.4 kB", FileSize.format(460));
34          // 461/1024 = 0.450195313
35          assertEquals("0.5 kB", FileSize.format(461));
36      }
37  
38      public void testFormatLessThan1MB()
39      {
40          // The javadoc says Anything between a kilobyte and a megabyte is presented in kilobytes to zero decimal places.
41  
42          // You can see that depending on the exact number of bytes, you can get either "1.0 kB", or "1 kB".
43          // This is inconsistent.
44          assertEquals("1.0 kB", FileSize.format(1023));
45          assertEquals("1.0 kB", FileSize.format(1024));
46          assertEquals("1 kB", FileSize.format(1025));
47  
48          // check the rounding
49          // 3583/1024 = 3.499023437 should round down to 3
50          assertEquals("3 kB", FileSize.format(3583));
51          // 3584/1024 = 3.5 should round up to 4
52          assertEquals("4 kB", FileSize.format(3584));
53      }
54  
55      public void testFormatGreaterThan1GB()
56      {
57          // Javadoc says "Anything greater than one megabyte is presented in megabytes to two decimal places."
58          // Note that 1 GiB = 1048576 B.
59          assertEquals("1024 kB", FileSize.format(1048575));
60          assertEquals("1024 kB", FileSize.format(1048576));
61          assertEquals("1.00 MB", FileSize.format(1048577));
62  
63          // check the rounding.
64          // 1294991/1048576 = 1.234999657 should round down to 1.23
65          assertEquals("1.23 MB", FileSize.format(1294991));
66          // 1294992/1048576 = 1.23500061 should round up to 1.24
67          assertEquals("1.24 MB", FileSize.format(1294992));
68      }
69  }