Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FileUtilTest.java 15KB

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