View Javadoc
1   /*
2    * Copyright (C) 2011, 2012, IBM Corporation and others.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  import static org.junit.Assert.fail;
49  
50  import java.io.ByteArrayOutputStream;
51  import java.io.File;
52  import java.io.IOException;
53  import java.io.InputStream;
54  
55  import org.eclipse.jgit.api.errors.PatchApplyException;
56  import org.eclipse.jgit.api.errors.PatchFormatException;
57  import org.eclipse.jgit.diff.RawText;
58  import org.eclipse.jgit.junit.RepositoryTestCase;
59  import org.junit.Test;
60  
61  public class ApplyCommandTest extends RepositoryTestCase {
62  
63  	private RawText a;
64  
65  	private RawText b;
66  
67  	private ApplyResult init(String name) throws Exception {
68  		return init(name, true, true);
69  	}
70  
71  	private ApplyResult init(final String name, final boolean preExists,
72  			final boolean postExists) throws Exception {
73  		try (Git git = new Git(db)) {
74  			if (preExists) {
75  				a = new RawText(readFile(name + "_PreImage"));
76  				write(new File(db.getDirectory().getParent(), name),
77  						a.getString(0, a.size(), false));
78  
79  				git.add().addFilepattern(name).call();
80  				git.commit().setMessage("PreImage").call();
81  			}
82  
83  			if (postExists) {
84  				b = new RawText(readFile(name + "_PostImage"));
85  			}
86  
87  			return git
88  					.apply()
89  					.setPatch(getTestResource(name + ".patch")).call();
90  		}
91  	}
92  
93  	@Test
94  	public void testAddA1() throws Exception {
95  		ApplyResult result = init("A1", false, true);
96  		assertEquals(1, result.getUpdatedFiles().size());
97  		assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
98  				.get(0));
99  		checkFile(new File(db.getWorkTree(), "A1"),
100 				b.getString(0, b.size(), false));
101 	}
102 
103 	@Test
104 	public void testAddA2() throws Exception {
105 		ApplyResult result = init("A2", false, true);
106 		assertEquals(1, result.getUpdatedFiles().size());
107 		assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
108 				.get(0));
109 		checkFile(new File(db.getWorkTree(), "A2"),
110 				b.getString(0, b.size(), false));
111 	}
112 
113 	@Test
114 	public void testAddA1Sub() throws Exception {
115 		ApplyResult result = init("A1_sub", false, false);
116 		assertEquals(1, result.getUpdatedFiles().size());
117 		assertEquals(new File(db.getWorkTree(), "sub/A1"), result
118 				.getUpdatedFiles().get(0));
119 	}
120 
121 	@Test
122 	public void testDeleteD() throws Exception {
123 		ApplyResult result = init("D", true, false);
124 		assertEquals(1, result.getUpdatedFiles().size());
125 		assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
126 				.get(0));
127 		assertFalse(new File(db.getWorkTree(), "D").exists());
128 	}
129 
130 	@Test(expected = PatchFormatException.class)
131 	public void testFailureF1() throws Exception {
132 		init("F1", true, false);
133 	}
134 
135 	@Test(expected = PatchApplyException.class)
136 	public void testFailureF2() throws Exception {
137 		init("F2", true, false);
138 	}
139 
140 	@Test
141 	public void testModifyE() throws Exception {
142 		ApplyResult result = init("E");
143 		assertEquals(1, result.getUpdatedFiles().size());
144 		assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
145 				.get(0));
146 		checkFile(new File(db.getWorkTree(), "E"),
147 				b.getString(0, b.size(), false));
148 	}
149 
150 	@Test
151 	public void testModifyW() throws Exception {
152 		ApplyResult result = init("W");
153 		assertEquals(1, result.getUpdatedFiles().size());
154 		assertEquals(new File(db.getWorkTree(), "W"),
155 				result.getUpdatedFiles().get(0));
156 		checkFile(new File(db.getWorkTree(), "W"),
157 				b.getString(0, b.size(), false));
158 	}
159 
160 	@Test
161 	public void testAddM1() throws Exception {
162 		ApplyResult result = init("M1", false, true);
163 		assertEquals(1, result.getUpdatedFiles().size());
164 		assertTrue(result.getUpdatedFiles().get(0).canExecute());
165 		checkFile(new File(db.getWorkTree(), "M1"),
166 				b.getString(0, b.size(), false));
167 	}
168 
169 	@Test
170 	public void testModifyM2() throws Exception {
171 		ApplyResult result = init("M2", true, true);
172 		assertEquals(1, result.getUpdatedFiles().size());
173 		assertTrue(result.getUpdatedFiles().get(0).canExecute());
174 		checkFile(new File(db.getWorkTree(), "M2"),
175 				b.getString(0, b.size(), false));
176 	}
177 
178 	@Test
179 	public void testModifyM3() throws Exception {
180 		ApplyResult result = init("M3", true, true);
181 		assertEquals(1, result.getUpdatedFiles().size());
182 		assertFalse(result.getUpdatedFiles().get(0).canExecute());
183 		checkFile(new File(db.getWorkTree(), "M3"),
184 				b.getString(0, b.size(), false));
185 	}
186 
187 	@Test
188 	public void testModifyX() throws Exception {
189 		ApplyResult result = init("X");
190 		assertEquals(1, result.getUpdatedFiles().size());
191 		assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
192 				.get(0));
193 		checkFile(new File(db.getWorkTree(), "X"),
194 				b.getString(0, b.size(), false));
195 	}
196 
197 	@Test
198 	public void testModifyY() throws Exception {
199 		ApplyResult result = init("Y");
200 		assertEquals(1, result.getUpdatedFiles().size());
201 		assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
202 				.get(0));
203 		checkFile(new File(db.getWorkTree(), "Y"),
204 				b.getString(0, b.size(), false));
205 	}
206 
207 	@Test
208 	public void testModifyZ() throws Exception {
209 		ApplyResult result = init("Z");
210 		assertEquals(1, result.getUpdatedFiles().size());
211 		assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
212 				.get(0));
213 		checkFile(new File(db.getWorkTree(), "Z"),
214 				b.getString(0, b.size(), false));
215 	}
216 
217 	@Test
218 	public void testModifyNL1() throws Exception {
219 		ApplyResult result = init("NL1");
220 		assertEquals(1, result.getUpdatedFiles().size());
221 		assertEquals(new File(db.getWorkTree(), "NL1"), result
222 				.getUpdatedFiles().get(0));
223 		checkFile(new File(db.getWorkTree(), "NL1"),
224 				b.getString(0, b.size(), false));
225 	}
226 
227 	@Test
228 	public void testNonASCII() throws Exception {
229 		ApplyResult result = init("NonASCII");
230 		assertEquals(1, result.getUpdatedFiles().size());
231 		assertEquals(new File(db.getWorkTree(), "NonASCII"),
232 				result.getUpdatedFiles().get(0));
233 		checkFile(new File(db.getWorkTree(), "NonASCII"),
234 				b.getString(0, b.size(), false));
235 	}
236 
237 	@Test
238 	public void testNonASCII2() throws Exception {
239 		ApplyResult result = init("NonASCII2");
240 		assertEquals(1, result.getUpdatedFiles().size());
241 		assertEquals(new File(db.getWorkTree(), "NonASCII2"),
242 				result.getUpdatedFiles().get(0));
243 		checkFile(new File(db.getWorkTree(), "NonASCII2"),
244 				b.getString(0, b.size(), false));
245 	}
246 
247 	@Test
248 	public void testNonASCIIAdd() throws Exception {
249 		ApplyResult result = init("NonASCIIAdd");
250 		assertEquals(1, result.getUpdatedFiles().size());
251 		assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
252 				result.getUpdatedFiles().get(0));
253 		checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
254 				b.getString(0, b.size(), false));
255 	}
256 
257 	@Test
258 	public void testNonASCIIAdd2() throws Exception {
259 		ApplyResult result = init("NonASCIIAdd2", false, true);
260 		assertEquals(1, result.getUpdatedFiles().size());
261 		assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
262 				result.getUpdatedFiles().get(0));
263 		checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
264 				b.getString(0, b.size(), false));
265 	}
266 
267 	@Test
268 	public void testNonASCIIDel() throws Exception {
269 		ApplyResult result = init("NonASCIIDel", true, false);
270 		assertEquals(1, result.getUpdatedFiles().size());
271 		assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
272 				result.getUpdatedFiles().get(0));
273 		assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
274 	}
275 
276 	private static byte[] readFile(String patchFile) throws IOException {
277 		final InputStream in = getTestResource(patchFile);
278 		if (in == null) {
279 			fail("No " + patchFile + " test vector");
280 			return null; // Never happens
281 		}
282 		try {
283 			final byte[] buf = new byte[1024];
284 			final ByteArrayOutputStream temp = new ByteArrayOutputStream();
285 			int n;
286 			while ((n = in.read(buf)) > 0)
287 				temp.write(buf, 0, n);
288 			return temp.toByteArray();
289 		} finally {
290 			in.close();
291 		}
292 	}
293 
294 	private static InputStream getTestResource(String patchFile) {
295 		return ApplyCommandTest.class.getClassLoader()
296 				.getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
297 	}
298 }