2 * Copyright (C) 2009, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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
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.
44 package org
.eclipse
.jgit
.revwalk
;
46 import java
.util
.List
;
48 import org
.eclipse
.jgit
.lib
.Constants
;
49 import org
.eclipse
.jgit
.lib
.ObjectId
;
50 import org
.eclipse
.jgit
.lib
.RepositoryTestCase
;
52 public class FooterLineTest
extends RepositoryTestCase
{
53 public void testNoFooters_EmptyBody() {
54 final RevCommit commit
= parse("");
55 final List
<FooterLine
> footers
= commit
.getFooterLines();
56 assertNotNull(footers
);
57 assertEquals(0, footers
.size());
60 public void testNoFooters_NewlineOnlyBody1() {
61 final RevCommit commit
= parse("\n");
62 final List
<FooterLine
> footers
= commit
.getFooterLines();
63 assertNotNull(footers
);
64 assertEquals(0, footers
.size());
67 public void testNoFooters_NewlineOnlyBody5() {
68 final RevCommit commit
= parse("\n\n\n\n\n");
69 final List
<FooterLine
> footers
= commit
.getFooterLines();
70 assertNotNull(footers
);
71 assertEquals(0, footers
.size());
74 public void testNoFooters_OneLineBodyNoLF() {
75 final RevCommit commit
= parse("this is a commit");
76 final List
<FooterLine
> footers
= commit
.getFooterLines();
77 assertNotNull(footers
);
78 assertEquals(0, footers
.size());
81 public void testNoFooters_OneLineBodyWithLF() {
82 final RevCommit commit
= parse("this is a commit\n");
83 final List
<FooterLine
> footers
= commit
.getFooterLines();
84 assertNotNull(footers
);
85 assertEquals(0, footers
.size());
88 public void testNoFooters_ShortBodyNoLF() {
89 final RevCommit commit
= parse("subject\n\nbody of commit");
90 final List
<FooterLine
> footers
= commit
.getFooterLines();
91 assertNotNull(footers
);
92 assertEquals(0, footers
.size());
95 public void testNoFooters_ShortBodyWithLF() {
96 final RevCommit commit
= parse("subject\n\nbody of commit\n");
97 final List
<FooterLine
> footers
= commit
.getFooterLines();
98 assertNotNull(footers
);
99 assertEquals(0, footers
.size());
102 public void testSignedOffBy_OneUserNoLF() {
103 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
104 + "Signed-off-by: A. U. Thor <a@example.com>");
105 final List
<FooterLine
> footers
= commit
.getFooterLines();
108 assertNotNull(footers
);
109 assertEquals(1, footers
.size());
112 assertEquals("Signed-off-by", f
.getKey());
113 assertEquals("A. U. Thor <a@example.com>", f
.getValue());
114 assertEquals("a@example.com", f
.getEmailAddress());
117 public void testSignedOffBy_OneUserWithLF() {
118 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
119 + "Signed-off-by: A. U. Thor <a@example.com>\n");
120 final List
<FooterLine
> footers
= commit
.getFooterLines();
123 assertNotNull(footers
);
124 assertEquals(1, footers
.size());
127 assertEquals("Signed-off-by", f
.getKey());
128 assertEquals("A. U. Thor <a@example.com>", f
.getValue());
129 assertEquals("a@example.com", f
.getEmailAddress());
132 public void testSignedOffBy_IgnoreWhitespace() {
133 // We only ignore leading whitespace on the value, trailing
134 // is assumed part of the value.
136 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
137 + "Signed-off-by: A. U. Thor <a@example.com> \n");
138 final List
<FooterLine
> footers
= commit
.getFooterLines();
141 assertNotNull(footers
);
142 assertEquals(1, footers
.size());
145 assertEquals("Signed-off-by", f
.getKey());
146 assertEquals("A. U. Thor <a@example.com> ", f
.getValue());
147 assertEquals("a@example.com", f
.getEmailAddress());
150 public void testEmptyValueNoLF() {
151 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
153 final List
<FooterLine
> footers
= commit
.getFooterLines();
156 assertNotNull(footers
);
157 assertEquals(1, footers
.size());
160 assertEquals("Signed-off-by", f
.getKey());
161 assertEquals("", f
.getValue());
162 assertNull(f
.getEmailAddress());
165 public void testEmptyValueWithLF() {
166 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
167 + "Signed-off-by:\n");
168 final List
<FooterLine
> footers
= commit
.getFooterLines();
171 assertNotNull(footers
);
172 assertEquals(1, footers
.size());
175 assertEquals("Signed-off-by", f
.getKey());
176 assertEquals("", f
.getValue());
177 assertNull(f
.getEmailAddress());
180 public void testShortKey() {
181 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
183 final List
<FooterLine
> footers
= commit
.getFooterLines();
186 assertNotNull(footers
);
187 assertEquals(1, footers
.size());
190 assertEquals("K", f
.getKey());
191 assertEquals("V", f
.getValue());
192 assertNull(f
.getEmailAddress());
195 public void testNonDelimtedEmail() {
196 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
197 + "Acked-by: re@example.com\n");
198 final List
<FooterLine
> footers
= commit
.getFooterLines();
201 assertNotNull(footers
);
202 assertEquals(1, footers
.size());
205 assertEquals("Acked-by", f
.getKey());
206 assertEquals("re@example.com", f
.getValue());
207 assertEquals("re@example.com", f
.getEmailAddress());
210 public void testNotEmail() {
211 final RevCommit commit
= parse("subject\n\nbody of commit\n" + "\n"
212 + "Acked-by: Main Tain Er\n");
213 final List
<FooterLine
> footers
= commit
.getFooterLines();
216 assertNotNull(footers
);
217 assertEquals(1, footers
.size());
220 assertEquals("Acked-by", f
.getKey());
221 assertEquals("Main Tain Er", f
.getValue());
222 assertNull(f
.getEmailAddress());
225 public void testSignedOffBy_ManyUsers() {
226 final RevCommit commit
= parse("subject\n\nbody of commit\n"
227 + "Not-A-Footer-Line: this line must not be read as a footer\n"
228 + "\n" // paragraph break, now footers appear in final block
229 + "Signed-off-by: A. U. Thor <a@example.com>\n"
230 + "CC: <some.mailing.list@example.com>\n"
231 + "Acked-by: Some Reviewer <sr@example.com>\n"
232 + "Signed-off-by: Main Tain Er <mte@example.com>\n");
233 final List
<FooterLine
> footers
= commit
.getFooterLines();
236 assertNotNull(footers
);
237 assertEquals(4, footers
.size());
240 assertEquals("Signed-off-by", f
.getKey());
241 assertEquals("A. U. Thor <a@example.com>", f
.getValue());
242 assertEquals("a@example.com", f
.getEmailAddress());
245 assertEquals("CC", f
.getKey());
246 assertEquals("<some.mailing.list@example.com>", f
.getValue());
247 assertEquals("some.mailing.list@example.com", f
.getEmailAddress());
250 assertEquals("Acked-by", f
.getKey());
251 assertEquals("Some Reviewer <sr@example.com>", f
.getValue());
252 assertEquals("sr@example.com", f
.getEmailAddress());
255 assertEquals("Signed-off-by", f
.getKey());
256 assertEquals("Main Tain Er <mte@example.com>", f
.getValue());
257 assertEquals("mte@example.com", f
.getEmailAddress());
260 public void testSignedOffBy_SkipNonFooter() {
261 final RevCommit commit
= parse("subject\n\nbody of commit\n"
262 + "Not-A-Footer-Line: this line must not be read as a footer\n"
263 + "\n" // paragraph break, now footers appear in final block
264 + "Signed-off-by: A. U. Thor <a@example.com>\n"
265 + "CC: <some.mailing.list@example.com>\n"
266 + "not really a footer line but we'll skip it anyway\n"
267 + "Acked-by: Some Reviewer <sr@example.com>\n"
268 + "Signed-off-by: Main Tain Er <mte@example.com>\n");
269 final List
<FooterLine
> footers
= commit
.getFooterLines();
272 assertNotNull(footers
);
273 assertEquals(4, footers
.size());
276 assertEquals("Signed-off-by", f
.getKey());
277 assertEquals("A. U. Thor <a@example.com>", f
.getValue());
280 assertEquals("CC", f
.getKey());
281 assertEquals("<some.mailing.list@example.com>", f
.getValue());
284 assertEquals("Acked-by", f
.getKey());
285 assertEquals("Some Reviewer <sr@example.com>", f
.getValue());
288 assertEquals("Signed-off-by", f
.getKey());
289 assertEquals("Main Tain Er <mte@example.com>", f
.getValue());
292 public void testFilterFootersIgnoreCase() {
293 final RevCommit commit
= parse("subject\n\nbody of commit\n"
294 + "Not-A-Footer-Line: this line must not be read as a footer\n"
295 + "\n" // paragraph break, now footers appear in final block
296 + "Signed-Off-By: A. U. Thor <a@example.com>\n"
297 + "CC: <some.mailing.list@example.com>\n"
298 + "Acked-by: Some Reviewer <sr@example.com>\n"
299 + "signed-off-by: Main Tain Er <mte@example.com>\n");
300 final List
<String
> footers
= commit
.getFooterLines("signed-off-by");
302 assertNotNull(footers
);
303 assertEquals(2, footers
.size());
305 assertEquals("A. U. Thor <a@example.com>", footers
.get(0));
306 assertEquals("Main Tain Er <mte@example.com>", footers
.get(1));
309 public void testMatchesBugId() {
310 final RevCommit commit
= parse("this is a commit subject for test\n"
311 + "\n" // paragraph break, now footers appear in final block
312 + "Simple-Bug-Id: 42\n");
313 final List
<FooterLine
> footers
= commit
.getFooterLines();
315 assertNotNull(footers
);
316 assertEquals(1, footers
.size());
318 final FooterLine line
= footers
.get(0);
320 assertEquals("Simple-Bug-Id", line
.getKey());
321 assertEquals("42", line
.getValue());
323 final FooterKey bugid
= new FooterKey("Simple-Bug-Id");
324 assertTrue("matches Simple-Bug-Id", line
.matches(bugid
));
325 assertFalse("not Signed-off-by", line
.matches(FooterKey
.SIGNED_OFF_BY
));
326 assertFalse("not CC", line
.matches(FooterKey
.CC
));
329 private RevCommit
parse(final String msg
) {
330 final StringBuilder buf
= new StringBuilder();
331 buf
.append("tree " + ObjectId
.zeroId().name() + "\n");
332 buf
.append("author A. U. Thor <a@example.com> 1 +0000\n");
333 buf
.append("committer A. U. Thor <a@example.com> 1 +0000\n");
337 final RevWalk walk
= new RevWalk(db
);
338 walk
.setRetainBody(true);
339 final RevCommit c
= new RevCommit(ObjectId
.zeroId());
340 c
.parseCanonical(walk
, Constants
.encode(buf
.toString()));