1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
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 }