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 11KB

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