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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2011, 2012, IBM Corporation and others. 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.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.eclipse.jgit.api.errors.PatchApplyException;
  20. import org.eclipse.jgit.api.errors.PatchFormatException;
  21. import org.eclipse.jgit.diff.RawText;
  22. import org.eclipse.jgit.junit.RepositoryTestCase;
  23. import org.junit.Test;
  24. public class ApplyCommandTest extends RepositoryTestCase {
  25. private RawText a;
  26. private RawText b;
  27. private ApplyResult init(String name) throws Exception {
  28. return init(name, true, true);
  29. }
  30. private ApplyResult init(final String name, final boolean preExists,
  31. final boolean postExists) throws Exception {
  32. try (Git git = new Git(db)) {
  33. if (preExists) {
  34. a = new RawText(readFile(name + "_PreImage"));
  35. write(new File(db.getDirectory().getParent(), name),
  36. a.getString(0, a.size(), false));
  37. git.add().addFilepattern(name).call();
  38. git.commit().setMessage("PreImage").call();
  39. }
  40. if (postExists) {
  41. b = new RawText(readFile(name + "_PostImage"));
  42. }
  43. return git
  44. .apply()
  45. .setPatch(getTestResource(name + ".patch")).call();
  46. }
  47. }
  48. @Test
  49. public void testAddA1() throws Exception {
  50. ApplyResult result = init("A1", false, true);
  51. assertEquals(1, result.getUpdatedFiles().size());
  52. assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
  53. .get(0));
  54. checkFile(new File(db.getWorkTree(), "A1"),
  55. b.getString(0, b.size(), false));
  56. }
  57. @Test
  58. public void testAddA2() throws Exception {
  59. ApplyResult result = init("A2", false, true);
  60. assertEquals(1, result.getUpdatedFiles().size());
  61. assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
  62. .get(0));
  63. checkFile(new File(db.getWorkTree(), "A2"),
  64. b.getString(0, b.size(), false));
  65. }
  66. @Test
  67. public void testAddA3() throws Exception {
  68. ApplyResult result = init("A3", false, true);
  69. assertEquals(1, result.getUpdatedFiles().size());
  70. assertEquals(new File(db.getWorkTree(), "A3"),
  71. result.getUpdatedFiles().get(0));
  72. checkFile(new File(db.getWorkTree(), "A3"),
  73. b.getString(0, b.size(), false));
  74. }
  75. @Test
  76. public void testAddA1Sub() throws Exception {
  77. ApplyResult result = init("A1_sub", false, false);
  78. assertEquals(1, result.getUpdatedFiles().size());
  79. assertEquals(new File(db.getWorkTree(), "sub/A1"), result
  80. .getUpdatedFiles().get(0));
  81. }
  82. @Test
  83. public void testDeleteD() throws Exception {
  84. ApplyResult result = init("D", true, false);
  85. assertEquals(1, result.getUpdatedFiles().size());
  86. assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
  87. .get(0));
  88. assertFalse(new File(db.getWorkTree(), "D").exists());
  89. }
  90. @Test(expected = PatchFormatException.class)
  91. public void testFailureF1() throws Exception {
  92. init("F1", true, false);
  93. }
  94. @Test(expected = PatchApplyException.class)
  95. public void testFailureF2() throws Exception {
  96. init("F2", true, false);
  97. }
  98. @Test
  99. public void testModifyE() throws Exception {
  100. ApplyResult result = init("E");
  101. assertEquals(1, result.getUpdatedFiles().size());
  102. assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
  103. .get(0));
  104. checkFile(new File(db.getWorkTree(), "E"),
  105. b.getString(0, b.size(), false));
  106. }
  107. @Test
  108. public void testModifyW() throws Exception {
  109. ApplyResult result = init("W");
  110. assertEquals(1, result.getUpdatedFiles().size());
  111. assertEquals(new File(db.getWorkTree(), "W"),
  112. result.getUpdatedFiles().get(0));
  113. checkFile(new File(db.getWorkTree(), "W"),
  114. b.getString(0, b.size(), false));
  115. }
  116. @Test
  117. public void testAddM1() throws Exception {
  118. ApplyResult result = init("M1", false, true);
  119. assertEquals(1, result.getUpdatedFiles().size());
  120. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  121. checkFile(new File(db.getWorkTree(), "M1"),
  122. b.getString(0, b.size(), false));
  123. }
  124. @Test
  125. public void testModifyM2() throws Exception {
  126. ApplyResult result = init("M2", true, true);
  127. assertEquals(1, result.getUpdatedFiles().size());
  128. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  129. checkFile(new File(db.getWorkTree(), "M2"),
  130. b.getString(0, b.size(), false));
  131. }
  132. @Test
  133. public void testModifyM3() throws Exception {
  134. ApplyResult result = init("M3", true, true);
  135. assertEquals(1, result.getUpdatedFiles().size());
  136. assertFalse(result.getUpdatedFiles().get(0).canExecute());
  137. checkFile(new File(db.getWorkTree(), "M3"),
  138. b.getString(0, b.size(), false));
  139. }
  140. @Test
  141. public void testModifyX() throws Exception {
  142. ApplyResult result = init("X");
  143. assertEquals(1, result.getUpdatedFiles().size());
  144. assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
  145. .get(0));
  146. checkFile(new File(db.getWorkTree(), "X"),
  147. b.getString(0, b.size(), false));
  148. }
  149. @Test
  150. public void testModifyY() throws Exception {
  151. ApplyResult result = init("Y");
  152. assertEquals(1, result.getUpdatedFiles().size());
  153. assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
  154. .get(0));
  155. checkFile(new File(db.getWorkTree(), "Y"),
  156. b.getString(0, b.size(), false));
  157. }
  158. @Test
  159. public void testModifyZ() throws Exception {
  160. ApplyResult result = init("Z");
  161. assertEquals(1, result.getUpdatedFiles().size());
  162. assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
  163. .get(0));
  164. checkFile(new File(db.getWorkTree(), "Z"),
  165. b.getString(0, b.size(), false));
  166. }
  167. @Test
  168. public void testModifyNL1() throws Exception {
  169. ApplyResult result = init("NL1");
  170. assertEquals(1, result.getUpdatedFiles().size());
  171. assertEquals(new File(db.getWorkTree(), "NL1"), result
  172. .getUpdatedFiles().get(0));
  173. checkFile(new File(db.getWorkTree(), "NL1"),
  174. b.getString(0, b.size(), false));
  175. }
  176. @Test
  177. public void testNonASCII() throws Exception {
  178. ApplyResult result = init("NonASCII");
  179. assertEquals(1, result.getUpdatedFiles().size());
  180. assertEquals(new File(db.getWorkTree(), "NonASCII"),
  181. result.getUpdatedFiles().get(0));
  182. checkFile(new File(db.getWorkTree(), "NonASCII"),
  183. b.getString(0, b.size(), false));
  184. }
  185. @Test
  186. public void testNonASCII2() throws Exception {
  187. ApplyResult result = init("NonASCII2");
  188. assertEquals(1, result.getUpdatedFiles().size());
  189. assertEquals(new File(db.getWorkTree(), "NonASCII2"),
  190. result.getUpdatedFiles().get(0));
  191. checkFile(new File(db.getWorkTree(), "NonASCII2"),
  192. b.getString(0, b.size(), false));
  193. }
  194. @Test
  195. public void testNonASCIIAdd() throws Exception {
  196. ApplyResult result = init("NonASCIIAdd");
  197. assertEquals(1, result.getUpdatedFiles().size());
  198. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
  199. result.getUpdatedFiles().get(0));
  200. checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
  201. b.getString(0, b.size(), false));
  202. }
  203. @Test
  204. public void testNonASCIIAdd2() throws Exception {
  205. ApplyResult result = init("NonASCIIAdd2", false, true);
  206. assertEquals(1, result.getUpdatedFiles().size());
  207. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
  208. result.getUpdatedFiles().get(0));
  209. checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
  210. b.getString(0, b.size(), false));
  211. }
  212. @Test
  213. public void testNonASCIIDel() throws Exception {
  214. ApplyResult result = init("NonASCIIDel", true, false);
  215. assertEquals(1, result.getUpdatedFiles().size());
  216. assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
  217. result.getUpdatedFiles().get(0));
  218. assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
  219. }
  220. private static byte[] readFile(String patchFile) throws IOException {
  221. final InputStream in = getTestResource(patchFile);
  222. if (in == null) {
  223. fail("No " + patchFile + " test vector");
  224. return null; // Never happens
  225. }
  226. try {
  227. final byte[] buf = new byte[1024];
  228. final ByteArrayOutputStream temp = new ByteArrayOutputStream();
  229. int n;
  230. while ((n = in.read(buf)) > 0)
  231. temp.write(buf, 0, n);
  232. return temp.toByteArray();
  233. } finally {
  234. in.close();
  235. }
  236. }
  237. private static InputStream getTestResource(String patchFile) {
  238. return ApplyCommandTest.class.getClassLoader()
  239. .getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
  240. }
  241. }