View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
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  
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 		// We only ignore leading whitespace on the value, trailing
151 		// is assumed part of the value.
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" // paragraph break, now footers appear in final block
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" // paragraph break, now footers appear in final block
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" // paragraph break, now footers appear in final block
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" // paragraph break, now footers appear in final block
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 }