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