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.

RevTagParseTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (C) 2008-2010, 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.assertNotNull;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertSame;
  48. import java.io.ByteArrayOutputStream;
  49. import org.eclipse.jgit.errors.CorruptObjectException;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectInserter;
  54. import org.eclipse.jgit.lib.PersonIdent;
  55. import org.eclipse.jgit.lib.TagBuilder;
  56. import org.junit.Test;
  57. public class RevTagParseTest extends RepositoryTestCase {
  58. @Test
  59. public void testTagBlob() throws Exception {
  60. testOneType(Constants.OBJ_BLOB);
  61. }
  62. @Test
  63. public void testTagTree() throws Exception {
  64. testOneType(Constants.OBJ_TREE);
  65. }
  66. @Test
  67. public void testTagCommit() throws Exception {
  68. testOneType(Constants.OBJ_COMMIT);
  69. }
  70. @Test
  71. public void testTagTag() throws Exception {
  72. testOneType(Constants.OBJ_TAG);
  73. }
  74. private void testOneType(final int typeCode) throws Exception {
  75. final ObjectId id = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
  76. final StringBuilder b = new StringBuilder();
  77. b.append("object " + id.name() + "\n");
  78. b.append("type " + Constants.typeString(typeCode) + "\n");
  79. b.append("tag v1.2.3.4.5\n");
  80. b.append("tagger A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
  81. b.append("\n");
  82. final RevWalk rw = new RevWalk(db);
  83. final RevTag c;
  84. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  85. assertNull(c.getObject());
  86. assertNull(c.getTagName());
  87. c.parseCanonical(rw, b.toString().getBytes("UTF-8"));
  88. assertNotNull(c.getObject());
  89. assertEquals(id, c.getObject().getId());
  90. assertSame(rw.lookupAny(id, typeCode), c.getObject());
  91. }
  92. @Test
  93. public void testParseAllFields() throws Exception {
  94. final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
  95. final String name = "v1.2.3.4.5";
  96. final String taggerName = "A U. Thor";
  97. final String taggerEmail = "a_u_thor@example.com";
  98. final int taggerTime = 1218123387;
  99. final StringBuilder body = new StringBuilder();
  100. body.append("object ");
  101. body.append(treeId.name());
  102. body.append("\n");
  103. body.append("type tree\n");
  104. body.append("tag ");
  105. body.append(name);
  106. body.append("\n");
  107. body.append("tagger ");
  108. body.append(taggerName);
  109. body.append(" <");
  110. body.append(taggerEmail);
  111. body.append("> ");
  112. body.append(taggerTime);
  113. body.append(" +0700\n");
  114. body.append("\n");
  115. final RevWalk rw = new RevWalk(db);
  116. final RevTag c;
  117. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  118. assertNull(c.getObject());
  119. assertNull(c.getTagName());
  120. c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
  121. assertNotNull(c.getObject());
  122. assertEquals(treeId, c.getObject().getId());
  123. assertSame(rw.lookupTree(treeId), c.getObject());
  124. assertNotNull(c.getTagName());
  125. assertEquals(name, c.getTagName());
  126. assertEquals("", c.getFullMessage());
  127. final PersonIdent cTagger = c.getTaggerIdent();
  128. assertNotNull(cTagger);
  129. assertEquals(taggerName, cTagger.getName());
  130. assertEquals(taggerEmail, cTagger.getEmailAddress());
  131. }
  132. @Test
  133. public void testParseOldStyleNoTagger() throws Exception {
  134. final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
  135. final String name = "v1.2.3.4.5";
  136. final String message = "test\n" //
  137. + "\n" //
  138. + "-----BEGIN PGP SIGNATURE-----\n" //
  139. + "Version: GnuPG v1.4.1 (GNU/Linux)\n" //
  140. + "\n" //
  141. + "iD8DBQBC0b9oF3Y\n" //
  142. + "-----END PGP SIGNATURE------n";
  143. final StringBuilder body = new StringBuilder();
  144. body.append("object ");
  145. body.append(treeId.name());
  146. body.append("\n");
  147. body.append("type tree\n");
  148. body.append("tag ");
  149. body.append(name);
  150. body.append("\n");
  151. body.append("\n");
  152. body.append(message);
  153. final RevWalk rw = new RevWalk(db);
  154. final RevTag c;
  155. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  156. assertNull(c.getObject());
  157. assertNull(c.getTagName());
  158. c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
  159. assertNotNull(c.getObject());
  160. assertEquals(treeId, c.getObject().getId());
  161. assertSame(rw.lookupTree(treeId), c.getObject());
  162. assertNotNull(c.getTagName());
  163. assertEquals(name, c.getTagName());
  164. assertEquals("test", c.getShortMessage());
  165. assertEquals(message, c.getFullMessage());
  166. assertNull(c.getTaggerIdent());
  167. }
  168. private RevTag create(final String msg) throws Exception {
  169. final StringBuilder b = new StringBuilder();
  170. b.append("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
  171. b.append("type tree\n");
  172. b.append("tag v1.2.3.4.5\n");
  173. b.append("tagger A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
  174. b.append("\n");
  175. b.append(msg);
  176. final RevTag c;
  177. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  178. c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
  179. return c;
  180. }
  181. @Test
  182. public void testParse_implicit_UTF8_encoded() throws Exception {
  183. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  184. b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
  185. .getBytes("UTF-8"));
  186. b.write("type tree\n".getBytes("UTF-8"));
  187. b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
  188. b
  189. .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
  190. .getBytes("UTF-8"));
  191. b.write("\n".getBytes("UTF-8"));
  192. b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
  193. b.write("\n".getBytes("UTF-8"));
  194. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  195. final RevTag c;
  196. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  197. c.parseCanonical(new RevWalk(db), b.toByteArray());
  198. assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
  199. assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
  200. assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c
  201. .getFullMessage());
  202. }
  203. @Test
  204. public void testParse_implicit_mixed_encoded() throws Exception {
  205. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  206. b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
  207. .getBytes("UTF-8"));
  208. b.write("type tree\n".getBytes("UTF-8"));
  209. b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
  210. b
  211. .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
  212. .getBytes("ISO-8859-1"));
  213. b.write("\n".getBytes("UTF-8"));
  214. b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
  215. b.write("\n".getBytes("UTF-8"));
  216. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  217. final RevTag c;
  218. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  219. c.parseCanonical(new RevWalk(db), b.toByteArray());
  220. assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
  221. assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
  222. assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c
  223. .getFullMessage());
  224. }
  225. /**
  226. * Test parsing of a commit whose encoding is given and works.
  227. *
  228. * @throws Exception
  229. */
  230. @Test
  231. public void testParse_explicit_encoded() throws Exception {
  232. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  233. b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
  234. .getBytes("EUC-JP"));
  235. b.write("type tree\n".getBytes("EUC-JP"));
  236. b.write("tag v1.2.3.4.5\n".getBytes("EUC-JP"));
  237. b
  238. .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
  239. .getBytes("EUC-JP"));
  240. b.write("encoding euc_JP\n".getBytes("EUC-JP"));
  241. b.write("\n".getBytes("EUC-JP"));
  242. b.write("\u304d\u308c\u3044\n".getBytes("EUC-JP"));
  243. b.write("\n".getBytes("EUC-JP"));
  244. b.write("Hi\n".getBytes("EUC-JP"));
  245. final RevTag c;
  246. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  247. c.parseCanonical(new RevWalk(db), b.toByteArray());
  248. assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
  249. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  250. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  251. }
  252. /**
  253. * This is a twisted case, but show what we expect here. We can revise the
  254. * expectations provided this case is updated.
  255. *
  256. * What happens here is that an encoding us given, but data is not encoded
  257. * that way (and we can detect it), so we try other encodings.
  258. *
  259. * @throws Exception
  260. */
  261. @Test
  262. public void testParse_explicit_bad_encoded() throws Exception {
  263. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  264. b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
  265. .getBytes("UTF-8"));
  266. b.write("type tree\n".getBytes("UTF-8"));
  267. b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
  268. b
  269. .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
  270. .getBytes("ISO-8859-1"));
  271. b.write("encoding EUC-JP\n".getBytes("UTF-8"));
  272. b.write("\n".getBytes("UTF-8"));
  273. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  274. b.write("\n".getBytes("UTF-8"));
  275. b.write("Hi\n".getBytes("UTF-8"));
  276. final RevTag c;
  277. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  278. c.parseCanonical(new RevWalk(db), b.toByteArray());
  279. assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
  280. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  281. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  282. }
  283. /**
  284. * This is a twisted case too, but show what we expect here. We can revise
  285. * the expectations provided this case is updated.
  286. *
  287. * What happens here is that an encoding us given, but data is not encoded
  288. * that way (and we can detect it), so we try other encodings. Here data
  289. * could actually be decoded in the stated encoding, but we override using
  290. * UTF-8.
  291. *
  292. * @throws Exception
  293. */
  294. @Test
  295. public void testParse_explicit_bad_encoded2() throws Exception {
  296. final ByteArrayOutputStream b = new ByteArrayOutputStream();
  297. b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
  298. .getBytes("UTF-8"));
  299. b.write("type tree\n".getBytes("UTF-8"));
  300. b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
  301. b
  302. .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
  303. .getBytes("UTF-8"));
  304. b.write("encoding ISO-8859-1\n".getBytes("UTF-8"));
  305. b.write("\n".getBytes("UTF-8"));
  306. b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
  307. b.write("\n".getBytes("UTF-8"));
  308. b.write("Hi\n".getBytes("UTF-8"));
  309. final RevTag c;
  310. c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
  311. c.parseCanonical(new RevWalk(db), b.toByteArray());
  312. assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
  313. assertEquals("\u304d\u308c\u3044", c.getShortMessage());
  314. assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
  315. }
  316. @Test
  317. public void testParse_NoMessage() throws Exception {
  318. final String msg = "";
  319. final RevTag c = create(msg);
  320. assertEquals(msg, c.getFullMessage());
  321. assertEquals(msg, c.getShortMessage());
  322. }
  323. @Test
  324. public void testParse_OnlyLFMessage() throws Exception {
  325. final RevTag c = create("\n");
  326. assertEquals("\n", c.getFullMessage());
  327. assertEquals("", c.getShortMessage());
  328. }
  329. @Test
  330. public void testParse_ShortLineOnlyNoLF() throws Exception {
  331. final String shortMsg = "This is a short message.";
  332. final RevTag c = create(shortMsg);
  333. assertEquals(shortMsg, c.getFullMessage());
  334. assertEquals(shortMsg, c.getShortMessage());
  335. }
  336. @Test
  337. public void testParse_ShortLineOnlyEndLF() throws Exception {
  338. final String shortMsg = "This is a short message.";
  339. final String fullMsg = shortMsg + "\n";
  340. final RevTag c = create(fullMsg);
  341. assertEquals(fullMsg, c.getFullMessage());
  342. assertEquals(shortMsg, c.getShortMessage());
  343. }
  344. @Test
  345. public void testParse_ShortLineOnlyEmbeddedLF() throws Exception {
  346. final String fullMsg = "This is a\nshort message.";
  347. final String shortMsg = fullMsg.replace('\n', ' ');
  348. final RevTag c = create(fullMsg);
  349. assertEquals(fullMsg, c.getFullMessage());
  350. assertEquals(shortMsg, c.getShortMessage());
  351. }
  352. @Test
  353. public void testParse_ShortLineOnlyEmbeddedAndEndingLF() throws Exception {
  354. final String fullMsg = "This is a\nshort message.\n";
  355. final String shortMsg = "This is a short message.";
  356. final RevTag c = create(fullMsg);
  357. assertEquals(fullMsg, c.getFullMessage());
  358. assertEquals(shortMsg, c.getShortMessage());
  359. }
  360. @Test
  361. public void testParse_GitStyleMessage() throws Exception {
  362. final String shortMsg = "This fixes a bug.";
  363. final String body = "We do it with magic and pixie dust and stuff.\n"
  364. + "\n" + "Signed-off-by: A U. Thor <author@example.com>\n";
  365. final String fullMsg = shortMsg + "\n" + "\n" + body;
  366. final RevTag c = create(fullMsg);
  367. assertEquals(fullMsg, c.getFullMessage());
  368. assertEquals(shortMsg, c.getShortMessage());
  369. }
  370. @Test
  371. public void testParse_PublicParseMethod() throws CorruptObjectException {
  372. ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
  373. TagBuilder src = new TagBuilder();
  374. src.setObjectId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}),
  375. Constants.OBJ_TREE);
  376. src.setTagger(committer);
  377. src.setTag("a.test");
  378. src.setMessage("Test tag\n\nThis is a test.\n");
  379. RevTag p = RevTag.parse(src.build());
  380. assertEquals(src.getObjectId(), p.getObject());
  381. assertEquals(committer, p.getTaggerIdent());
  382. assertEquals("a.test", p.getTagName());
  383. assertEquals("Test tag", p.getShortMessage());
  384. assertEquals(src.getMessage(), p.getFullMessage());
  385. }
  386. private static ObjectId id(final String str) {
  387. return ObjectId.fromString(str);
  388. }
  389. }