James Moger
2012-10-01 eb1405f736f2f98e14215774dd53eea9b9a77017
commit | author | age
88598b 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.tests;
17
7e8873 18 import static org.junit.Assert.assertEquals;
JM 19 import static org.junit.Assert.assertTrue;
20
88598b 21 import java.io.File;
JM 22
7e8873 23 import org.junit.Test;
88598b 24
671c19 25 import com.gitblit.utils.FileUtils;
JM 26
7e8873 27 public class FileUtilsTest {
88598b 28
7e8873 29     @Test
88598b 30     public void testReadContent() throws Exception {
JM 31         File dir = new File(System.getProperty("user.dir"));
32         String rawContent = FileUtils.readContent(new File(dir, "LICENSE"), "\n");
33         assertTrue(rawContent.trim().startsWith("Apache License"));
34     }
7e8873 35
JM 36     @Test
143fc9 37     public void testWriteContent() throws Exception {
JM 38         String contentA = "this is a test";
39         File tmp = File.createTempFile("gitblit-", ".test");
40         FileUtils.writeContent(tmp, contentA);
41         String contentB = FileUtils.readContent(tmp, "\n").trim();
42         assertEquals(contentA, contentB);
43     }
88598b 44
7e8873 45     @Test
88598b 46     public void testFolderSize() throws Exception {
JM 47         assertEquals(-1, FileUtils.folderSize(null));
48         assertEquals(-1, FileUtils.folderSize(new File(System.getProperty("user.dir"), "pretend")));
49
50         File dir = new File(System.getProperty("user.dir"), "distrib");
51         long size = FileUtils.folderSize(dir);
52         assertTrue("size is actually " + size, size >= 470000L);
53
54         File file = new File(System.getProperty("user.dir"), "LICENSE");
55         size = FileUtils.folderSize(file);
7e8873 56         assertEquals("size is actually " + size, 11556L, size);
88598b 57     }
478678 58     
JM 59     @Test
60     public void testStringSizes() throws Exception {
61         assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50k", 0));
62         assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50m", 0));
63         assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2g", 0));
64
65         assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50kb", 0));
66         assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50mb", 0));
67         assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2gb", 0));
68
69         assertEquals(50L * FileUtils.KB, FileUtils.convertSizeToLong("50k", 0));
70         assertEquals(50L * FileUtils.MB, FileUtils.convertSizeToLong("50m", 0));
71         assertEquals(50L * FileUtils.GB, FileUtils.convertSizeToLong("50g", 0));
72
73         assertEquals(50L * FileUtils.KB, FileUtils.convertSizeToLong("50kb", 0));
74         assertEquals(50L * FileUtils.MB, FileUtils.convertSizeToLong("50mb", 0));
75         assertEquals(50L * FileUtils.GB, FileUtils.convertSizeToLong("50gb", 0));
76         
77         assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50 k", 0));
78         assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50 m", 0));
79         assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2 g", 0));
80
81         assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50 kb", 0));
82         assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50 mb", 0));
83         assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2 gb", 0));
84
85     }
88598b 86 }