View Javadoc

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