You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FooterLineTest.java 12KB

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