Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RevTagParseTest.java 14KB

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