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.

RevCommitParseTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) 2008-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.io.ByteArrayOutputStream;
  45. import org.eclipse.jgit.lib.Constants;
  46. import org.eclipse.jgit.lib.ObjectId;
  47. import org.eclipse.jgit.lib.PersonIdent;
  48. import org.eclipse.jgit.lib.RepositoryTestCase;
  49. public class RevCommitParseTest extends RepositoryTestCase {
  50. public void testParse_NoParents() throws Exception {
  51. final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
  52. final String authorName = "A U. Thor";
  53. final String authorEmail = "a_u_thor@example.com";
  54. final int authorTime = 1218123387;
  55. final String committerName = "C O. Miter";
  56. final String committerEmail = "comiter@example.com";
  57. final int committerTime = 1218123390;
  58. final StringBuilder body = new StringBuilder();
  59. body.append("tree ");
  60. body.append(treeId.name());
  61. body.append("\n");
  62. body.append("author ");
  63. body.append(authorName);
  64. body.append(" <");
  65. body.append(authorEmail);
  66. body.append("> ");
  67. body.append(authorTime);
  68. body.append(" +0700\n");
  69. body.append("committer ");
  70. body.append(committerName);
  71. body.append(" <");
  72. body.append(committerEmail);
  73. body.append("> ");
  74. body.append(committerTime);
  75. body.append(" -0500\n");
  76. body.append("\n");
  77. final RevWalk rw = new RevWalk(db);
  78. final RevCommit c;
  79. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  80. assertNull(c.getTree());
  81. assertNull(c.parents);
  82. c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
  83. assertNotNull(c.getTree());
  84. assertEquals(treeId, c.getTree().getId());
  85. assertSame(rw.lookupTree(treeId), c.getTree());
  86. assertNotNull(c.parents);
  87. assertEquals(0, c.parents.length);
  88. assertEquals("", c.getFullMessage());
  89. final PersonIdent cAuthor = c.getAuthorIdent();
  90. assertNotNull(cAuthor);
  91. assertEquals(authorName, cAuthor.getName());
  92. assertEquals(authorEmail, cAuthor.getEmailAddress());
  93. final PersonIdent cCommitter = c.getCommitterIdent();
  94. assertNotNull(cCommitter);
  95. assertEquals(committerName, cCommitter.getName());
  96. assertEquals(committerEmail, cCommitter.getEmailAddress());
  97. }
  98. private RevCommit create(final String msg) throws Exception {
  99. final StringBuilder b = new StringBuilder();
  100. b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
  101. b.append("author A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
  102. b.append("committer C O. Miter <c@example.com> 1218123390 -0500\n");
  103. b.append("\n");
  104. b.append(msg);
  105. final RevCommit c;
  106. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  107. c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
  108. return c;
  109. }
  110. public void testParse_WeirdHeaderOnlyCommit() throws Exception {
  111. final StringBuilder b = new StringBuilder();
  112. b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
  113. b.append("author A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
  114. b.append("committer C O. Miter <c@example.com> 1218123390 -0500\n");
  115. final RevCommit c;
  116. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  117. c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
  118. assertEquals("", c.getFullMessage());
  119. assertEquals("", c.getShortMessage());
  120. }
  121. public void testParse_implicit_UTF8_encoded() throws Exception {
  122. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  123. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  124. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("UTF-8"));
  125. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  126. b.write("\n".getBytes("UTF-8"));
  127. b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
  128. b.write("\n".getBytes("UTF-8"));
  129. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  130. final RevCommit c;
  131. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  132. c.parseCanonical(new RevWalk(db), b.toByteArray());
  133. assertSame(Constants.CHARSET, c.getEncoding());
  134. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  135. assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
  136. assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c.getFullMessage());
  137. }
  138. public void testParse_implicit_mixed_encoded() throws Exception {
  139. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  140. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  141. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("ISO-8859-1"));
  142. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  143. b.write("\n".getBytes("UTF-8"));
  144. b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
  145. b.write("\n".getBytes("UTF-8"));
  146. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  147. final RevCommit c;
  148. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  149. c.parseCanonical(new RevWalk(db), b.toByteArray());
  150. assertSame(Constants.CHARSET, c.getEncoding());
  151. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  152. assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
  153. assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c.getFullMessage());
  154. }
  155. /**
  156. * Test parsing of a commit whose encoding is given and works.
  157. *
  158. * @throws Exception
  159. */
  160. public void testParse_explicit_encoded() throws Exception {
  161. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  162. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("EUC-JP"));
  163. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("EUC-JP"));
  164. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("EUC-JP"));
  165. b.write("encoding euc_JP\n".getBytes("EUC-JP"));
  166. b.write("\n".getBytes("EUC-JP"));
  167. b.write("\u304d\u308c\u3044\n".getBytes("EUC-JP"));
  168. b.write("\n".getBytes("EUC-JP"));
  169. b.write("Hi\n".getBytes("EUC-JP"));
  170. final RevCommit c;
  171. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  172. c.parseCanonical(new RevWalk(db), b.toByteArray());
  173. assertEquals("EUC-JP", c.getEncoding().name());
  174. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  175. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  176. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  177. }
  178. /**
  179. * This is a twisted case, but show what we expect here. We can revise the
  180. * expectations provided this case is updated.
  181. *
  182. * What happens here is that an encoding us given, but data is not encoded
  183. * that way (and we can detect it), so we try other encodings.
  184. *
  185. * @throws Exception
  186. */
  187. public void testParse_explicit_bad_encoded() throws Exception {
  188. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  189. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  190. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("ISO-8859-1"));
  191. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  192. b.write("encoding EUC-JP\n".getBytes("UTF-8"));
  193. b.write("\n".getBytes("UTF-8"));
  194. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  195. b.write("\n".getBytes("UTF-8"));
  196. b.write("Hi\n".getBytes("UTF-8"));
  197. final RevCommit c;
  198. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  199. c.parseCanonical(new RevWalk(db), b.toByteArray());
  200. assertEquals("EUC-JP", c.getEncoding().name());
  201. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  202. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  203. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  204. }
  205. /**
  206. * This is a twisted case too, but show what we expect here. We can revise the
  207. * expectations provided this case is updated.
  208. *
  209. * What happens here is that an encoding us given, but data is not encoded
  210. * that way (and we can detect it), so we try other encodings. Here data could
  211. * actually be decoded in the stated encoding, but we override using UTF-8.
  212. *
  213. * @throws Exception
  214. */
  215. public void testParse_explicit_bad_encoded2() throws Exception {
  216. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  217. b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("UTF-8"));
  218. b.write("author F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n".getBytes("UTF-8"));
  219. b.write("committer C O. Miter <c@example.com> 1218123390 -0500\n".getBytes("UTF-8"));
  220. b.write("encoding ISO-8859-1\n".getBytes("UTF-8"));
  221. b.write("\n".getBytes("UTF-8"));
  222. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  223. b.write("\n".getBytes("UTF-8"));
  224. b.write("Hi\n".getBytes("UTF-8"));
  225. final RevCommit c;
  226. c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); // bogus id
  227. c.parseCanonical(new RevWalk(db), b.toByteArray());
  228. assertEquals("ISO-8859-1", c.getEncoding().name());
  229. assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName());
  230. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  231. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  232. }
  233. public void testParse_NoMessage() throws Exception {
  234. final String msg = "";
  235. final RevCommit c = create(msg);
  236. assertEquals(msg, c.getFullMessage());
  237. assertEquals(msg, c.getShortMessage());
  238. }
  239. public void testParse_OnlyLFMessage() throws Exception {
  240. final RevCommit c = create("\n");
  241. assertEquals("\n", c.getFullMessage());
  242. assertEquals("", c.getShortMessage());
  243. }
  244. public void testParse_ShortLineOnlyNoLF() throws Exception {
  245. final String shortMsg = "This is a short message.";
  246. final RevCommit c = create(shortMsg);
  247. assertEquals(shortMsg, c.getFullMessage());
  248. assertEquals(shortMsg, c.getShortMessage());
  249. }
  250. public void testParse_ShortLineOnlyEndLF() throws Exception {
  251. final String shortMsg = "This is a short message.";
  252. final String fullMsg = shortMsg + "\n";
  253. final RevCommit c = create(fullMsg);
  254. assertEquals(fullMsg, c.getFullMessage());
  255. assertEquals(shortMsg, c.getShortMessage());
  256. }
  257. public void testParse_ShortLineOnlyEmbeddedLF() throws Exception {
  258. final String fullMsg = "This is a\nshort message.";
  259. final String shortMsg = fullMsg.replace('\n', ' ');
  260. final RevCommit c = create(fullMsg);
  261. assertEquals(fullMsg, c.getFullMessage());
  262. assertEquals(shortMsg, c.getShortMessage());
  263. }
  264. public void testParse_ShortLineOnlyEmbeddedAndEndingLF() throws Exception {
  265. final String fullMsg = "This is a\nshort message.\n";
  266. final String shortMsg = "This is a short message.";
  267. final RevCommit c = create(fullMsg);
  268. assertEquals(fullMsg, c.getFullMessage());
  269. assertEquals(shortMsg, c.getShortMessage());
  270. }
  271. public void testParse_GitStyleMessage() throws Exception {
  272. final String shortMsg = "This fixes a bug.";
  273. final String body = "We do it with magic and pixie dust and stuff.\n"
  274. + "\n" + "Signed-off-by: A U. Thor <author@example.com>\n";
  275. final String fullMsg = shortMsg + "\n" + "\n" + body;
  276. final RevCommit c = create(fullMsg);
  277. assertEquals(fullMsg, c.getFullMessage());
  278. assertEquals(shortMsg, c.getShortMessage());
  279. }
  280. private static ObjectId id(final String str) {
  281. return ObjectId.fromString(str);
  282. }
  283. }