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.fail;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.File;
51  import java.io.IOException;
52  import java.io.InputStream;
53  
54  import org.eclipse.jgit.api.errors.PatchApplyException;
55  import org.eclipse.jgit.api.errors.PatchFormatException;
56  import org.eclipse.jgit.diff.RawText;
57  import org.eclipse.jgit.junit.RepositoryTestCase;
58  import org.junit.Test;
59  
60  public class ApplyCommandTest extends RepositoryTestCase {
61  
62  	private RawText a;
63  
64  	private RawText b;
65  
66  	private ApplyResult init(final String name) throws Exception {
67  		return init(name, true, true);
68  	}
69  
70  	private ApplyResult init(final String name, final boolean preExists,
71  			final boolean postExists) throws Exception {
72  		Git git = new Git(db);
73  
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  		return git
87  				.apply()
88  				.setPatch(getTestResource(name + ".patch")).call();
89  	}
90  
91  	@Test
92  	public void testAddA1() throws Exception {
93  		ApplyResult result = init("A1", false, true);
94  		assertEquals(1, result.getUpdatedFiles().size());
95  		assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
96  				.get(0));
97  		checkFile(new File(db.getWorkTree(), "A1"),
98  				b.getString(0, b.size(), false));
99  	}
100 
101 	@Test
102 	public void testAddA2() throws Exception {
103 		ApplyResult result = init("A2", false, true);
104 		assertEquals(1, result.getUpdatedFiles().size());
105 		assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
106 				.get(0));
107 		checkFile(new File(db.getWorkTree(), "A2"),
108 				b.getString(0, b.size(), false));
109 	}
110 
111 	@Test
112 	public void testAddA1Sub() throws Exception {
113 		ApplyResult result = init("A1_sub", false, false);
114 		assertEquals(1, result.getUpdatedFiles().size());
115 		assertEquals(new File(db.getWorkTree(), "sub/A1"), result
116 				.getUpdatedFiles().get(0));
117 	}
118 
119 	@Test
120 	public void testDeleteD() throws Exception {
121 		ApplyResult result = init("D", true, false);
122 		assertEquals(1, result.getUpdatedFiles().size());
123 		assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
124 				.get(0));
125 		assertFalse(new File(db.getWorkTree(), "D").exists());
126 	}
127 
128 	@Test(expected = PatchFormatException.class)
129 	public void testFailureF1() throws Exception {
130 		init("F1", true, false);
131 	}
132 
133 	@Test(expected = PatchApplyException.class)
134 	public void testFailureF2() throws Exception {
135 		init("F2", true, false);
136 	}
137 
138 	@Test
139 	public void testModifyE() throws Exception {
140 		ApplyResult result = init("E");
141 		assertEquals(1, result.getUpdatedFiles().size());
142 		assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
143 				.get(0));
144 		checkFile(new File(db.getWorkTree(), "E"),
145 				b.getString(0, b.size(), false));
146 	}
147 
148 	@Test
149 	public void testModifyX() throws Exception {
150 		ApplyResult result = init("X");
151 		assertEquals(1, result.getUpdatedFiles().size());
152 		assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
153 				.get(0));
154 		checkFile(new File(db.getWorkTree(), "X"),
155 				b.getString(0, b.size(), false));
156 	}
157 
158 	@Test
159 	public void testModifyY() throws Exception {
160 		ApplyResult result = init("Y");
161 		assertEquals(1, result.getUpdatedFiles().size());
162 		assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
163 				.get(0));
164 		checkFile(new File(db.getWorkTree(), "Y"),
165 				b.getString(0, b.size(), false));
166 	}
167 
168 	@Test
169 	public void testModifyZ() throws Exception {
170 		ApplyResult result = init("Z");
171 		assertEquals(1, result.getUpdatedFiles().size());
172 		assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
173 				.get(0));
174 		checkFile(new File(db.getWorkTree(), "Z"),
175 				b.getString(0, b.size(), false));
176 	}
177 
178 	@Test
179 	public void testModifyNL1() throws Exception {
180 		ApplyResult result = init("NL1");
181 		assertEquals(1, result.getUpdatedFiles().size());
182 		assertEquals(new File(db.getWorkTree(), "NL1"), result
183 				.getUpdatedFiles().get(0));
184 		checkFile(new File(db.getWorkTree(), "NL1"),
185 				b.getString(0, b.size(), false));
186 	}
187 
188 	private static byte[] readFile(final String patchFile) throws IOException {
189 		final InputStream in = getTestResource(patchFile);
190 		if (in == null) {
191 			fail("No " + patchFile + " test vector");
192 			return null; // Never happens
193 		}
194 		try {
195 			final byte[] buf = new byte[1024];
196 			final ByteArrayOutputStream temp = new ByteArrayOutputStream();
197 			int n;
198 			while ((n = in.read(buf)) > 0)
199 				temp.write(buf, 0, n);
200 			return temp.toByteArray();
201 		} finally {
202 			in.close();
203 		}
204 	}
205 
206 	private static InputStream getTestResource(final String patchFile) {
207 		return ApplyCommandTest.class.getClassLoader()
208 				.getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
209 	}
210 }