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

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