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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 java.util.regex.Matcher;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.junit.After;
  53. import org.junit.Assume;
  54. import org.junit.Before;
  55. import org.junit.Test;
  56. public class FileUtilTest {
  57. private File trash;
  58. @Before
  59. public void setUp() throws Exception {
  60. trash = File.createTempFile("tmp_", "");
  61. trash.delete();
  62. assertTrue("mkdir " + trash, trash.mkdir());
  63. }
  64. @After
  65. public void tearDown() throws Exception {
  66. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  67. }
  68. @Test
  69. public void testDeleteFile() throws IOException {
  70. File f = new File(trash, "test");
  71. FileUtils.createNewFile(f);
  72. FileUtils.delete(f);
  73. assertFalse(f.exists());
  74. try {
  75. FileUtils.delete(f);
  76. fail("deletion of non-existing file must fail");
  77. } catch (IOException e) {
  78. // expected
  79. }
  80. try {
  81. FileUtils.delete(f, FileUtils.SKIP_MISSING);
  82. } catch (IOException e) {
  83. fail("deletion of non-existing file must not fail with option SKIP_MISSING");
  84. }
  85. }
  86. @Test
  87. public void testDeleteRecursive() throws IOException {
  88. File f1 = new File(trash, "test/test/a");
  89. FileUtils.mkdirs(f1.getParentFile());
  90. FileUtils.createNewFile(f1);
  91. File f2 = new File(trash, "test/test/b");
  92. FileUtils.createNewFile(f2);
  93. File d = new File(trash, "test");
  94. FileUtils.delete(d, FileUtils.RECURSIVE);
  95. assertFalse(d.exists());
  96. try {
  97. FileUtils.delete(d, FileUtils.RECURSIVE);
  98. fail("recursive deletion of non-existing directory must fail");
  99. } catch (IOException e) {
  100. // expected
  101. }
  102. try {
  103. FileUtils.delete(d, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  104. } catch (IOException e) {
  105. fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING");
  106. }
  107. }
  108. @Test
  109. public void testDeleteRecursiveEmpty() throws IOException {
  110. File f1 = new File(trash, "test/test/a");
  111. File f2 = new File(trash, "test/a");
  112. File d1 = new File(trash, "test");
  113. File d2 = new File(trash, "test/test");
  114. File d3 = new File(trash, "test/b");
  115. FileUtils.mkdirs(f1.getParentFile());
  116. FileUtils.createNewFile(f2);
  117. FileUtils.createNewFile(f1);
  118. FileUtils.mkdirs(d3);
  119. // Cannot delete hierarchy since files exist
  120. try {
  121. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  122. fail("delete should fail");
  123. } catch (IOException e1) {
  124. try {
  125. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY|FileUtils.RECURSIVE);
  126. fail("delete should fail");
  127. } catch (IOException e2) {
  128. // Everything still there
  129. assertTrue(f1.exists());
  130. assertTrue(f2.exists());
  131. assertTrue(d1.exists());
  132. assertTrue(d2.exists());
  133. assertTrue(d3.exists());
  134. }
  135. }
  136. // setup: delete files, only directories left
  137. assertTrue(f1.delete());
  138. assertTrue(f2.delete());
  139. // Shall not delete hierarchy without recursive
  140. try {
  141. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  142. fail("delete should fail");
  143. } catch (IOException e2) {
  144. // Everything still there
  145. assertTrue(d1.exists());
  146. assertTrue(d2.exists());
  147. assertTrue(d3.exists());
  148. }
  149. // Now delete the empty hierarchy
  150. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  151. | FileUtils.RECURSIVE);
  152. assertFalse(d2.exists());
  153. // Will fail to delete non-existing without SKIP_MISSING
  154. try {
  155. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY);
  156. fail("Cannot delete non-existent entity");
  157. } catch (IOException e) {
  158. // ok
  159. }
  160. // ..with SKIP_MISSING there is no exception
  161. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  162. | FileUtils.SKIP_MISSING);
  163. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  164. | FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  165. // essentially the same, using IGNORE_ERRORS
  166. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  167. | FileUtils.IGNORE_ERRORS);
  168. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  169. | FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
  170. }
  171. @Test
  172. public void testDeleteRecursiveEmptyNeedsToCheckFilesFirst()
  173. throws IOException {
  174. File d1 = new File(trash, "test");
  175. File d2 = new File(trash, "test/a");
  176. File d3 = new File(trash, "test/b");
  177. File f1 = new File(trash, "test/c");
  178. File d4 = new File(trash, "test/d");
  179. FileUtils.mkdirs(d1);
  180. FileUtils.mkdirs(d2);
  181. FileUtils.mkdirs(d3);
  182. FileUtils.mkdirs(d4);
  183. FileUtils.createNewFile(f1);
  184. // Cannot delete hierarchy since file exists
  185. try {
  186. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY
  187. | FileUtils.RECURSIVE);
  188. fail("delete should fail");
  189. } catch (IOException e) {
  190. // Everything still there
  191. assertTrue(f1.exists());
  192. assertTrue(d1.exists());
  193. assertTrue(d2.exists());
  194. assertTrue(d3.exists());
  195. assertTrue(d4.exists());
  196. }
  197. }
  198. @Test
  199. public void testDeleteRecursiveEmptyDirectoriesOnlyButIsFile()
  200. throws IOException {
  201. File f1 = new File(trash, "test/test/a");
  202. FileUtils.mkdirs(f1.getParentFile());
  203. FileUtils.createNewFile(f1);
  204. try {
  205. FileUtils.delete(f1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  206. fail("delete should fail");
  207. } catch (IOException e) {
  208. assertTrue(f1.exists());
  209. }
  210. }
  211. @Test
  212. public void testMkdir() throws IOException {
  213. File d = new File(trash, "test");
  214. FileUtils.mkdir(d);
  215. assertTrue(d.exists() && d.isDirectory());
  216. try {
  217. FileUtils.mkdir(d);
  218. fail("creation of existing directory must fail");
  219. } catch (IOException e) {
  220. // expected
  221. }
  222. FileUtils.mkdir(d, true);
  223. assertTrue(d.exists() && d.isDirectory());
  224. assertTrue(d.delete());
  225. File f = new File(trash, "test");
  226. FileUtils.createNewFile(f);
  227. try {
  228. FileUtils.mkdir(d);
  229. fail("creation of directory having same path as existing file must"
  230. + " fail");
  231. } catch (IOException e) {
  232. // expected
  233. }
  234. assertTrue(f.delete());
  235. }
  236. @Test
  237. public void testMkdirs() throws IOException {
  238. File root = new File(trash, "test");
  239. assertTrue(root.mkdir());
  240. File d = new File(root, "test/test");
  241. FileUtils.mkdirs(d);
  242. assertTrue(d.exists() && d.isDirectory());
  243. try {
  244. FileUtils.mkdirs(d);
  245. fail("creation of existing directory hierarchy must fail");
  246. } catch (IOException e) {
  247. // expected
  248. }
  249. FileUtils.mkdirs(d, true);
  250. assertTrue(d.exists() && d.isDirectory());
  251. FileUtils.delete(root, FileUtils.RECURSIVE);
  252. File f = new File(trash, "test");
  253. FileUtils.createNewFile(f);
  254. try {
  255. FileUtils.mkdirs(d);
  256. fail("creation of directory having path conflicting with existing"
  257. + " file must fail");
  258. } catch (IOException e) {
  259. // expected
  260. }
  261. assertTrue(f.delete());
  262. }
  263. @Test
  264. public void testCreateNewFile() throws IOException {
  265. File f = new File(trash, "x");
  266. FileUtils.createNewFile(f);
  267. assertTrue(f.exists());
  268. try {
  269. FileUtils.createNewFile(f);
  270. fail("creation of already existing file must fail");
  271. } catch (IOException e) {
  272. // expected
  273. }
  274. FileUtils.delete(f);
  275. }
  276. @Test
  277. public void testDeleteEmptyTreeOk() throws IOException {
  278. File t = new File(trash, "t");
  279. FileUtils.mkdir(t);
  280. FileUtils.mkdir(new File(t, "d"));
  281. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  282. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  283. assertFalse(t.exists());
  284. }
  285. @Test
  286. public void testDeleteNotEmptyTreeNotOk() throws IOException {
  287. File t = new File(trash, "t");
  288. FileUtils.mkdir(t);
  289. FileUtils.mkdir(new File(t, "d"));
  290. File f = new File(new File(t, "d"), "f");
  291. FileUtils.createNewFile(f);
  292. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  293. try {
  294. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  295. fail("expected failure to delete f");
  296. } catch (IOException e) {
  297. assertTrue(e.getMessage().endsWith(f.getAbsolutePath()));
  298. }
  299. assertTrue(t.exists());
  300. }
  301. @Test
  302. public void testDeleteNotEmptyTreeNotOkButIgnoreFail() throws IOException {
  303. File t = new File(trash, "t");
  304. FileUtils.mkdir(t);
  305. FileUtils.mkdir(new File(t, "d"));
  306. File f = new File(new File(t, "d"), "f");
  307. FileUtils.createNewFile(f);
  308. File e = new File(new File(t, "d"), "e");
  309. FileUtils.mkdir(e);
  310. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE
  311. | FileUtils.IGNORE_ERRORS);
  312. // Should have deleted as much as possible, but not all
  313. assertTrue(t.exists());
  314. assertTrue(f.exists());
  315. assertFalse(e.exists());
  316. }
  317. @Test
  318. public void testRenameOverNonExistingFile() throws IOException {
  319. File d = new File(trash, "d");
  320. FileUtils.mkdirs(d);
  321. File f1 = new File(trash, "d/f");
  322. File f2 = new File(trash, "d/g");
  323. JGitTestUtil.write(f1, "f1");
  324. // test
  325. FileUtils.rename(f1, f2);
  326. assertFalse(f1.exists());
  327. assertTrue(f2.exists());
  328. assertEquals("f1", JGitTestUtil.read(f2));
  329. }
  330. @Test
  331. public void testRenameOverExistingFile() throws IOException {
  332. File d = new File(trash, "d");
  333. FileUtils.mkdirs(d);
  334. File f1 = new File(trash, "d/f");
  335. File f2 = new File(trash, "d/g");
  336. JGitTestUtil.write(f1, "f1");
  337. JGitTestUtil.write(f2, "f2");
  338. // test
  339. FileUtils.rename(f1, f2);
  340. assertFalse(f1.exists());
  341. assertTrue(f2.exists());
  342. assertEquals("f1", JGitTestUtil.read(f2));
  343. }
  344. @Test
  345. public void testRenameOverExistingNonEmptyDirectory() throws IOException {
  346. File d = new File(trash, "d");
  347. FileUtils.mkdirs(d);
  348. File f1 = new File(trash, "d/f");
  349. File f2 = new File(trash, "d/g");
  350. File d1 = new File(trash, "d/g/h/i");
  351. File f3 = new File(trash, "d/g/h/f");
  352. FileUtils.mkdirs(d1);
  353. JGitTestUtil.write(f1, "f1");
  354. JGitTestUtil.write(f3, "f3");
  355. // test
  356. try {
  357. FileUtils.rename(f1, f2);
  358. fail("rename to non-empty directory should fail");
  359. } catch (IOException e) {
  360. assertEquals("f1", JGitTestUtil.read(f1)); // untouched source
  361. assertEquals("f3", JGitTestUtil.read(f3)); // untouched
  362. // empty directories within f2 may or may not have been deleted
  363. }
  364. }
  365. @Test
  366. public void testRenameOverExistingEmptyDirectory() throws IOException {
  367. File d = new File(trash, "d");
  368. FileUtils.mkdirs(d);
  369. File f1 = new File(trash, "d/f");
  370. File f2 = new File(trash, "d/g");
  371. File d1 = new File(trash, "d/g/h/i");
  372. FileUtils.mkdirs(d1);
  373. JGitTestUtil.write(f1, "f1");
  374. // test
  375. FileUtils.rename(f1, f2);
  376. assertFalse(f1.exists());
  377. assertTrue(f2.exists());
  378. assertEquals("f1", JGitTestUtil.read(f2));
  379. }
  380. @Test
  381. public void testCreateSymlink() throws IOException {
  382. FS fs = FS.DETECTED;
  383. // show test as ignored if the FS doesn't support symlinks
  384. Assume.assumeTrue(fs.supportsSymlinks());
  385. fs.createSymLink(new File(trash, "x"), "y");
  386. String target = fs.readSymLink(new File(trash, "x"));
  387. assertEquals("y", target);
  388. }
  389. @Test
  390. public void testCreateSymlinkOverrideExisting() throws IOException {
  391. FS fs = FS.DETECTED;
  392. // show test as ignored if the FS doesn't support symlinks
  393. Assume.assumeTrue(fs.supportsSymlinks());
  394. File file = new File(trash, "x");
  395. fs.createSymLink(file, "y");
  396. String target = fs.readSymLink(file);
  397. assertEquals("y", target);
  398. fs.createSymLink(file, "z");
  399. target = fs.readSymLink(file);
  400. assertEquals("z", target);
  401. }
  402. @Test
  403. public void testRelativize_doc() {
  404. // This is the javadoc example
  405. String base = toOSPathString("c:\\Users\\jdoe\\eclipse\\git\\project");
  406. String other = toOSPathString("c:\\Users\\jdoe\\eclipse\\git\\another_project\\pom.xml");
  407. String expected = toOSPathString("..\\another_project\\pom.xml");
  408. String actual = FileUtils.relativize(base, other);
  409. assertEquals(expected, actual);
  410. }
  411. @Test
  412. public void testRelativize_mixedCase() {
  413. SystemReader systemReader = SystemReader.getInstance();
  414. String base = toOSPathString("C:\\git\\jgit");
  415. String other = toOSPathString("C:\\Git\\test\\d\\f.txt");
  416. String expectedCaseInsensitive = toOSPathString("..\\test\\d\\f.txt");
  417. String expectedCaseSensitive = toOSPathString("..\\..\\Git\\test\\d\\f.txt");
  418. if (systemReader.isWindows()) {
  419. String actual = FileUtils.relativize(base, other);
  420. assertEquals(expectedCaseInsensitive, actual);
  421. } else if (systemReader.isMacOS()) {
  422. String actual = FileUtils.relativize(base, other);
  423. assertEquals(expectedCaseInsensitive, actual);
  424. } else {
  425. String actual = FileUtils.relativize(base, other);
  426. assertEquals(expectedCaseSensitive, actual);
  427. }
  428. }
  429. @Test
  430. public void testRelativize_scheme() {
  431. String base = toOSPathString("file:/home/eclipse/runtime-New_configuration/project_1/file.java");
  432. String other = toOSPathString("file:/home/eclipse/runtime-New_configuration/project");
  433. // 'file.java' is treated as a folder
  434. String expected = toOSPathString("../../project");
  435. String actual = FileUtils.relativize(base, other);
  436. assertEquals(expected, actual);
  437. }
  438. @Test
  439. public void testRelativize_equalPaths() {
  440. String base = toOSPathString("file:/home/eclipse/runtime-New_configuration/project_1");
  441. String other = toOSPathString("file:/home/eclipse/runtime-New_configuration/project_1");
  442. String expected = "";
  443. String actual = FileUtils.relativize(base, other);
  444. assertEquals(expected, actual);
  445. }
  446. @Test
  447. public void testRelativize_whitespaces() {
  448. String base = toOSPathString("/home/eclipse 3.4/runtime New_configuration/project_1");
  449. String other = toOSPathString("/home/eclipse 3.4/runtime New_configuration/project_1/file");
  450. String expected = "file";
  451. String actual = FileUtils.relativize(base, other);
  452. assertEquals(expected, actual);
  453. }
  454. private String toOSPathString(String path) {
  455. return path.replaceAll("/|\\\\",
  456. Matcher.quoteReplacement(File.separator));
  457. }
  458. }