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.

ApplyCommandTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright (C) 2011, 2021 IBM Corporation and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.nio.charset.StandardCharsets;
  22. import java.nio.file.Files;
  23. import org.eclipse.jgit.api.errors.PatchApplyException;
  24. import org.eclipse.jgit.api.errors.PatchFormatException;
  25. import org.eclipse.jgit.attributes.FilterCommand;
  26. import org.eclipse.jgit.attributes.FilterCommandFactory;
  27. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  28. import org.eclipse.jgit.diff.RawText;
  29. import org.eclipse.jgit.junit.RepositoryTestCase;
  30. import org.eclipse.jgit.lib.Config;
  31. import org.eclipse.jgit.lib.ConfigConstants;
  32. import org.eclipse.jgit.util.IO;
  33. import org.junit.Test;
  34. public class ApplyCommandTest extends RepositoryTestCase {
  35. private RawText a;
  36. private RawText b;
  37. private ApplyResult init(String name) throws Exception {
  38. return init(name, true, true);
  39. }
  40. private ApplyResult init(final String name, final boolean preExists,
  41. final boolean postExists) throws Exception {
  42. try (Git git = new Git(db)) {
  43. if (preExists) {
  44. a = new RawText(readFile(name + "_PreImage"));
  45. write(new File(db.getDirectory().getParent(), name),
  46. a.getString(0, a.size(), false));
  47. git.add().addFilepattern(name).call();
  48. git.commit().setMessage("PreImage").call();
  49. }
  50. if (postExists) {
  51. b = new RawText(readFile(name + "_PostImage"));
  52. }
  53. return git
  54. .apply()
  55. .setPatch(getTestResource(name + ".patch")).call();
  56. }
  57. }
  58. @Test
  59. public void testCrLf() throws Exception {
  60. try {
  61. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  62. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  63. ApplyResult result = init("crlf", true, true);
  64. assertEquals(1, result.getUpdatedFiles().size());
  65. assertEquals(new File(db.getWorkTree(), "crlf"),
  66. result.getUpdatedFiles().get(0));
  67. checkFile(new File(db.getWorkTree(), "crlf"),
  68. b.getString(0, b.size(), false));
  69. } finally {
  70. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  71. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  72. }
  73. }
  74. @Test
  75. public void testCrLfOff() throws Exception {
  76. try {
  77. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  78. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  79. ApplyResult result = init("crlf", true, true);
  80. assertEquals(1, result.getUpdatedFiles().size());
  81. assertEquals(new File(db.getWorkTree(), "crlf"),
  82. result.getUpdatedFiles().get(0));
  83. checkFile(new File(db.getWorkTree(), "crlf"),
  84. b.getString(0, b.size(), false));
  85. } finally {
  86. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  87. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  88. }
  89. }
  90. @Test
  91. public void testCrLfEmptyCommitted() throws Exception {
  92. try {
  93. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  94. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  95. ApplyResult result = init("crlf3", true, true);
  96. assertEquals(1, result.getUpdatedFiles().size());
  97. assertEquals(new File(db.getWorkTree(), "crlf3"),
  98. result.getUpdatedFiles().get(0));
  99. checkFile(new File(db.getWorkTree(), "crlf3"),
  100. b.getString(0, b.size(), false));
  101. } finally {
  102. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  103. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  104. }
  105. }
  106. @Test
  107. public void testCrLfNewFile() throws Exception {
  108. try {
  109. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  110. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  111. ApplyResult result = init("crlf4", false, true);
  112. assertEquals(1, result.getUpdatedFiles().size());
  113. assertEquals(new File(db.getWorkTree(), "crlf4"),
  114. result.getUpdatedFiles().get(0));
  115. checkFile(new File(db.getWorkTree(), "crlf4"),
  116. b.getString(0, b.size(), false));
  117. } finally {
  118. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  119. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  120. }
  121. }
  122. @Test
  123. public void testPatchWithCrLf() throws Exception {
  124. try {
  125. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  126. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  127. ApplyResult result = init("crlf2", true, true);
  128. assertEquals(1, result.getUpdatedFiles().size());
  129. assertEquals(new File(db.getWorkTree(), "crlf2"),
  130. result.getUpdatedFiles().get(0));
  131. checkFile(new File(db.getWorkTree(), "crlf2"),
  132. b.getString(0, b.size(), false));
  133. } finally {
  134. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  135. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  136. }
  137. }
  138. @Test
  139. public void testPatchWithCrLf2() throws Exception {
  140. String name = "crlf2";
  141. try (Git git = new Git(db)) {
  142. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  143. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  144. a = new RawText(readFile(name + "_PreImage"));
  145. write(new File(db.getWorkTree(), name),
  146. a.getString(0, a.size(), false));
  147. git.add().addFilepattern(name).call();
  148. git.commit().setMessage("PreImage").call();
  149. b = new RawText(readFile(name + "_PostImage"));
  150. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  151. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  152. ApplyResult result = git.apply()
  153. .setPatch(getTestResource(name + ".patch")).call();
  154. assertEquals(1, result.getUpdatedFiles().size());
  155. assertEquals(new File(db.getWorkTree(), name),
  156. result.getUpdatedFiles().get(0));
  157. checkFile(new File(db.getWorkTree(), name),
  158. b.getString(0, b.size(), false));
  159. } finally {
  160. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  161. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  162. }
  163. }
  164. // Clean/smudge filter for testFiltering. The smudgetest test resources were
  165. // created with C git using a clean filter sed -e "s/A/E/g" and the smudge
  166. // filter sed -e "s/E/A/g". To keep the test independent of the presence of
  167. // sed, implement this with a built-in filter.
  168. private static class ReplaceFilter extends FilterCommand {
  169. private final char toReplace;
  170. private final char replacement;
  171. ReplaceFilter(InputStream in, OutputStream out, char toReplace,
  172. char replacement) {
  173. super(in, out);
  174. this.toReplace = toReplace;
  175. this.replacement = replacement;
  176. }
  177. @Override
  178. public int run() throws IOException {
  179. int b = in.read();
  180. if (b < 0) {
  181. in.close();
  182. out.close();
  183. return -1;
  184. }
  185. if ((b & 0xFF) == toReplace) {
  186. b = replacement;
  187. }
  188. out.write(b);
  189. return 1;
  190. }
  191. }
  192. @Test
  193. public void testFiltering() throws Exception {
  194. // Set up filter
  195. FilterCommandFactory clean = (repo, in, out) -> {
  196. return new ReplaceFilter(in, out, 'A', 'E');
  197. };
  198. FilterCommandFactory smudge = (repo, in, out) -> {
  199. return new ReplaceFilter(in, out, 'E', 'A');
  200. };
  201. FilterCommandRegistry.register("jgit://builtin/a2e/clean", clean);
  202. FilterCommandRegistry.register("jgit://builtin/a2e/smudge", smudge);
  203. try (Git git = new Git(db)) {
  204. Config config = db.getConfig();
  205. config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e",
  206. "clean", "jgit://builtin/a2e/clean");
  207. config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e",
  208. "smudge", "jgit://builtin/a2e/smudge");
  209. write(new File(db.getWorkTree(), ".gitattributes"),
  210. "smudgetest filter=a2e");
  211. git.add().addFilepattern(".gitattributes").call();
  212. git.commit().setMessage("Attributes").call();
  213. ApplyResult result = init("smudgetest", true, true);
  214. assertEquals(1, result.getUpdatedFiles().size());
  215. assertEquals(new File(db.getWorkTree(), "smudgetest"),
  216. result.getUpdatedFiles().get(0));
  217. checkFile(new File(db.getWorkTree(), "smudgetest"),
  218. b.getString(0, b.size(), false));
  219. } finally {
  220. // Tear down filter
  221. FilterCommandRegistry.unregister("jgit://builtin/a2e/clean");
  222. FilterCommandRegistry.unregister("jgit://builtin/a2e/smudge");
  223. }
  224. }
  225. private void checkBinary(String name, boolean hasPreImage)
  226. throws Exception {
  227. checkBinary(name, hasPreImage, 1);
  228. }
  229. private void checkBinary(String name, boolean hasPreImage,
  230. int numberOfFiles) throws Exception {
  231. try (Git git = new Git(db)) {
  232. byte[] post = IO
  233. .readWholeStream(getTestResource(name + "_PostImage"), 0)
  234. .array();
  235. File f = new File(db.getWorkTree(), name);
  236. if (hasPreImage) {
  237. byte[] pre = IO
  238. .readWholeStream(getTestResource(name + "_PreImage"), 0)
  239. .array();
  240. Files.write(f.toPath(), pre);
  241. git.add().addFilepattern(name).call();
  242. git.commit().setMessage("PreImage").call();
  243. }
  244. ApplyResult result = git.apply()
  245. .setPatch(getTestResource(name + ".patch")).call();
  246. assertEquals(numberOfFiles, result.getUpdatedFiles().size());
  247. assertEquals(f, result.getUpdatedFiles().get(0));
  248. assertArrayEquals(post, Files.readAllBytes(f.toPath()));
  249. }
  250. }
  251. @Test
  252. public void testBinaryDelta() throws Exception {
  253. checkBinary("delta", true);
  254. }
  255. @Test
  256. public void testBinaryLiteral() throws Exception {
  257. checkBinary("literal", true);
  258. }
  259. @Test
  260. public void testBinaryLiteralAdd() throws Exception {
  261. checkBinary("literal_add", false);
  262. }
  263. @Test
  264. public void testEncodingChange() throws Exception {
  265. // This is a text patch that changes a file containing ÄÖÜ in UTF-8 to
  266. // the same characters in ISO-8859-1. The patch file itself uses mixed
  267. // encoding. Since checkFile() works with strings use the binary check.
  268. checkBinary("umlaut", true);
  269. }
  270. @Test
  271. public void testEmptyLine() throws Exception {
  272. // C git accepts completely empty lines as empty context lines.
  273. // According to comments in the C git sources (apply.c), newer GNU diff
  274. // may produce such diffs.
  275. checkBinary("emptyLine", true);
  276. }
  277. @Test
  278. public void testMultiFileNoNewline() throws Exception {
  279. // This test needs two files. One is in the test resources.
  280. try (Git git = new Git(db)) {
  281. Files.write(db.getWorkTree().toPath().resolve("yello"),
  282. "yello".getBytes(StandardCharsets.US_ASCII));
  283. git.add().addFilepattern("yello").call();
  284. git.commit().setMessage("yello").call();
  285. }
  286. checkBinary("hello", true, 2);
  287. }
  288. @Test
  289. public void testAddA1() throws Exception {
  290. ApplyResult result = init("A1", false, true);
  291. assertEquals(1, result.getUpdatedFiles().size());
  292. assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
  293. .get(0));
  294. checkFile(new File(db.getWorkTree(), "A1"),
  295. b.getString(0, b.size(), false));
  296. }
  297. @Test
  298. public void testAddA2() throws Exception {
  299. ApplyResult result = init("A2", false, true);
  300. assertEquals(1, result.getUpdatedFiles().size());
  301. assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
  302. .get(0));
  303. checkFile(new File(db.getWorkTree(), "A2"),
  304. b.getString(0, b.size(), false));
  305. }
  306. @Test
  307. public void testAddA3() throws Exception {
  308. ApplyResult result = init("A3", false, true);
  309. assertEquals(1, result.getUpdatedFiles().size());
  310. assertEquals(new File(db.getWorkTree(), "A3"),
  311. result.getUpdatedFiles().get(0));
  312. checkFile(new File(db.getWorkTree(), "A3"),
  313. b.getString(0, b.size(), false));
  314. }
  315. @Test
  316. public void testAddA1Sub() throws Exception {
  317. ApplyResult result = init("A1_sub", false, false);
  318. assertEquals(1, result.getUpdatedFiles().size());
  319. assertEquals(new File(db.getWorkTree(), "sub/A1"), result
  320. .getUpdatedFiles().get(0));
  321. }
  322. @Test
  323. public void testDeleteD() throws Exception {
  324. ApplyResult result = init("D", true, false);
  325. assertEquals(1, result.getUpdatedFiles().size());
  326. assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
  327. .get(0));
  328. assertFalse(new File(db.getWorkTree(), "D").exists());
  329. }
  330. @Test(expected = PatchFormatException.class)
  331. public void testFailureF1() throws Exception {
  332. init("F1", true, false);
  333. }
  334. @Test(expected = PatchApplyException.class)
  335. public void testFailureF2() throws Exception {
  336. init("F2", true, false);
  337. }
  338. @Test
  339. public void testModifyE() throws Exception {
  340. ApplyResult result = init("E");
  341. assertEquals(1, result.getUpdatedFiles().size());
  342. assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
  343. .get(0));
  344. checkFile(new File(db.getWorkTree(), "E"),
  345. b.getString(0, b.size(), false));
  346. }
  347. @Test
  348. public void testModifyW() throws Exception {
  349. ApplyResult result = init("W");
  350. assertEquals(1, result.getUpdatedFiles().size());
  351. assertEquals(new File(db.getWorkTree(), "W"),
  352. result.getUpdatedFiles().get(0));
  353. checkFile(new File(db.getWorkTree(), "W"),
  354. b.getString(0, b.size(), false));
  355. }
  356. @Test
  357. public void testAddM1() throws Exception {
  358. ApplyResult result = init("M1", false, true);
  359. assertEquals(1, result.getUpdatedFiles().size());
  360. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  361. checkFile(new File(db.getWorkTree(), "M1"),
  362. b.getString(0, b.size(), false));
  363. }
  364. @Test
  365. public void testModifyM2() throws Exception {
  366. ApplyResult result = init("M2", true, true);
  367. assertEquals(1, result.getUpdatedFiles().size());
  368. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  369. checkFile(new File(db.getWorkTree(), "M2"),
  370. b.getString(0, b.size(), false));
  371. }
  372. @Test
  373. public void testModifyM3() throws Exception {
  374. ApplyResult result = init("M3", true, true);
  375. assertEquals(1, result.getUpdatedFiles().size());
  376. assertFalse(result.getUpdatedFiles().get(0).canExecute());
  377. checkFile(new File(db.getWorkTree(), "M3"),
  378. b.getString(0, b.size(), false));
  379. }
  380. @Test
  381. public void testModifyX() throws Exception {
  382. ApplyResult result = init("X");
  383. assertEquals(1, result.getUpdatedFiles().size());
  384. assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
  385. .get(0));
  386. checkFile(new File(db.getWorkTree(), "X"),
  387. b.getString(0, b.size(), false));
  388. }
  389. @Test
  390. public void testModifyY() throws Exception {
  391. ApplyResult result = init("Y");
  392. assertEquals(1, result.getUpdatedFiles().size());
  393. assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
  394. .get(0));
  395. checkFile(new File(db.getWorkTree(), "Y"),
  396. b.getString(0, b.size(), false));
  397. }
  398. @Test
  399. public void testModifyZ() throws Exception {
  400. ApplyResult result = init("Z");
  401. assertEquals(1, result.getUpdatedFiles().size());
  402. assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
  403. .get(0));
  404. checkFile(new File(db.getWorkTree(), "Z"),
  405. b.getString(0, b.size(), false));
  406. }
  407. @Test
  408. public void testModifyNL1() throws Exception {
  409. ApplyResult result = init("NL1");
  410. assertEquals(1, result.getUpdatedFiles().size());
  411. assertEquals(new File(db.getWorkTree(), "NL1"), result
  412. .getUpdatedFiles().get(0));
  413. checkFile(new File(db.getWorkTree(), "NL1"),
  414. b.getString(0, b.size(), false));
  415. }
  416. @Test
  417. public void testNonASCII() throws Exception {
  418. ApplyResult result = init("NonASCII");
  419. assertEquals(1, result.getUpdatedFiles().size());
  420. assertEquals(new File(db.getWorkTree(), "NonASCII"),
  421. result.getUpdatedFiles().get(0));
  422. checkFile(new File(db.getWorkTree(), "NonASCII"),
  423. b.getString(0, b.size(), false));
  424. }
  425. @Test
  426. public void testNonASCII2() throws Exception {
  427. ApplyResult result = init("NonASCII2");
  428. assertEquals(1, result.getUpdatedFiles().size());
  429. assertEquals(new File(db.getWorkTree(), "NonASCII2"),
  430. result.getUpdatedFiles().get(0));
  431. checkFile(new File(db.getWorkTree(), "NonASCII2"),
  432. b.getString(0, b.size(), false));
  433. }
  434. @Test
  435. public void testNonASCIIAdd() throws Exception {
  436. ApplyResult result = init("NonASCIIAdd");
  437. assertEquals(1, result.getUpdatedFiles().size());
  438. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
  439. result.getUpdatedFiles().get(0));
  440. checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
  441. b.getString(0, b.size(), false));
  442. }
  443. @Test
  444. public void testNonASCIIAdd2() throws Exception {
  445. ApplyResult result = init("NonASCIIAdd2", false, true);
  446. assertEquals(1, result.getUpdatedFiles().size());
  447. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
  448. result.getUpdatedFiles().get(0));
  449. checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
  450. b.getString(0, b.size(), false));
  451. }
  452. @Test
  453. public void testNonASCIIDel() throws Exception {
  454. ApplyResult result = init("NonASCIIDel", true, false);
  455. assertEquals(1, result.getUpdatedFiles().size());
  456. assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
  457. result.getUpdatedFiles().get(0));
  458. assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
  459. }
  460. @Test
  461. public void testRenameNoHunks() throws Exception {
  462. ApplyResult result = init("RenameNoHunks", true, true);
  463. assertEquals(1, result.getUpdatedFiles().size());
  464. assertEquals(new File(db.getWorkTree(), "RenameNoHunks"), result.getUpdatedFiles()
  465. .get(0));
  466. checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
  467. b.getString(0, b.size(), false));
  468. }
  469. @Test
  470. public void testRenameWithHunks() throws Exception {
  471. ApplyResult result = init("RenameWithHunks", true, true);
  472. assertEquals(1, result.getUpdatedFiles().size());
  473. assertEquals(new File(db.getWorkTree(), "RenameWithHunks"), result.getUpdatedFiles()
  474. .get(0));
  475. checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
  476. b.getString(0, b.size(), false));
  477. }
  478. @Test
  479. public void testCopyWithHunks() throws Exception {
  480. ApplyResult result = init("CopyWithHunks", true, true);
  481. assertEquals(1, result.getUpdatedFiles().size());
  482. assertEquals(new File(db.getWorkTree(), "CopyWithHunks"), result.getUpdatedFiles()
  483. .get(0));
  484. checkFile(new File(db.getWorkTree(), "CopyResult"),
  485. b.getString(0, b.size(), false));
  486. }
  487. @Test
  488. public void testShiftUp() throws Exception {
  489. ApplyResult result = init("ShiftUp");
  490. assertEquals(1, result.getUpdatedFiles().size());
  491. assertEquals(new File(db.getWorkTree(), "ShiftUp"),
  492. result.getUpdatedFiles().get(0));
  493. checkFile(new File(db.getWorkTree(), "ShiftUp"),
  494. b.getString(0, b.size(), false));
  495. }
  496. @Test
  497. public void testShiftUp2() throws Exception {
  498. ApplyResult result = init("ShiftUp2");
  499. assertEquals(1, result.getUpdatedFiles().size());
  500. assertEquals(new File(db.getWorkTree(), "ShiftUp2"),
  501. result.getUpdatedFiles().get(0));
  502. checkFile(new File(db.getWorkTree(), "ShiftUp2"),
  503. b.getString(0, b.size(), false));
  504. }
  505. @Test
  506. public void testShiftDown() throws Exception {
  507. ApplyResult result = init("ShiftDown");
  508. assertEquals(1, result.getUpdatedFiles().size());
  509. assertEquals(new File(db.getWorkTree(), "ShiftDown"),
  510. result.getUpdatedFiles().get(0));
  511. checkFile(new File(db.getWorkTree(), "ShiftDown"),
  512. b.getString(0, b.size(), false));
  513. }
  514. @Test
  515. public void testShiftDown2() throws Exception {
  516. ApplyResult result = init("ShiftDown2");
  517. assertEquals(1, result.getUpdatedFiles().size());
  518. assertEquals(new File(db.getWorkTree(), "ShiftDown2"),
  519. result.getUpdatedFiles().get(0));
  520. checkFile(new File(db.getWorkTree(), "ShiftDown2"),
  521. b.getString(0, b.size(), false));
  522. }
  523. private static byte[] readFile(String patchFile) throws IOException {
  524. final InputStream in = getTestResource(patchFile);
  525. if (in == null) {
  526. fail("No " + patchFile + " test vector");
  527. return null; // Never happens
  528. }
  529. try {
  530. final byte[] buf = new byte[1024];
  531. final ByteArrayOutputStream temp = new ByteArrayOutputStream();
  532. int n;
  533. while ((n = in.read(buf)) > 0)
  534. temp.write(buf, 0, n);
  535. return temp.toByteArray();
  536. } finally {
  537. in.close();
  538. }
  539. }
  540. private static InputStream getTestResource(String patchFile) {
  541. return ApplyCommandTest.class.getClassLoader()
  542. .getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
  543. }
  544. }