jgit: Switch usage of AnyObjectId.toString() to new AnyObjectId.name()
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / revwalk / RevCommitParseTest.java
blob3d9d42dfa404fafbecd5861b6a0f1b968b3c9446
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.revwalk;
40 import org.spearce.jgit.lib.ObjectId;
41 import org.spearce.jgit.lib.PersonIdent;
42 import org.spearce.jgit.lib.RepositoryTestCase;
44 public class RevCommitParseTest extends RepositoryTestCase {
45 public void testParse_NoParents() throws Exception {
46 final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
47 final String authorName = "A U. Thor";
48 final String authorEmail = "a_u_thor@example.com";
49 final int authorTime = 1218123387;
51 final String committerName = "C O. Miter";
52 final String committerEmail = "comiter@example.com";
53 final int committerTime = 1218123390;
54 final StringBuilder body = new StringBuilder();
56 body.append("tree ");
57 body.append(treeId.name());
58 body.append("\n");
60 body.append("author ");
61 body.append(authorName);
62 body.append(" <");
63 body.append(authorEmail);
64 body.append("> ");
65 body.append(authorTime);
66 body.append(" +0700\n");
68 body.append("committer ");
69 body.append(committerName);
70 body.append(" <");
71 body.append(committerEmail);
72 body.append("> ");
73 body.append(committerTime);
74 body.append(" -0500\n");
76 body.append("\n");
78 final RevWalk rw = new RevWalk(db);
79 final RevCommit c;
81 c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
82 assertNull(c.getTree());
83 assertNull(c.parents);
85 c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
86 assertNotNull(c.getTree());
87 assertEquals(treeId, c.getTree().getId());
88 assertSame(rw.lookupTree(treeId), c.getTree());
90 assertNotNull(c.parents);
91 assertEquals(0, c.parents.length);
92 assertEquals("", c.getFullMessage());
94 final PersonIdent cAuthor = c.getAuthorIdent();
95 assertNotNull(cAuthor);
96 assertEquals(authorName, cAuthor.getName());
97 assertEquals(authorEmail, cAuthor.getEmailAddress());
99 final PersonIdent cCommitter = c.getCommitterIdent();
100 assertNotNull(cCommitter);
101 assertEquals(committerName, cCommitter.getName());
102 assertEquals(committerEmail, cCommitter.getEmailAddress());
105 private RevCommit create(final String msg) throws Exception {
106 final StringBuilder b = new StringBuilder();
107 b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
108 b.append("author A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
109 b.append("committer C O. Miter <c@example.com> 1218123390 -0500\n");
110 b.append("\n");
111 b.append(msg);
113 final RevCommit c;
114 c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
115 c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
116 return c;
119 public void testParse_WeirdHeaderOnlyCommit() throws Exception {
120 final StringBuilder b = new StringBuilder();
121 b.append("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
122 b.append("author A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
123 b.append("committer C O. Miter <c@example.com> 1218123390 -0500\n");
125 final RevCommit c;
126 c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
127 c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
129 assertEquals("", c.getFullMessage());
130 assertEquals("", c.getShortMessage());
133 public void testParse_NoMessage() throws Exception {
134 final String msg = "";
135 final RevCommit c = create(msg);
136 assertEquals(msg, c.getFullMessage());
137 assertEquals(msg, c.getShortMessage());
140 public void testParse_OnlyLFMessage() throws Exception {
141 final RevCommit c = create("\n");
142 assertEquals("\n", c.getFullMessage());
143 assertEquals("", c.getShortMessage());
146 public void testParse_ShortLineOnlyNoLF() throws Exception {
147 final String shortMsg = "This is a short message.";
148 final RevCommit c = create(shortMsg);
149 assertEquals(shortMsg, c.getFullMessage());
150 assertEquals(shortMsg, c.getShortMessage());
153 public void testParse_ShortLineOnlyEndLF() throws Exception {
154 final String shortMsg = "This is a short message.";
155 final String fullMsg = shortMsg + "\n";
156 final RevCommit c = create(fullMsg);
157 assertEquals(fullMsg, c.getFullMessage());
158 assertEquals(shortMsg, c.getShortMessage());
161 public void testParse_ShortLineOnlyEmbeddedLF() throws Exception {
162 final String fullMsg = "This is a\nshort message.";
163 final String shortMsg = fullMsg.replace('\n', ' ');
164 final RevCommit c = create(fullMsg);
165 assertEquals(fullMsg, c.getFullMessage());
166 assertEquals(shortMsg, c.getShortMessage());
169 public void testParse_ShortLineOnlyEmbeddedAndEndingLF() throws Exception {
170 final String fullMsg = "This is a\nshort message.\n";
171 final String shortMsg = "This is a short message.";
172 final RevCommit c = create(fullMsg);
173 assertEquals(fullMsg, c.getFullMessage());
174 assertEquals(shortMsg, c.getShortMessage());
177 public void testParse_GitStyleMessage() throws Exception {
178 final String shortMsg = "This fixes a bug.";
179 final String body = "We do it with magic and pixie dust and stuff.\n"
180 + "\n" + "Signed-off-by: A U. Thor <author@example.com>\n";
181 final String fullMsg = shortMsg + "\n" + "\n" + body;
182 final RevCommit c = create(fullMsg);
183 assertEquals(fullMsg, c.getFullMessage());
184 assertEquals(shortMsg, c.getShortMessage());
187 private static ObjectId id(final String str) {
188 return ObjectId.fromString(str);