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.

FileUtilTest.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 static org.hamcrest.MatcherAssert.assertThat;
  45. import static org.hamcrest.Matchers.endsWith;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import org.junit.After;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class FileUtilTest {
  55. private final File trash = new File(new File("target"), "trash");
  56. @Before
  57. public void setUp() throws Exception {
  58. assertTrue(trash.mkdirs());
  59. }
  60. @After
  61. public void tearDown() throws Exception {
  62. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  63. }
  64. @Test
  65. public void testDeleteFile() throws IOException {
  66. File f = new File(trash, "test");
  67. FileUtils.createNewFile(f);
  68. FileUtils.delete(f);
  69. assertFalse(f.exists());
  70. try {
  71. FileUtils.delete(f);
  72. fail("deletion of non-existing file must fail");
  73. } catch (IOException e) {
  74. // expected
  75. }
  76. try {
  77. FileUtils.delete(f, FileUtils.SKIP_MISSING);
  78. } catch (IOException e) {
  79. fail("deletion of non-existing file must not fail with option SKIP_MISSING");
  80. }
  81. }
  82. @Test
  83. public void testDeleteRecursive() throws IOException {
  84. File f1 = new File(trash, "test/test/a");
  85. FileUtils.mkdirs(f1.getParentFile());
  86. FileUtils.createNewFile(f1);
  87. File f2 = new File(trash, "test/test/b");
  88. FileUtils.createNewFile(f2);
  89. File d = new File(trash, "test");
  90. FileUtils.delete(d, FileUtils.RECURSIVE);
  91. assertFalse(d.exists());
  92. try {
  93. FileUtils.delete(d, FileUtils.RECURSIVE);
  94. fail("recursive deletion of non-existing directory must fail");
  95. } catch (IOException e) {
  96. // expected
  97. }
  98. try {
  99. FileUtils.delete(d, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  100. } catch (IOException e) {
  101. fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING");
  102. }
  103. }
  104. @Test
  105. public void testDeleteRecursiveEmpty() throws IOException {
  106. File f1 = new File(trash, "test/test/a");
  107. File f2 = new File(trash, "test/a");
  108. File d1 = new File(trash, "test");
  109. File d2 = new File(trash, "test/test");
  110. File d3 = new File(trash, "test/b");
  111. FileUtils.mkdirs(f1.getParentFile());
  112. FileUtils.createNewFile(f2);
  113. FileUtils.createNewFile(f1);
  114. FileUtils.mkdirs(d3);
  115. // Cannot delete hierarchy since files exist
  116. try {
  117. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  118. fail("delete should fail");
  119. } catch (IOException e1) {
  120. try {
  121. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY|FileUtils.RECURSIVE);
  122. fail("delete should fail");
  123. } catch (IOException e2) {
  124. // Everything still there
  125. assertTrue(f1.exists());
  126. assertTrue(f2.exists());
  127. assertTrue(d1.exists());
  128. assertTrue(d2.exists());
  129. assertTrue(d3.exists());
  130. }
  131. }
  132. // setup: delete files, only directories left
  133. assertTrue(f1.delete());
  134. assertTrue(f2.delete());
  135. // Shall not delete hierarchy without recursive
  136. try {
  137. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  138. fail("delete should fail");
  139. } catch (IOException e2) {
  140. // Everything still there
  141. assertTrue(d1.exists());
  142. assertTrue(d2.exists());
  143. assertTrue(d3.exists());
  144. }
  145. // Now delete the empty hierarchy
  146. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  147. | FileUtils.RECURSIVE);
  148. assertFalse(d2.exists());
  149. // Will fail to delete non-existing without SKIP_MISSING
  150. try {
  151. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY);
  152. fail("Cannot delete non-existent entity");
  153. } catch (IOException e) {
  154. // ok
  155. }
  156. // ..with SKIP_MISSING there is no exception
  157. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  158. | FileUtils.SKIP_MISSING);
  159. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  160. | FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  161. // essentially the same, using IGNORE_ERRORS
  162. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  163. | FileUtils.IGNORE_ERRORS);
  164. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  165. | FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
  166. }
  167. @Test
  168. public void testDeleteRecursiveEmptyDirectoriesOnlyButIsFile()
  169. throws IOException {
  170. File f1 = new File(trash, "test/test/a");
  171. FileUtils.mkdirs(f1.getParentFile());
  172. FileUtils.createNewFile(f1);
  173. try {
  174. FileUtils.delete(f1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  175. fail("delete should fail");
  176. } catch (IOException e) {
  177. assertTrue(f1.exists());
  178. }
  179. }
  180. @Test
  181. public void testMkdir() throws IOException {
  182. File d = new File(trash, "test");
  183. FileUtils.mkdir(d);
  184. assertTrue(d.exists() && d.isDirectory());
  185. try {
  186. FileUtils.mkdir(d);
  187. fail("creation of existing directory must fail");
  188. } catch (IOException e) {
  189. // expected
  190. }
  191. FileUtils.mkdir(d, true);
  192. assertTrue(d.exists() && d.isDirectory());
  193. assertTrue(d.delete());
  194. File f = new File(trash, "test");
  195. FileUtils.createNewFile(f);
  196. try {
  197. FileUtils.mkdir(d);
  198. fail("creation of directory having same path as existing file must"
  199. + " fail");
  200. } catch (IOException e) {
  201. // expected
  202. }
  203. assertTrue(f.delete());
  204. }
  205. @Test
  206. public void testMkdirs() throws IOException {
  207. File root = new File(trash, "test");
  208. assertTrue(root.mkdir());
  209. File d = new File(root, "test/test");
  210. FileUtils.mkdirs(d);
  211. assertTrue(d.exists() && d.isDirectory());
  212. try {
  213. FileUtils.mkdirs(d);
  214. fail("creation of existing directory hierarchy must fail");
  215. } catch (IOException e) {
  216. // expected
  217. }
  218. FileUtils.mkdirs(d, true);
  219. assertTrue(d.exists() && d.isDirectory());
  220. FileUtils.delete(root, FileUtils.RECURSIVE);
  221. File f = new File(trash, "test");
  222. FileUtils.createNewFile(f);
  223. try {
  224. FileUtils.mkdirs(d);
  225. fail("creation of directory having path conflicting with existing"
  226. + " file must fail");
  227. } catch (IOException e) {
  228. // expected
  229. }
  230. assertTrue(f.delete());
  231. }
  232. @Test
  233. public void testCreateNewFile() throws IOException {
  234. File f = new File(trash, "x");
  235. FileUtils.createNewFile(f);
  236. assertTrue(f.exists());
  237. try {
  238. FileUtils.createNewFile(f);
  239. fail("creation of already existing file must fail");
  240. } catch (IOException e) {
  241. // expected
  242. }
  243. FileUtils.delete(f);
  244. }
  245. @Test
  246. public void testDeleteEmptyTreeOk() throws IOException {
  247. File t = new File(trash, "t");
  248. FileUtils.mkdir(t);
  249. FileUtils.mkdir(new File(t, "d"));
  250. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  251. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  252. assertFalse(t.exists());
  253. }
  254. @Test
  255. public void testDeleteNotEmptyTreeNotOk() throws IOException {
  256. File t = new File(trash, "t");
  257. FileUtils.mkdir(t);
  258. FileUtils.mkdir(new File(t, "d"));
  259. File f = new File(new File(t, "d"), "f");
  260. FileUtils.createNewFile(f);
  261. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  262. try {
  263. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  264. fail("expected failure to delete f");
  265. } catch (IOException e) {
  266. assertThat(e.getMessage(), endsWith(f.getAbsolutePath()));
  267. }
  268. assertTrue(t.exists());
  269. }
  270. @Test
  271. public void testDeleteNotEmptyTreeNotOkButIgnoreFail() throws IOException {
  272. File t = new File(trash, "t");
  273. FileUtils.mkdir(t);
  274. FileUtils.mkdir(new File(t, "d"));
  275. File f = new File(new File(t, "d"), "f");
  276. FileUtils.createNewFile(f);
  277. File e = new File(new File(t, "d"), "e");
  278. FileUtils.mkdir(e);
  279. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE
  280. | FileUtils.IGNORE_ERRORS);
  281. // Should have deleted as much as possible, but not all
  282. assertTrue(t.exists());
  283. assertTrue(f.exists());
  284. assertFalse(e.exists());
  285. }
  286. }