You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtilsTest.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  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. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.File;
  20. import org.junit.Test;
  21. import com.gitblit.utils.FileUtils;
  22. public class FileUtilsTest {
  23. @Test
  24. public void testReadContent() throws Exception {
  25. File dir = new File(System.getProperty("user.dir"));
  26. String rawContent = FileUtils.readContent(new File(dir, "LICENSE"), "\n");
  27. assertTrue(rawContent.trim().startsWith("Apache License"));
  28. }
  29. @Test
  30. public void testWriteContent() throws Exception {
  31. String contentA = "this is a test";
  32. File tmp = File.createTempFile("gitblit-", ".test");
  33. FileUtils.writeContent(tmp, contentA);
  34. String contentB = FileUtils.readContent(tmp, "\n").trim();
  35. assertEquals(contentA, contentB);
  36. }
  37. @Test
  38. public void testFolderSize() throws Exception {
  39. assertEquals(-1, FileUtils.folderSize(null));
  40. assertEquals(-1, FileUtils.folderSize(new File(System.getProperty("user.dir"), "pretend")));
  41. File dir = new File(System.getProperty("user.dir"), "distrib");
  42. long size = FileUtils.folderSize(dir);
  43. assertTrue("size is actually " + size, size >= 470000L);
  44. File file = new File(System.getProperty("user.dir"), "LICENSE");
  45. size = FileUtils.folderSize(file);
  46. assertEquals("size is actually " + size, 11556L, size);
  47. }
  48. @Test
  49. public void testStringSizes() throws Exception {
  50. assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50k", 0));
  51. assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50m", 0));
  52. assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2g", 0));
  53. assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50kb", 0));
  54. assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50mb", 0));
  55. assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2gb", 0));
  56. assertEquals(50L * FileUtils.KB, FileUtils.convertSizeToLong("50k", 0));
  57. assertEquals(50L * FileUtils.MB, FileUtils.convertSizeToLong("50m", 0));
  58. assertEquals(50L * FileUtils.GB, FileUtils.convertSizeToLong("50g", 0));
  59. assertEquals(50L * FileUtils.KB, FileUtils.convertSizeToLong("50kb", 0));
  60. assertEquals(50L * FileUtils.MB, FileUtils.convertSizeToLong("50mb", 0));
  61. assertEquals(50L * FileUtils.GB, FileUtils.convertSizeToLong("50gb", 0));
  62. assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50 k", 0));
  63. assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50 m", 0));
  64. assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2 g", 0));
  65. assertEquals(50 * FileUtils.KB, FileUtils.convertSizeToInt("50 kb", 0));
  66. assertEquals(50 * FileUtils.MB, FileUtils.convertSizeToInt("50 mb", 0));
  67. assertEquals(2 * FileUtils.GB, FileUtils.convertSizeToInt("2 gb", 0));
  68. }
  69. }