View Javadoc
1   /*
2    * Copyright (C) 2011, 2020 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  
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  
17  import java.io.ByteArrayOutputStream;
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.eclipse.jgit.api.errors.PatchApplyException;
23  import org.eclipse.jgit.api.errors.PatchFormatException;
24  import org.eclipse.jgit.diff.RawText;
25  import org.eclipse.jgit.junit.RepositoryTestCase;
26  import org.junit.Test;
27  
28  public class ApplyCommandTest extends RepositoryTestCase {
29  
30  	private RawText a;
31  
32  	private RawText b;
33  
34  	private ApplyResult init(String name) throws Exception {
35  		return init(name, true, true);
36  	}
37  
38  	private ApplyResult init(final String name, final boolean preExists,
39  			final boolean postExists) throws Exception {
40  		try (Git git = new Git(db)) {
41  			if (preExists) {
42  				a = new RawText(readFile(name + "_PreImage"));
43  				write(new File(db.getDirectory().getParent(), name),
44  						a.getString(0, a.size(), false));
45  
46  				git.add().addFilepattern(name).call();
47  				git.commit().setMessage("PreImage").call();
48  			}
49  
50  			if (postExists) {
51  				b = new RawText(readFile(name + "_PostImage"));
52  			}
53  
54  			return git
55  					.apply()
56  					.setPatch(getTestResource(name + ".patch")).call();
57  		}
58  	}
59  
60  	@Test
61  	public void testAddA1() throws Exception {
62  		ApplyResult result = init("A1", false, true);
63  		assertEquals(1, result.getUpdatedFiles().size());
64  		assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
65  				.get(0));
66  		checkFile(new File(db.getWorkTree(), "A1"),
67  				b.getString(0, b.size(), false));
68  	}
69  
70  	@Test
71  	public void testAddA2() throws Exception {
72  		ApplyResult result = init("A2", false, true);
73  		assertEquals(1, result.getUpdatedFiles().size());
74  		assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
75  				.get(0));
76  		checkFile(new File(db.getWorkTree(), "A2"),
77  				b.getString(0, b.size(), false));
78  	}
79  
80  	@Test
81  	public void testAddA3() throws Exception {
82  		ApplyResult result = init("A3", false, true);
83  		assertEquals(1, result.getUpdatedFiles().size());
84  		assertEquals(new File(db.getWorkTree(), "A3"),
85  				result.getUpdatedFiles().get(0));
86  		checkFile(new File(db.getWorkTree(), "A3"),
87  				b.getString(0, b.size(), false));
88  	}
89  
90  	@Test
91  	public void testAddA1Sub() throws Exception {
92  		ApplyResult result = init("A1_sub", false, false);
93  		assertEquals(1, result.getUpdatedFiles().size());
94  		assertEquals(new File(db.getWorkTree(), "sub/A1"), result
95  				.getUpdatedFiles().get(0));
96  	}
97  
98  	@Test
99  	public void testDeleteD() throws Exception {
100 		ApplyResult result = init("D", true, false);
101 		assertEquals(1, result.getUpdatedFiles().size());
102 		assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
103 				.get(0));
104 		assertFalse(new File(db.getWorkTree(), "D").exists());
105 	}
106 
107 	@Test(expected = PatchFormatException.class)
108 	public void testFailureF1() throws Exception {
109 		init("F1", true, false);
110 	}
111 
112 	@Test(expected = PatchApplyException.class)
113 	public void testFailureF2() throws Exception {
114 		init("F2", true, false);
115 	}
116 
117 	@Test
118 	public void testModifyE() throws Exception {
119 		ApplyResult result = init("E");
120 		assertEquals(1, result.getUpdatedFiles().size());
121 		assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
122 				.get(0));
123 		checkFile(new File(db.getWorkTree(), "E"),
124 				b.getString(0, b.size(), false));
125 	}
126 
127 	@Test
128 	public void testModifyW() throws Exception {
129 		ApplyResult result = init("W");
130 		assertEquals(1, result.getUpdatedFiles().size());
131 		assertEquals(new File(db.getWorkTree(), "W"),
132 				result.getUpdatedFiles().get(0));
133 		checkFile(new File(db.getWorkTree(), "W"),
134 				b.getString(0, b.size(), false));
135 	}
136 
137 	@Test
138 	public void testAddM1() throws Exception {
139 		ApplyResult result = init("M1", false, true);
140 		assertEquals(1, result.getUpdatedFiles().size());
141 		assertTrue(result.getUpdatedFiles().get(0).canExecute());
142 		checkFile(new File(db.getWorkTree(), "M1"),
143 				b.getString(0, b.size(), false));
144 	}
145 
146 	@Test
147 	public void testModifyM2() throws Exception {
148 		ApplyResult result = init("M2", true, true);
149 		assertEquals(1, result.getUpdatedFiles().size());
150 		assertTrue(result.getUpdatedFiles().get(0).canExecute());
151 		checkFile(new File(db.getWorkTree(), "M2"),
152 				b.getString(0, b.size(), false));
153 	}
154 
155 	@Test
156 	public void testModifyM3() throws Exception {
157 		ApplyResult result = init("M3", true, true);
158 		assertEquals(1, result.getUpdatedFiles().size());
159 		assertFalse(result.getUpdatedFiles().get(0).canExecute());
160 		checkFile(new File(db.getWorkTree(), "M3"),
161 				b.getString(0, b.size(), false));
162 	}
163 
164 	@Test
165 	public void testModifyX() throws Exception {
166 		ApplyResult result = init("X");
167 		assertEquals(1, result.getUpdatedFiles().size());
168 		assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
169 				.get(0));
170 		checkFile(new File(db.getWorkTree(), "X"),
171 				b.getString(0, b.size(), false));
172 	}
173 
174 	@Test
175 	public void testModifyY() throws Exception {
176 		ApplyResult result = init("Y");
177 		assertEquals(1, result.getUpdatedFiles().size());
178 		assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
179 				.get(0));
180 		checkFile(new File(db.getWorkTree(), "Y"),
181 				b.getString(0, b.size(), false));
182 	}
183 
184 	@Test
185 	public void testModifyZ() throws Exception {
186 		ApplyResult result = init("Z");
187 		assertEquals(1, result.getUpdatedFiles().size());
188 		assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
189 				.get(0));
190 		checkFile(new File(db.getWorkTree(), "Z"),
191 				b.getString(0, b.size(), false));
192 	}
193 
194 	@Test
195 	public void testModifyNL1() throws Exception {
196 		ApplyResult result = init("NL1");
197 		assertEquals(1, result.getUpdatedFiles().size());
198 		assertEquals(new File(db.getWorkTree(), "NL1"), result
199 				.getUpdatedFiles().get(0));
200 		checkFile(new File(db.getWorkTree(), "NL1"),
201 				b.getString(0, b.size(), false));
202 	}
203 
204 	@Test
205 	public void testNonASCII() throws Exception {
206 		ApplyResult result = init("NonASCII");
207 		assertEquals(1, result.getUpdatedFiles().size());
208 		assertEquals(new File(db.getWorkTree(), "NonASCII"),
209 				result.getUpdatedFiles().get(0));
210 		checkFile(new File(db.getWorkTree(), "NonASCII"),
211 				b.getString(0, b.size(), false));
212 	}
213 
214 	@Test
215 	public void testNonASCII2() throws Exception {
216 		ApplyResult result = init("NonASCII2");
217 		assertEquals(1, result.getUpdatedFiles().size());
218 		assertEquals(new File(db.getWorkTree(), "NonASCII2"),
219 				result.getUpdatedFiles().get(0));
220 		checkFile(new File(db.getWorkTree(), "NonASCII2"),
221 				b.getString(0, b.size(), false));
222 	}
223 
224 	@Test
225 	public void testNonASCIIAdd() throws Exception {
226 		ApplyResult result = init("NonASCIIAdd");
227 		assertEquals(1, result.getUpdatedFiles().size());
228 		assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
229 				result.getUpdatedFiles().get(0));
230 		checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
231 				b.getString(0, b.size(), false));
232 	}
233 
234 	@Test
235 	public void testNonASCIIAdd2() throws Exception {
236 		ApplyResult result = init("NonASCIIAdd2", false, true);
237 		assertEquals(1, result.getUpdatedFiles().size());
238 		assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
239 				result.getUpdatedFiles().get(0));
240 		checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
241 				b.getString(0, b.size(), false));
242 	}
243 
244 	@Test
245 	public void testNonASCIIDel() throws Exception {
246 		ApplyResult result = init("NonASCIIDel", true, false);
247 		assertEquals(1, result.getUpdatedFiles().size());
248 		assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
249 				result.getUpdatedFiles().get(0));
250 		assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
251 	}
252 
253 	@Test
254 	public void testRenameNoHunks() throws Exception {
255 		ApplyResult result = init("RenameNoHunks", true, true);
256 		assertEquals(1, result.getUpdatedFiles().size());
257 		assertEquals(new File(db.getWorkTree(), "RenameNoHunks"), result.getUpdatedFiles()
258 				.get(0));
259 		checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
260 				b.getString(0, b.size(), false));
261 	}
262 
263 	@Test
264 	public void testRenameWithHunks() throws Exception {
265 		ApplyResult result = init("RenameWithHunks", true, true);
266 		assertEquals(1, result.getUpdatedFiles().size());
267 		assertEquals(new File(db.getWorkTree(), "RenameWithHunks"), result.getUpdatedFiles()
268 				.get(0));
269 		checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
270 				b.getString(0, b.size(), false));
271 	}
272 
273 	@Test
274 	public void testCopyWithHunks() throws Exception {
275 		ApplyResult result = init("CopyWithHunks", true, true);
276 		assertEquals(1, result.getUpdatedFiles().size());
277 		assertEquals(new File(db.getWorkTree(), "CopyWithHunks"), result.getUpdatedFiles()
278 				.get(0));
279 		checkFile(new File(db.getWorkTree(), "CopyResult"),
280 				b.getString(0, b.size(), false));
281 	}
282 
283 	@Test
284 	public void testShiftUp() throws Exception {
285 		ApplyResult result = init("ShiftUp");
286 		assertEquals(1, result.getUpdatedFiles().size());
287 		assertEquals(new File(db.getWorkTree(), "ShiftUp"),
288 				result.getUpdatedFiles().get(0));
289 		checkFile(new File(db.getWorkTree(), "ShiftUp"),
290 				b.getString(0, b.size(), false));
291 	}
292 
293 	@Test
294 	public void testShiftUp2() throws Exception {
295 		ApplyResult result = init("ShiftUp2");
296 		assertEquals(1, result.getUpdatedFiles().size());
297 		assertEquals(new File(db.getWorkTree(), "ShiftUp2"),
298 				result.getUpdatedFiles().get(0));
299 		checkFile(new File(db.getWorkTree(), "ShiftUp2"),
300 				b.getString(0, b.size(), false));
301 	}
302 
303 	@Test
304 	public void testShiftDown() throws Exception {
305 		ApplyResult result = init("ShiftDown");
306 		assertEquals(1, result.getUpdatedFiles().size());
307 		assertEquals(new File(db.getWorkTree(), "ShiftDown"),
308 				result.getUpdatedFiles().get(0));
309 		checkFile(new File(db.getWorkTree(), "ShiftDown"),
310 				b.getString(0, b.size(), false));
311 	}
312 
313 	@Test
314 	public void testShiftDown2() throws Exception {
315 		ApplyResult result = init("ShiftDown2");
316 		assertEquals(1, result.getUpdatedFiles().size());
317 		assertEquals(new File(db.getWorkTree(), "ShiftDown2"),
318 				result.getUpdatedFiles().get(0));
319 		checkFile(new File(db.getWorkTree(), "ShiftDown2"),
320 				b.getString(0, b.size(), false));
321 	}
322 
323 	private static byte[] readFile(String patchFile) throws IOException {
324 		final InputStream in = getTestResource(patchFile);
325 		if (in == null) {
326 			fail("No " + patchFile + " test vector");
327 			return null; // Never happens
328 		}
329 		try {
330 			final byte[] buf = new byte[1024];
331 			final ByteArrayOutputStream temp = new ByteArrayOutputStream();
332 			int n;
333 			while ((n = in.read(buf)) > 0)
334 				temp.write(buf, 0, n);
335 			return temp.toByteArray();
336 		} finally {
337 			in.close();
338 		}
339 	}
340 
341 	private static InputStream getTestResource(String patchFile) {
342 		return ApplyCommandTest.class.getClassLoader()
343 				.getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
344 	}
345 }