Add parsing support for Signed-off-by lines in commit messages
[jgit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / revwalk / FooterLineTest.java
blob342346fdc5d732b7e85864e2328b2bf24bd405b7
1 /*
2 * Copyright (C) 2009, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.revwalk;
40 import java.util.List;
42 import org.spearce.jgit.lib.Constants;
43 import org.spearce.jgit.lib.ObjectId;
44 import org.spearce.jgit.lib.RepositoryTestCase;
46 public class FooterLineTest extends RepositoryTestCase {
47 public void testNoFooters_EmptyBody() {
48 final RevCommit commit = parse("");
49 final List<FooterLine> footers = commit.getFooterLines();
50 assertNotNull(footers);
51 assertEquals(0, footers.size());
54 public void testNoFooters_NewlineOnlyBody1() {
55 final RevCommit commit = parse("\n");
56 final List<FooterLine> footers = commit.getFooterLines();
57 assertNotNull(footers);
58 assertEquals(0, footers.size());
61 public void testNoFooters_NewlineOnlyBody5() {
62 final RevCommit commit = parse("\n\n\n\n\n");
63 final List<FooterLine> footers = commit.getFooterLines();
64 assertNotNull(footers);
65 assertEquals(0, footers.size());
68 public void testNoFooters_OneLineBodyNoLF() {
69 final RevCommit commit = parse("this is a commit");
70 final List<FooterLine> footers = commit.getFooterLines();
71 assertNotNull(footers);
72 assertEquals(0, footers.size());
75 public void testNoFooters_OneLineBodyWithLF() {
76 final RevCommit commit = parse("this is a commit\n");
77 final List<FooterLine> footers = commit.getFooterLines();
78 assertNotNull(footers);
79 assertEquals(0, footers.size());
82 public void testNoFooters_ShortBodyNoLF() {
83 final RevCommit commit = parse("subject\n\nbody of commit");
84 final List<FooterLine> footers = commit.getFooterLines();
85 assertNotNull(footers);
86 assertEquals(0, footers.size());
89 public void testNoFooters_ShortBodyWithLF() {
90 final RevCommit commit = parse("subject\n\nbody of commit\n");
91 final List<FooterLine> footers = commit.getFooterLines();
92 assertNotNull(footers);
93 assertEquals(0, footers.size());
96 public void testSignedOffBy_OneUserNoLF() {
97 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
98 + "Signed-off-by: A. U. Thor <a@example.com>");
99 final List<FooterLine> footers = commit.getFooterLines();
100 FooterLine f;
102 assertNotNull(footers);
103 assertEquals(1, footers.size());
105 f = footers.get(0);
106 assertEquals("Signed-off-by", f.getKey());
107 assertEquals("A. U. Thor <a@example.com>", f.getValue());
110 public void testSignedOffBy_OneUserWithLF() {
111 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
112 + "Signed-off-by: A. U. Thor <a@example.com>\n");
113 final List<FooterLine> footers = commit.getFooterLines();
114 FooterLine f;
116 assertNotNull(footers);
117 assertEquals(1, footers.size());
119 f = footers.get(0);
120 assertEquals("Signed-off-by", f.getKey());
121 assertEquals("A. U. Thor <a@example.com>", f.getValue());
124 public void testSignedOffBy_IgnoreWhitespace() {
125 // We only ignore leading whitespace on the value, trailing
126 // is assumed part of the value.
128 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
129 + "Signed-off-by: A. U. Thor <a@example.com> \n");
130 final List<FooterLine> footers = commit.getFooterLines();
131 FooterLine f;
133 assertNotNull(footers);
134 assertEquals(1, footers.size());
136 f = footers.get(0);
137 assertEquals("Signed-off-by", f.getKey());
138 assertEquals("A. U. Thor <a@example.com> ", f.getValue());
141 public void testEmptyValueNoLF() {
142 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
143 + "Signed-off-by:");
144 final List<FooterLine> footers = commit.getFooterLines();
145 FooterLine f;
147 assertNotNull(footers);
148 assertEquals(1, footers.size());
150 f = footers.get(0);
151 assertEquals("Signed-off-by", f.getKey());
152 assertEquals("", f.getValue());
155 public void testEmptyValueWithLF() {
156 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
157 + "Signed-off-by:\n");
158 final List<FooterLine> footers = commit.getFooterLines();
159 FooterLine f;
161 assertNotNull(footers);
162 assertEquals(1, footers.size());
164 f = footers.get(0);
165 assertEquals("Signed-off-by", f.getKey());
166 assertEquals("", f.getValue());
169 public void testShortKey() {
170 final RevCommit commit = parse("subject\n\nbody of commit\n" + "\n"
171 + "K:V\n");
172 final List<FooterLine> footers = commit.getFooterLines();
173 FooterLine f;
175 assertNotNull(footers);
176 assertEquals(1, footers.size());
178 f = footers.get(0);
179 assertEquals("K", f.getKey());
180 assertEquals("V", f.getValue());
183 public void testSignedOffBy_ManyUsers() {
184 final RevCommit commit = parse("subject\n\nbody of commit\n"
185 + "Not-A-Footer-Line: this line must not be read as a footer\n"
186 + "\n" // paragraph break, now footers appear in final block
187 + "Signed-off-by: A. U. Thor <a@example.com>\n"
188 + "CC: <some.mailing.list@example.com>\n"
189 + "Acked-by: Some Reviewer <sr@example.com>\n"
190 + "Signed-off-by: Main Tain Er <mte@example.com>\n");
191 final List<FooterLine> footers = commit.getFooterLines();
192 FooterLine f;
194 assertNotNull(footers);
195 assertEquals(4, footers.size());
197 f = footers.get(0);
198 assertEquals("Signed-off-by", f.getKey());
199 assertEquals("A. U. Thor <a@example.com>", f.getValue());
201 f = footers.get(1);
202 assertEquals("CC", f.getKey());
203 assertEquals("<some.mailing.list@example.com>", f.getValue());
205 f = footers.get(2);
206 assertEquals("Acked-by", f.getKey());
207 assertEquals("Some Reviewer <sr@example.com>", f.getValue());
209 f = footers.get(3);
210 assertEquals("Signed-off-by", f.getKey());
211 assertEquals("Main Tain Er <mte@example.com>", f.getValue());
214 public void testSignedOffBy_SkipNonFooter() {
215 final RevCommit commit = parse("subject\n\nbody of commit\n"
216 + "Not-A-Footer-Line: this line must not be read as a footer\n"
217 + "\n" // paragraph break, now footers appear in final block
218 + "Signed-off-by: A. U. Thor <a@example.com>\n"
219 + "CC: <some.mailing.list@example.com>\n"
220 + "not really a footer line but we'll skip it anyway\n"
221 + "Acked-by: Some Reviewer <sr@example.com>\n"
222 + "Signed-off-by: Main Tain Er <mte@example.com>\n");
223 final List<FooterLine> footers = commit.getFooterLines();
224 FooterLine f;
226 assertNotNull(footers);
227 assertEquals(4, footers.size());
229 f = footers.get(0);
230 assertEquals("Signed-off-by", f.getKey());
231 assertEquals("A. U. Thor <a@example.com>", f.getValue());
233 f = footers.get(1);
234 assertEquals("CC", f.getKey());
235 assertEquals("<some.mailing.list@example.com>", f.getValue());
237 f = footers.get(2);
238 assertEquals("Acked-by", f.getKey());
239 assertEquals("Some Reviewer <sr@example.com>", f.getValue());
241 f = footers.get(3);
242 assertEquals("Signed-off-by", f.getKey());
243 assertEquals("Main Tain Er <mte@example.com>", f.getValue());
246 public void testFilterFootersIgnoreCase() {
247 final RevCommit commit = parse("subject\n\nbody of commit\n"
248 + "Not-A-Footer-Line: this line must not be read as a footer\n"
249 + "\n" // paragraph break, now footers appear in final block
250 + "Signed-Off-By: A. U. Thor <a@example.com>\n"
251 + "CC: <some.mailing.list@example.com>\n"
252 + "Acked-by: Some Reviewer <sr@example.com>\n"
253 + "signed-off-by: Main Tain Er <mte@example.com>\n");
254 final List<String> footers = commit.getFooterLines("signed-off-by");
256 assertNotNull(footers);
257 assertEquals(2, footers.size());
259 assertEquals("A. U. Thor <a@example.com>", footers.get(0));
260 assertEquals("Main Tain Er <mte@example.com>", footers.get(1));
263 private RevCommit parse(final String msg) {
264 final StringBuilder buf = new StringBuilder();
265 buf.append("tree " + ObjectId.zeroId().name() + "\n");
266 buf.append("author A. U. Thor <a@example.com> 1 +0000\n");
267 buf.append("committer A. U. Thor <a@example.com> 1 +0000\n");
268 buf.append("\n");
269 buf.append(msg);
271 final RevWalk walk = new RevWalk(db);
272 walk.setRetainBody(true);
273 final RevCommit c = new RevCommit(ObjectId.zeroId());
274 c.parseCanonical(walk, Constants.encode(buf.toString()));
275 return c;