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

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