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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (C) 2010, 2013 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.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import org.eclipse.jgit.junit.JGitTestUtil;
  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 testDeleteRecursiveEmptyNeedsToCheckFilesFirst()
  169. throws IOException {
  170. File d1 = new File(trash, "test");
  171. File d2 = new File(trash, "test/a");
  172. File d3 = new File(trash, "test/b");
  173. File f1 = new File(trash, "test/c");
  174. File d4 = new File(trash, "test/d");
  175. FileUtils.mkdirs(d1);
  176. FileUtils.mkdirs(d2);
  177. FileUtils.mkdirs(d3);
  178. FileUtils.mkdirs(d4);
  179. FileUtils.createNewFile(f1);
  180. // Cannot delete hierarchy since file exists
  181. try {
  182. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY
  183. | FileUtils.RECURSIVE);
  184. fail("delete should fail");
  185. } catch (IOException e) {
  186. // Everything still there
  187. assertTrue(f1.exists());
  188. assertTrue(d1.exists());
  189. assertTrue(d2.exists());
  190. assertTrue(d3.exists());
  191. assertTrue(d4.exists());
  192. }
  193. }
  194. @Test
  195. public void testDeleteRecursiveEmptyDirectoriesOnlyButIsFile()
  196. throws IOException {
  197. File f1 = new File(trash, "test/test/a");
  198. FileUtils.mkdirs(f1.getParentFile());
  199. FileUtils.createNewFile(f1);
  200. try {
  201. FileUtils.delete(f1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  202. fail("delete should fail");
  203. } catch (IOException e) {
  204. assertTrue(f1.exists());
  205. }
  206. }
  207. @Test
  208. public void testMkdir() throws IOException {
  209. File d = new File(trash, "test");
  210. FileUtils.mkdir(d);
  211. assertTrue(d.exists() && d.isDirectory());
  212. try {
  213. FileUtils.mkdir(d);
  214. fail("creation of existing directory must fail");
  215. } catch (IOException e) {
  216. // expected
  217. }
  218. FileUtils.mkdir(d, true);
  219. assertTrue(d.exists() && d.isDirectory());
  220. assertTrue(d.delete());
  221. File f = new File(trash, "test");
  222. FileUtils.createNewFile(f);
  223. try {
  224. FileUtils.mkdir(d);
  225. fail("creation of directory having same path as existing file must"
  226. + " fail");
  227. } catch (IOException e) {
  228. // expected
  229. }
  230. assertTrue(f.delete());
  231. }
  232. @Test
  233. public void testMkdirs() throws IOException {
  234. File root = new File(trash, "test");
  235. assertTrue(root.mkdir());
  236. File d = new File(root, "test/test");
  237. FileUtils.mkdirs(d);
  238. assertTrue(d.exists() && d.isDirectory());
  239. try {
  240. FileUtils.mkdirs(d);
  241. fail("creation of existing directory hierarchy must fail");
  242. } catch (IOException e) {
  243. // expected
  244. }
  245. FileUtils.mkdirs(d, true);
  246. assertTrue(d.exists() && d.isDirectory());
  247. FileUtils.delete(root, FileUtils.RECURSIVE);
  248. File f = new File(trash, "test");
  249. FileUtils.createNewFile(f);
  250. try {
  251. FileUtils.mkdirs(d);
  252. fail("creation of directory having path conflicting with existing"
  253. + " file must fail");
  254. } catch (IOException e) {
  255. // expected
  256. }
  257. assertTrue(f.delete());
  258. }
  259. @Test
  260. public void testCreateNewFile() throws IOException {
  261. File f = new File(trash, "x");
  262. FileUtils.createNewFile(f);
  263. assertTrue(f.exists());
  264. try {
  265. FileUtils.createNewFile(f);
  266. fail("creation of already existing file must fail");
  267. } catch (IOException e) {
  268. // expected
  269. }
  270. FileUtils.delete(f);
  271. }
  272. @Test
  273. public void testDeleteEmptyTreeOk() throws IOException {
  274. File t = new File(trash, "t");
  275. FileUtils.mkdir(t);
  276. FileUtils.mkdir(new File(t, "d"));
  277. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  278. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  279. assertFalse(t.exists());
  280. }
  281. @Test
  282. public void testDeleteNotEmptyTreeNotOk() throws IOException {
  283. File t = new File(trash, "t");
  284. FileUtils.mkdir(t);
  285. FileUtils.mkdir(new File(t, "d"));
  286. File f = new File(new File(t, "d"), "f");
  287. FileUtils.createNewFile(f);
  288. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  289. try {
  290. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  291. fail("expected failure to delete f");
  292. } catch (IOException e) {
  293. assertTrue(e.getMessage().endsWith(f.getAbsolutePath()));
  294. }
  295. assertTrue(t.exists());
  296. }
  297. @Test
  298. public void testDeleteNotEmptyTreeNotOkButIgnoreFail() throws IOException {
  299. File t = new File(trash, "t");
  300. FileUtils.mkdir(t);
  301. FileUtils.mkdir(new File(t, "d"));
  302. File f = new File(new File(t, "d"), "f");
  303. FileUtils.createNewFile(f);
  304. File e = new File(new File(t, "d"), "e");
  305. FileUtils.mkdir(e);
  306. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE
  307. | FileUtils.IGNORE_ERRORS);
  308. // Should have deleted as much as possible, but not all
  309. assertTrue(t.exists());
  310. assertTrue(f.exists());
  311. assertFalse(e.exists());
  312. }
  313. @Test
  314. public void testRenameOverNonExistingFile() throws IOException {
  315. File d = new File(trash, "d");
  316. FileUtils.mkdirs(d);
  317. File f1 = new File(trash, "d/f");
  318. File f2 = new File(trash, "d/g");
  319. JGitTestUtil.write(f1, "f1");
  320. // test
  321. FileUtils.rename(f1, f2);
  322. assertFalse(f1.exists());
  323. assertTrue(f2.exists());
  324. assertEquals("f1", JGitTestUtil.read(f2));
  325. }
  326. @Test
  327. public void testRenameOverExistingFile() throws IOException {
  328. File d = new File(trash, "d");
  329. FileUtils.mkdirs(d);
  330. File f1 = new File(trash, "d/f");
  331. File f2 = new File(trash, "d/g");
  332. JGitTestUtil.write(f1, "f1");
  333. JGitTestUtil.write(f2, "f2");
  334. // test
  335. FileUtils.rename(f1, f2);
  336. assertFalse(f1.exists());
  337. assertTrue(f2.exists());
  338. assertEquals("f1", JGitTestUtil.read(f2));
  339. }
  340. @Test
  341. public void testRenameOverExistingNonEmptyDirectory() throws IOException {
  342. File d = new File(trash, "d");
  343. FileUtils.mkdirs(d);
  344. File f1 = new File(trash, "d/f");
  345. File f2 = new File(trash, "d/g");
  346. File d1 = new File(trash, "d/g/h/i");
  347. File f3 = new File(trash, "d/g/h/f");
  348. FileUtils.mkdirs(d1);
  349. JGitTestUtil.write(f1, "f1");
  350. JGitTestUtil.write(f3, "f3");
  351. // test
  352. try {
  353. FileUtils.rename(f1, f2);
  354. fail("rename to non-empty directory should fail");
  355. } catch (IOException e) {
  356. assertEquals("f1", JGitTestUtil.read(f1)); // untouched source
  357. assertEquals("f3", JGitTestUtil.read(f3)); // untouched
  358. // empty directories within f2 may or may not have been deleted
  359. }
  360. }
  361. @Test
  362. public void testRenameOverExistingEmptyDirectory() throws IOException {
  363. File d = new File(trash, "d");
  364. FileUtils.mkdirs(d);
  365. File f1 = new File(trash, "d/f");
  366. File f2 = new File(trash, "d/g");
  367. File d1 = new File(trash, "d/g/h/i");
  368. FileUtils.mkdirs(d1);
  369. JGitTestUtil.write(f1, "f1");
  370. // test
  371. FileUtils.rename(f1, f2);
  372. assertFalse(f1.exists());
  373. assertTrue(f2.exists());
  374. assertEquals("f1", JGitTestUtil.read(f2));
  375. }
  376. @Test
  377. public void testCreateSymlink() throws IOException {
  378. FS fs = FS.DETECTED;
  379. try {
  380. fs.createSymLink(new File(trash, "x"), "y");
  381. } catch (IOException e) {
  382. if (fs.supportsSymlinks())
  383. fail("FS claims to support symlinks but attempt to create symlink failed");
  384. return;
  385. }
  386. assertTrue(fs.supportsSymlinks());
  387. String target = fs.readSymLink(new File(trash, "x"));
  388. assertEquals("y", target);
  389. }
  390. }