選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FileUtilTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import junit.framework.TestCase;
  47. public class FileUtilTest extends TestCase {
  48. private final File trash = new File(new File("target"), "trash");
  49. @Override
  50. protected void setUp() throws Exception {
  51. assertTrue(trash.mkdirs());
  52. }
  53. @Override
  54. protected void tearDown() throws Exception {
  55. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  56. }
  57. public void testDeleteFile() throws IOException {
  58. File f = new File(trash, "test");
  59. assertTrue(f.createNewFile());
  60. FileUtils.delete(f);
  61. assertFalse(f.exists());
  62. try {
  63. FileUtils.delete(f);
  64. fail("deletion of non-existing file must fail");
  65. } catch (IOException e) {
  66. // expected
  67. }
  68. try {
  69. FileUtils.delete(f, FileUtils.SKIP_MISSING);
  70. } catch (IOException e) {
  71. fail("deletion of non-existing file must not fail with option SKIP_MISSING");
  72. }
  73. }
  74. public void testDeleteRecursive() throws IOException {
  75. File f1 = new File(trash, "test/test/a");
  76. f1.mkdirs();
  77. f1.createNewFile();
  78. File f2 = new File(trash, "test/test/b");
  79. f2.createNewFile();
  80. File d = new File(trash, "test");
  81. FileUtils.delete(d, FileUtils.RECURSIVE);
  82. assertFalse(d.exists());
  83. try {
  84. FileUtils.delete(d, FileUtils.RECURSIVE);
  85. fail("recursive deletion of non-existing directory must fail");
  86. } catch (IOException e) {
  87. // expected
  88. }
  89. try {
  90. FileUtils.delete(d, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  91. } catch (IOException e) {
  92. fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING");
  93. }
  94. }
  95. public void testMkdir() throws IOException {
  96. File d = new File(trash, "test");
  97. FileUtils.mkdir(d);
  98. assertTrue(d.exists() && d.isDirectory());
  99. try {
  100. FileUtils.mkdir(d);
  101. fail("creation of existing directory must fail");
  102. } catch (IOException e) {
  103. // expected
  104. }
  105. FileUtils.mkdir(d, true);
  106. assertTrue(d.exists() && d.isDirectory());
  107. assertTrue(d.delete());
  108. File f = new File(trash, "test");
  109. assertTrue(f.createNewFile());
  110. try {
  111. FileUtils.mkdir(d);
  112. fail("creation of directory having same path as existing file must"
  113. + " fail");
  114. } catch (IOException e) {
  115. // expected
  116. }
  117. assertTrue(f.delete());
  118. }
  119. public void testMkdirs() throws IOException {
  120. File root = new File(trash, "test");
  121. assertTrue(root.mkdir());
  122. File d = new File(root, "test/test");
  123. FileUtils.mkdirs(d);
  124. assertTrue(d.exists() && d.isDirectory());
  125. try {
  126. FileUtils.mkdirs(d);
  127. fail("creation of existing directory hierarchy must fail");
  128. } catch (IOException e) {
  129. // expected
  130. }
  131. FileUtils.mkdirs(d, true);
  132. assertTrue(d.exists() && d.isDirectory());
  133. FileUtils.delete(root, FileUtils.RECURSIVE);
  134. File f = new File(trash, "test");
  135. assertTrue(f.createNewFile());
  136. try {
  137. FileUtils.mkdirs(d);
  138. fail("creation of directory having path conflicting with existing"
  139. + " file must fail");
  140. } catch (IOException e) {
  141. // expected
  142. }
  143. assertTrue(f.delete());
  144. }
  145. }