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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 testAddA1() throws Exception {
  267. ApplyResult result = init("A1", false, true);
  268. assertEquals(1, result.getUpdatedFiles().size());
  269. assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
  270. .get(0));
  271. checkFile(new File(db.getWorkTree(), "A1"),
  272. b.getString(0, b.size(), false));
  273. }
  274. @Test
  275. public void testAddA2() throws Exception {
  276. ApplyResult result = init("A2", false, true);
  277. assertEquals(1, result.getUpdatedFiles().size());
  278. assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
  279. .get(0));
  280. checkFile(new File(db.getWorkTree(), "A2"),
  281. b.getString(0, b.size(), false));
  282. }
  283. @Test
  284. public void testAddA3() throws Exception {
  285. ApplyResult result = init("A3", false, true);
  286. assertEquals(1, result.getUpdatedFiles().size());
  287. assertEquals(new File(db.getWorkTree(), "A3"),
  288. result.getUpdatedFiles().get(0));
  289. checkFile(new File(db.getWorkTree(), "A3"),
  290. b.getString(0, b.size(), false));
  291. }
  292. @Test
  293. public void testAddA1Sub() throws Exception {
  294. ApplyResult result = init("A1_sub", false, false);
  295. assertEquals(1, result.getUpdatedFiles().size());
  296. assertEquals(new File(db.getWorkTree(), "sub/A1"), result
  297. .getUpdatedFiles().get(0));
  298. }
  299. @Test
  300. public void testDeleteD() throws Exception {
  301. ApplyResult result = init("D", true, false);
  302. assertEquals(1, result.getUpdatedFiles().size());
  303. assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
  304. .get(0));
  305. assertFalse(new File(db.getWorkTree(), "D").exists());
  306. }
  307. @Test(expected = PatchFormatException.class)
  308. public void testFailureF1() throws Exception {
  309. init("F1", true, false);
  310. }
  311. @Test(expected = PatchApplyException.class)
  312. public void testFailureF2() throws Exception {
  313. init("F2", true, false);
  314. }
  315. @Test
  316. public void testModifyE() throws Exception {
  317. ApplyResult result = init("E");
  318. assertEquals(1, result.getUpdatedFiles().size());
  319. assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
  320. .get(0));
  321. checkFile(new File(db.getWorkTree(), "E"),
  322. b.getString(0, b.size(), false));
  323. }
  324. @Test
  325. public void testModifyW() throws Exception {
  326. ApplyResult result = init("W");
  327. assertEquals(1, result.getUpdatedFiles().size());
  328. assertEquals(new File(db.getWorkTree(), "W"),
  329. result.getUpdatedFiles().get(0));
  330. checkFile(new File(db.getWorkTree(), "W"),
  331. b.getString(0, b.size(), false));
  332. }
  333. @Test
  334. public void testAddM1() throws Exception {
  335. ApplyResult result = init("M1", false, true);
  336. assertEquals(1, result.getUpdatedFiles().size());
  337. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  338. checkFile(new File(db.getWorkTree(), "M1"),
  339. b.getString(0, b.size(), false));
  340. }
  341. @Test
  342. public void testModifyM2() throws Exception {
  343. ApplyResult result = init("M2", true, true);
  344. assertEquals(1, result.getUpdatedFiles().size());
  345. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  346. checkFile(new File(db.getWorkTree(), "M2"),
  347. b.getString(0, b.size(), false));
  348. }
  349. @Test
  350. public void testModifyM3() throws Exception {
  351. ApplyResult result = init("M3", true, true);
  352. assertEquals(1, result.getUpdatedFiles().size());
  353. assertFalse(result.getUpdatedFiles().get(0).canExecute());
  354. checkFile(new File(db.getWorkTree(), "M3"),
  355. b.getString(0, b.size(), false));
  356. }
  357. @Test
  358. public void testModifyX() throws Exception {
  359. ApplyResult result = init("X");
  360. assertEquals(1, result.getUpdatedFiles().size());
  361. assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
  362. .get(0));
  363. checkFile(new File(db.getWorkTree(), "X"),
  364. b.getString(0, b.size(), false));
  365. }
  366. @Test
  367. public void testModifyY() throws Exception {
  368. ApplyResult result = init("Y");
  369. assertEquals(1, result.getUpdatedFiles().size());
  370. assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
  371. .get(0));
  372. checkFile(new File(db.getWorkTree(), "Y"),
  373. b.getString(0, b.size(), false));
  374. }
  375. @Test
  376. public void testModifyZ() throws Exception {
  377. ApplyResult result = init("Z");
  378. assertEquals(1, result.getUpdatedFiles().size());
  379. assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
  380. .get(0));
  381. checkFile(new File(db.getWorkTree(), "Z"),
  382. b.getString(0, b.size(), false));
  383. }
  384. @Test
  385. public void testModifyNL1() throws Exception {
  386. ApplyResult result = init("NL1");
  387. assertEquals(1, result.getUpdatedFiles().size());
  388. assertEquals(new File(db.getWorkTree(), "NL1"), result
  389. .getUpdatedFiles().get(0));
  390. checkFile(new File(db.getWorkTree(), "NL1"),
  391. b.getString(0, b.size(), false));
  392. }
  393. @Test
  394. public void testNonASCII() throws Exception {
  395. ApplyResult result = init("NonASCII");
  396. assertEquals(1, result.getUpdatedFiles().size());
  397. assertEquals(new File(db.getWorkTree(), "NonASCII"),
  398. result.getUpdatedFiles().get(0));
  399. checkFile(new File(db.getWorkTree(), "NonASCII"),
  400. b.getString(0, b.size(), false));
  401. }
  402. @Test
  403. public void testNonASCII2() throws Exception {
  404. ApplyResult result = init("NonASCII2");
  405. assertEquals(1, result.getUpdatedFiles().size());
  406. assertEquals(new File(db.getWorkTree(), "NonASCII2"),
  407. result.getUpdatedFiles().get(0));
  408. checkFile(new File(db.getWorkTree(), "NonASCII2"),
  409. b.getString(0, b.size(), false));
  410. }
  411. @Test
  412. public void testNonASCIIAdd() throws Exception {
  413. ApplyResult result = init("NonASCIIAdd");
  414. assertEquals(1, result.getUpdatedFiles().size());
  415. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
  416. result.getUpdatedFiles().get(0));
  417. checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
  418. b.getString(0, b.size(), false));
  419. }
  420. @Test
  421. public void testNonASCIIAdd2() throws Exception {
  422. ApplyResult result = init("NonASCIIAdd2", false, true);
  423. assertEquals(1, result.getUpdatedFiles().size());
  424. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
  425. result.getUpdatedFiles().get(0));
  426. checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
  427. b.getString(0, b.size(), false));
  428. }
  429. @Test
  430. public void testNonASCIIDel() throws Exception {
  431. ApplyResult result = init("NonASCIIDel", true, false);
  432. assertEquals(1, result.getUpdatedFiles().size());
  433. assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
  434. result.getUpdatedFiles().get(0));
  435. assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
  436. }
  437. @Test
  438. public void testRenameNoHunks() throws Exception {
  439. ApplyResult result = init("RenameNoHunks", true, true);
  440. assertEquals(1, result.getUpdatedFiles().size());
  441. assertEquals(new File(db.getWorkTree(), "RenameNoHunks"), result.getUpdatedFiles()
  442. .get(0));
  443. checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
  444. b.getString(0, b.size(), false));
  445. }
  446. @Test
  447. public void testRenameWithHunks() throws Exception {
  448. ApplyResult result = init("RenameWithHunks", true, true);
  449. assertEquals(1, result.getUpdatedFiles().size());
  450. assertEquals(new File(db.getWorkTree(), "RenameWithHunks"), result.getUpdatedFiles()
  451. .get(0));
  452. checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
  453. b.getString(0, b.size(), false));
  454. }
  455. @Test
  456. public void testCopyWithHunks() throws Exception {
  457. ApplyResult result = init("CopyWithHunks", true, true);
  458. assertEquals(1, result.getUpdatedFiles().size());
  459. assertEquals(new File(db.getWorkTree(), "CopyWithHunks"), result.getUpdatedFiles()
  460. .get(0));
  461. checkFile(new File(db.getWorkTree(), "CopyResult"),
  462. b.getString(0, b.size(), false));
  463. }
  464. @Test
  465. public void testShiftUp() throws Exception {
  466. ApplyResult result = init("ShiftUp");
  467. assertEquals(1, result.getUpdatedFiles().size());
  468. assertEquals(new File(db.getWorkTree(), "ShiftUp"),
  469. result.getUpdatedFiles().get(0));
  470. checkFile(new File(db.getWorkTree(), "ShiftUp"),
  471. b.getString(0, b.size(), false));
  472. }
  473. @Test
  474. public void testShiftUp2() throws Exception {
  475. ApplyResult result = init("ShiftUp2");
  476. assertEquals(1, result.getUpdatedFiles().size());
  477. assertEquals(new File(db.getWorkTree(), "ShiftUp2"),
  478. result.getUpdatedFiles().get(0));
  479. checkFile(new File(db.getWorkTree(), "ShiftUp2"),
  480. b.getString(0, b.size(), false));
  481. }
  482. @Test
  483. public void testShiftDown() throws Exception {
  484. ApplyResult result = init("ShiftDown");
  485. assertEquals(1, result.getUpdatedFiles().size());
  486. assertEquals(new File(db.getWorkTree(), "ShiftDown"),
  487. result.getUpdatedFiles().get(0));
  488. checkFile(new File(db.getWorkTree(), "ShiftDown"),
  489. b.getString(0, b.size(), false));
  490. }
  491. @Test
  492. public void testShiftDown2() throws Exception {
  493. ApplyResult result = init("ShiftDown2");
  494. assertEquals(1, result.getUpdatedFiles().size());
  495. assertEquals(new File(db.getWorkTree(), "ShiftDown2"),
  496. result.getUpdatedFiles().get(0));
  497. checkFile(new File(db.getWorkTree(), "ShiftDown2"),
  498. b.getString(0, b.size(), false));
  499. }
  500. private static byte[] readFile(String patchFile) throws IOException {
  501. final InputStream in = getTestResource(patchFile);
  502. if (in == null) {
  503. fail("No " + patchFile + " test vector");
  504. return null; // Never happens
  505. }
  506. try {
  507. final byte[] buf = new byte[1024];
  508. final ByteArrayOutputStream temp = new ByteArrayOutputStream();
  509. int n;
  510. while ((n = in.read(buf)) > 0)
  511. temp.write(buf, 0, n);
  512. return temp.toByteArray();
  513. } finally {
  514. in.close();
  515. }
  516. }
  517. private static InputStream getTestResource(String patchFile) {
  518. return ApplyCommandTest.class.getClassLoader()
  519. .getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
  520. }
  521. }