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
44 package org.eclipse.jgit.revwalk;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertTrue;
51 import java.io.IOException;
52 import java.util.List;
53
54 import org.eclipse.jgit.junit.RepositoryTestCase;
55 import org.eclipse.jgit.lib.Constants;
56 import org.eclipse.jgit.lib.ObjectId;
57 import org.junit.Test;
58
59 public class FooterLineTest extends RepositoryTestCase {
60 @Test
61 public void testNoFooters_EmptyBody() throws IOException {
62 final RevCommit commit = parse("");
63 final List<FooterLine> footers = commit.getFooterLines();
64 assertNotNull(footers);
65 assertEquals(0, footers.size());
66 }
67
68 @Test
69 public void testNoFooters_NewlineOnlyBody1() throws IOException {
70 final RevCommit commit = parse("\n");
71 final List<FooterLine> footers = commit.getFooterLines();
72 assertNotNull(footers);
73 assertEquals(0, footers.size());
74 }
75
76 @Test
77 public void testNoFooters_NewlineOnlyBody5() throws IOException {
78 final RevCommit commit = parse("\n\n\n\n\n");
79 final List<FooterLine> footers = commit.getFooterLines();
80 assertNotNull(footers);
81 assertEquals(0, footers.size());
82 }
83
84 @Test
85 public void testNoFooters_OneLineBodyNoLF() throws IOException {
86 final RevCommit commit = parse("this is a commit");
87 final List<FooterLine> footers = commit.getFooterLines();
88 assertNotNull(footers);
89 assertEquals(0, footers.size());
90 }
91
92 @Test
93 public void testNoFooters_OneLineBodyWithLF() throws IOException {
94 final RevCommit commit = parse("this is a commit\n");
95 final List<FooterLine> footers = commit.getFooterLines();
96 assertNotNull(footers);
97 assertEquals(0, footers.size());
98 }
99
100 @Test
101 public void testNoFooters_ShortBodyNoLF() throws IOException {
102 final RevCommit commit = parse("subject\n\nbody of commit");
103 final List<FooterLine> footers = commit.getFooterLines();
104 assertNotNull(footers);
105 assertEquals(0, footers.size());
106 }
107
108 @Test
109 public void testNoFooters_ShortBodyWithLF() throws IOException {
110 final RevCommit commit = parse("subject\n\nbody of commit\n");
111 final List<FooterLine> footers = commit.getFooterLines();
112 assertNotNull(footers);
113 assertEquals(0, footers.size());
114 }
115
116 @Test
117 public void testSignedOffBy_OneUserNoLF() throws IOException {
118 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
119 + "Signed-off-by: A. U. Thor <a@example.com>");
120 final List<FooterLine> footers = commit.getFooterLines();
121 FooterLine f;
122
123 assertNotNull(footers);
124 assertEquals(1, footers.size());
125
126 f = footers.get(0);
127 assertEquals("Signed-off-by", f.getKey());
128 assertEquals("A. U. Thor <a@example.com>", f.getValue());
129 assertEquals("a@example.com", f.getEmailAddress());
130 }
131
132 @Test
133 public void testSignedOffBy_OneUserWithLF() throws IOException {
134 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
135 + "Signed-off-by: A. U. Thor <a@example.com>\n");
136 final List<FooterLine> footers = commit.getFooterLines();
137 FooterLine f;
138
139 assertNotNull(footers);
140 assertEquals(1, footers.size());
141
142 f = footers.get(0);
143 assertEquals("Signed-off-by", f.getKey());
144 assertEquals("A. U. Thor <a@example.com>", f.getValue());
145 assertEquals("a@example.com", f.getEmailAddress());
146 }
147
148 @Test
149 public void testSignedOffBy_IgnoreWhitespace() throws IOException {
150
151
152
153 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
154 + "Signed-off-by: A. U. Thor <a@example.com> \n");
155 final List<FooterLine> footers = commit.getFooterLines();
156 FooterLine f;
157
158 assertNotNull(footers);
159 assertEquals(1, footers.size());
160
161 f = footers.get(0);
162 assertEquals("Signed-off-by", f.getKey());
163 assertEquals("A. U. Thor <a@example.com> ", f.getValue());
164 assertEquals("a@example.com", f.getEmailAddress());
165 }
166
167 @Test
168 public void testEmptyValueNoLF() throws IOException {
169 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
170 + "Signed-off-by:");
171 final List<FooterLine> footers = commit.getFooterLines();
172 FooterLine f;
173
174 assertNotNull(footers);
175 assertEquals(1, footers.size());
176
177 f = footers.get(0);
178 assertEquals("Signed-off-by", f.getKey());
179 assertEquals("", f.getValue());
180 assertNull(f.getEmailAddress());
181 }
182
183 @Test
184 public void testEmptyValueWithLF() throws IOException {
185 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
186 + "Signed-off-by:\n");
187 final List<FooterLine> footers = commit.getFooterLines();
188 FooterLine f;
189
190 assertNotNull(footers);
191 assertEquals(1, footers.size());
192
193 f = footers.get(0);
194 assertEquals("Signed-off-by", f.getKey());
195 assertEquals("", f.getValue());
196 assertNull(f.getEmailAddress());
197 }
198
199 @Test
200 public void testShortKey() throws IOException {
201 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
202 + "K:V\n");
203 final List<FooterLine> footers = commit.getFooterLines();
204 FooterLine f;
205
206 assertNotNull(footers);
207 assertEquals(1, footers.size());
208
209 f = footers.get(0);
210 assertEquals("K", f.getKey());
211 assertEquals("V", f.getValue());
212 assertNull(f.getEmailAddress());
213 }
214
215 @Test
216 public void testNonDelimtedEmail() throws IOException {
217 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
218 + "Acked-by: re@example.com\n");
219 final List<FooterLine> footers = commit.getFooterLines();
220 FooterLine f;
221
222 assertNotNull(footers);
223 assertEquals(1, footers.size());
224
225 f = footers.get(0);
226 assertEquals("Acked-by", f.getKey());
227 assertEquals("re@example.com", f.getValue());
228 assertEquals("re@example.com", f.getEmailAddress());
229 }
230
231 @Test
232 public void testNotEmail() throws IOException {
233 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
234 + "Acked-by: Main Tain Er\n");
235 final List<FooterLine> footers = commit.getFooterLines();
236 FooterLine f;
237
238 assertNotNull(footers);
239 assertEquals(1, footers.size());
240
241 f = footers.get(0);
242 assertEquals("Acked-by", f.getKey());
243 assertEquals("Main Tain Er", f.getValue());
244 assertNull(f.getEmailAddress());
245 }
246
247 @Test
248 public void testSignedOffBy_ManyUsers() throws IOException {
249 final RevCommit commit = parse("subject\n\nbody of commit\n"
250 + "Not-A-Footer-Line: this line must not be read as a footer\n"
251 + "\n"
252 + "Signed-off-by: A. U. Thor <a@example.com>\n"
253 + "CC: <some.mailing.list@example.com>\n"
254 + "Acked-by: Some Reviewer <sr@example.com>\n"
255 + "Signed-off-by: Main Tain Er <mte@example.com>\n");
256 final List<FooterLine> footers = commit.getFooterLines();
257 FooterLine f;
258
259 assertNotNull(footers);
260 assertEquals(4, footers.size());
261
262 f = footers.get(0);
263 assertEquals("Signed-off-by", f.getKey());
264 assertEquals("A. U. Thor <a@example.com>", f.getValue());
265 assertEquals("a@example.com", f.getEmailAddress());
266
267 f = footers.get(1);
268 assertEquals("CC", f.getKey());
269 assertEquals("<some.mailing.list@example.com>", f.getValue());
270 assertEquals("some.mailing.list@example.com", f.getEmailAddress());
271
272 f = footers.get(2);
273 assertEquals("Acked-by", f.getKey());
274 assertEquals("Some Reviewer <sr@example.com>", f.getValue());
275 assertEquals("sr@example.com", f.getEmailAddress());
276
277 f = footers.get(3);
278 assertEquals("Signed-off-by", f.getKey());
279 assertEquals("Main Tain Er <mte@example.com>", f.getValue());
280 assertEquals("mte@example.com", f.getEmailAddress());
281 }
282
283 @Test
284 public void testSignedOffBy_SkipNonFooter() throws IOException {
285 final RevCommit commit = parse("subject\n\nbody of commit\n"
286 + "Not-A-Footer-Line: this line must not be read as a footer\n"
287 + "\n"
288 + "Signed-off-by: A. U. Thor <a@example.com>\n"
289 + "CC: <some.mailing.list@example.com>\n"
290 + "not really a footer line but we'll skip it anyway\n"
291 + "Acked-by: Some Reviewer <sr@example.com>\n"
292 + "Signed-off-by: Main Tain Er <mte@example.com>\n");
293 final List<FooterLine> footers = commit.getFooterLines();
294 FooterLine f;
295
296 assertNotNull(footers);
297 assertEquals(4, footers.size());
298
299 f = footers.get(0);
300 assertEquals("Signed-off-by", f.getKey());
301 assertEquals("A. U. Thor <a@example.com>", f.getValue());
302
303 f = footers.get(1);
304 assertEquals("CC", f.getKey());
305 assertEquals("<some.mailing.list@example.com>", f.getValue());
306
307 f = footers.get(2);
308 assertEquals("Acked-by", f.getKey());
309 assertEquals("Some Reviewer <sr@example.com>", f.getValue());
310
311 f = footers.get(3);
312 assertEquals("Signed-off-by", f.getKey());
313 assertEquals("Main Tain Er <mte@example.com>", f.getValue());
314 }
315
316 @Test
317 public void testFilterFootersIgnoreCase() throws IOException {
318 final RevCommit commit = parse("subject\n\nbody of commit\n"
319 + "Not-A-Footer-Line: this line must not be read as a footer\n"
320 + "\n"
321 + "Signed-Off-By: A. U. Thor <a@example.com>\n"
322 + "CC: <some.mailing.list@example.com>\n"
323 + "Acked-by: Some Reviewer <sr@example.com>\n"
324 + "signed-off-by: Main Tain Er <mte@example.com>\n");
325 final List<String> footers = commit.getFooterLines("signed-off-by");
326
327 assertNotNull(footers);
328 assertEquals(2, footers.size());
329
330 assertEquals("A. U. Thor <a@example.com>", footers.get(0));
331 assertEquals("Main Tain Er <mte@example.com>", footers.get(1));
332 }
333
334 @Test
335 public void testMatchesBugId() throws IOException {
336 final RevCommit commit = parse("this is a commit subject for test\n"
337 + "\n"
338 + "Simple-Bug-Id: 42\n");
339 final List<FooterLine> footers = commit.getFooterLines();
340
341 assertNotNull(footers);
342 assertEquals(1, footers.size());
343
344 final FooterLine line = footers.get(0);
345 assertNotNull(line);
346 assertEquals("Simple-Bug-Id", line.getKey());
347 assertEquals("42", line.getValue());
348
349 final FooterKey bugid = new FooterKey("Simple-Bug-Id");
350 assertTrue("matches Simple-Bug-Id", line.matches(bugid));
351 assertFalse("not Signed-off-by", line.matches(FooterKey.SIGNED_OFF_BY));
352 assertFalse("not CC", line.matches(FooterKey.CC));
353 }
354
355 private RevCommit parse(final String msg) throws IOException {
356 final StringBuilder buf = new StringBuilder();
357 buf.append("tree " + ObjectId.zeroId().name() + "\n");
358 buf.append("author A. U. Thor <a@example.com> 1 +0000\n");
359 buf.append("committer A. U. Thor <a@example.com> 1 +0000\n");
360 buf.append("\n");
361 buf.append(msg);
362
363 try (RevWalk walk = new RevWalk(db)) {
364 RevCommit c = new RevCommit(ObjectId.zeroId());
365 c.parseCanonical(walk, Constants.encode(buf.toString()));
366 return c;
367 }
368 }
369 }