123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- /*
- * Copyright (C) 2008-2010, Google Inc.
- * and other copyright owners as documented in the project's IP log.
- *
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
- package org.eclipse.jgit.revwalk;
-
- import static java.nio.charset.StandardCharsets.UTF_8;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertSame;
-
- import java.io.ByteArrayOutputStream;
-
- import org.eclipse.jgit.errors.CorruptObjectException;
- import org.eclipse.jgit.junit.RepositoryTestCase;
- import org.eclipse.jgit.lib.Constants;
- import org.eclipse.jgit.lib.ObjectId;
- import org.eclipse.jgit.lib.ObjectInserter;
- import org.eclipse.jgit.lib.PersonIdent;
- import org.eclipse.jgit.lib.TagBuilder;
- import org.junit.Test;
-
- public class RevTagParseTest extends RepositoryTestCase {
- @Test
- public void testTagBlob() throws Exception {
- testOneType(Constants.OBJ_BLOB);
- }
-
- @Test
- public void testTagTree() throws Exception {
- testOneType(Constants.OBJ_TREE);
- }
-
- @Test
- public void testTagCommit() throws Exception {
- testOneType(Constants.OBJ_COMMIT);
- }
-
- @Test
- public void testTagTag() throws Exception {
- testOneType(Constants.OBJ_TAG);
- }
-
- private void testOneType(final int typeCode) throws Exception {
- final ObjectId id = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
- final StringBuilder b = new StringBuilder();
- b.append("object " + id.name() + "\n");
- b.append("type " + Constants.typeString(typeCode) + "\n");
- b.append("tag v1.2.3.4.5\n");
- b.append("tagger A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
- b.append("\n");
-
- final RevWalk rw = new RevWalk(db);
- final RevTag c;
-
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- assertNull(c.getObject());
- assertNull(c.getTagName());
-
- c.parseCanonical(rw, b.toString().getBytes("UTF-8"));
- assertNotNull(c.getObject());
- assertEquals(id, c.getObject().getId());
- assertSame(rw.lookupAny(id, typeCode), c.getObject());
- }
-
- @Test
- public void testParseAllFields() throws Exception {
- final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
- final String name = "v1.2.3.4.5";
- final String taggerName = "A U. Thor";
- final String taggerEmail = "a_u_thor@example.com";
- final int taggerTime = 1218123387;
-
- final StringBuilder body = new StringBuilder();
-
- body.append("object ");
- body.append(treeId.name());
- body.append("\n");
-
- body.append("type tree\n");
-
- body.append("tag ");
- body.append(name);
- body.append("\n");
-
- body.append("tagger ");
- body.append(taggerName);
- body.append(" <");
- body.append(taggerEmail);
- body.append("> ");
- body.append(taggerTime);
- body.append(" +0700\n");
-
- body.append("\n");
-
- final RevWalk rw = new RevWalk(db);
- final RevTag c;
-
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- assertNull(c.getObject());
- assertNull(c.getTagName());
-
- c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
- assertNotNull(c.getObject());
- assertEquals(treeId, c.getObject().getId());
- assertSame(rw.lookupTree(treeId), c.getObject());
-
- assertNotNull(c.getTagName());
- assertEquals(name, c.getTagName());
- assertEquals("", c.getFullMessage());
-
- final PersonIdent cTagger = c.getTaggerIdent();
- assertNotNull(cTagger);
- assertEquals(taggerName, cTagger.getName());
- assertEquals(taggerEmail, cTagger.getEmailAddress());
- }
-
- @Test
- public void testParseOldStyleNoTagger() throws Exception {
- final ObjectId treeId = id("9788669ad918b6fcce64af8882fc9a81cb6aba67");
- final String name = "v1.2.3.4.5";
- final String message = "test\n" //
- + "\n" //
- + "-----BEGIN PGP SIGNATURE-----\n" //
- + "Version: GnuPG v1.4.1 (GNU/Linux)\n" //
- + "\n" //
- + "iD8DBQBC0b9oF3Y\n" //
- + "-----END PGP SIGNATURE------n";
-
- final StringBuilder body = new StringBuilder();
-
- body.append("object ");
- body.append(treeId.name());
- body.append("\n");
-
- body.append("type tree\n");
-
- body.append("tag ");
- body.append(name);
- body.append("\n");
- body.append("\n");
- body.append(message);
-
- final RevWalk rw = new RevWalk(db);
- final RevTag c;
-
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- assertNull(c.getObject());
- assertNull(c.getTagName());
-
- c.parseCanonical(rw, body.toString().getBytes("UTF-8"));
- assertNotNull(c.getObject());
- assertEquals(treeId, c.getObject().getId());
- assertSame(rw.lookupTree(treeId), c.getObject());
-
- assertNotNull(c.getTagName());
- assertEquals(name, c.getTagName());
- assertEquals("test", c.getShortMessage());
- assertEquals(message, c.getFullMessage());
-
- assertNull(c.getTaggerIdent());
- }
-
- private RevTag create(final String msg) throws Exception {
- final StringBuilder b = new StringBuilder();
- b.append("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n");
- b.append("type tree\n");
- b.append("tag v1.2.3.4.5\n");
- b.append("tagger A U. Thor <a_u_thor@example.com> 1218123387 +0700\n");
- b.append("\n");
- b.append(msg);
-
- final RevTag c;
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- c.parseCanonical(new RevWalk(db), b.toString().getBytes("UTF-8"));
- return c;
- }
-
- @Test
- public void testParse_implicit_UTF8_encoded() throws Exception {
- final ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
- .getBytes("UTF-8"));
- b.write("type tree\n".getBytes("UTF-8"));
- b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
-
- b
- .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
- .getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
- final RevTag c;
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- c.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
- assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
- assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c
- .getFullMessage());
- }
-
- @Test
- public void testParse_implicit_mixed_encoded() throws Exception {
- final ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
- .getBytes("UTF-8"));
- b.write("type tree\n".getBytes("UTF-8"));
- b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
- b
- .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
- .getBytes("ISO-8859-1"));
- b.write("\n".getBytes("UTF-8"));
- b.write("Sm\u00f6rg\u00e5sbord\n".getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
- final RevTag c;
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- c.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
- assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage());
- assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c
- .getFullMessage());
- }
-
- /**
- * Test parsing of a commit whose encoding is given and works.
- *
- * @throws Exception
- */
- @Test
- public void testParse_explicit_encoded() throws Exception {
- final ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
- .getBytes("EUC-JP"));
- b.write("type tree\n".getBytes("EUC-JP"));
- b.write("tag v1.2.3.4.5\n".getBytes("EUC-JP"));
- b
- .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
- .getBytes("EUC-JP"));
- b.write("encoding euc_JP\n".getBytes("EUC-JP"));
- b.write("\n".getBytes("EUC-JP"));
- b.write("\u304d\u308c\u3044\n".getBytes("EUC-JP"));
- b.write("\n".getBytes("EUC-JP"));
- b.write("Hi\n".getBytes("EUC-JP"));
- final RevTag c;
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- c.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
- assertEquals("\u304d\u308c\u3044", c.getShortMessage());
- assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
- }
-
- /**
- * This is a twisted case, but show what we expect here. We can revise the
- * expectations provided this case is updated.
- *
- * What happens here is that an encoding us given, but data is not encoded
- * that way (and we can detect it), so we try other encodings.
- *
- * @throws Exception
- */
- @Test
- public void testParse_explicit_bad_encoded() throws Exception {
- final ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
- .getBytes("UTF-8"));
- b.write("type tree\n".getBytes("UTF-8"));
- b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
- b
- .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
- .getBytes("ISO-8859-1"));
- b.write("encoding EUC-JP\n".getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("Hi\n".getBytes("UTF-8"));
- final RevTag c;
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- c.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
- assertEquals("\u304d\u308c\u3044", c.getShortMessage());
- assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
- }
-
- /**
- * This is a twisted case too, but show what we expect here. We can revise
- * the expectations provided this case is updated.
- *
- * What happens here is that an encoding us given, but data is not encoded
- * that way (and we can detect it), so we try other encodings. Here data
- * could actually be decoded in the stated encoding, but we override using
- * UTF-8.
- *
- * @throws Exception
- */
- @Test
- public void testParse_explicit_bad_encoded2() throws Exception {
- final ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n"
- .getBytes("UTF-8"));
- b.write("type tree\n".getBytes("UTF-8"));
- b.write("tag v1.2.3.4.5\n".getBytes("UTF-8"));
- b
- .write("tagger F\u00f6r fattare <a_u_thor@example.com> 1218123387 +0700\n"
- .getBytes("UTF-8"));
- b.write("encoding ISO-8859-1\n".getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("\u304d\u308c\u3044\n".getBytes("UTF-8"));
- b.write("\n".getBytes("UTF-8"));
- b.write("Hi\n".getBytes("UTF-8"));
- final RevTag c;
- c = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- c.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("F\u00f6r fattare", c.getTaggerIdent().getName());
- assertEquals("\u304d\u308c\u3044", c.getShortMessage());
- assertEquals("\u304d\u308c\u3044\n\nHi\n", c.getFullMessage());
- }
-
- @Test
- public void testParse_illegalEncoding() throws Exception {
- ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8));
- b.write("type tree\n".getBytes(UTF_8));
- b.write("tag v1.0\n".getBytes(UTF_8));
- b.write("tagger t <t@example.com> 1218123387 +0700\n".getBytes(UTF_8));
- b.write("encoding utf-8logoutputencoding=gbk\n".getBytes(UTF_8));
- b.write("\n".getBytes(UTF_8));
- b.write("message\n".getBytes(UTF_8));
-
- RevTag t = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- t.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("t", t.getTaggerIdent().getName());
- assertEquals("message", t.getShortMessage());
- assertEquals("message\n", t.getFullMessage());
- }
-
- @Test
- public void testParse_unsupportedEncoding() throws Exception {
- ByteArrayOutputStream b = new ByteArrayOutputStream();
- b.write("object 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8));
- b.write("type tree\n".getBytes(UTF_8));
- b.write("tag v1.0\n".getBytes(UTF_8));
- b.write("tagger t <t@example.com> 1218123387 +0700\n".getBytes(UTF_8));
- b.write("encoding it_IT.UTF8\n".getBytes(UTF_8));
- b.write("\n".getBytes(UTF_8));
- b.write("message\n".getBytes(UTF_8));
-
- RevTag t = new RevTag(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67"));
- t.parseCanonical(new RevWalk(db), b.toByteArray());
-
- assertEquals("t", t.getTaggerIdent().getName());
- assertEquals("message", t.getShortMessage());
- assertEquals("message\n", t.getFullMessage());
- }
-
- @Test
- public void testParse_NoMessage() throws Exception {
- final String msg = "";
- final RevTag c = create(msg);
- assertEquals(msg, c.getFullMessage());
- assertEquals(msg, c.getShortMessage());
- }
-
- @Test
- public void testParse_OnlyLFMessage() throws Exception {
- final RevTag c = create("\n");
- assertEquals("\n", c.getFullMessage());
- assertEquals("", c.getShortMessage());
- }
-
- @Test
- public void testParse_ShortLineOnlyNoLF() throws Exception {
- final String shortMsg = "This is a short message.";
- final RevTag c = create(shortMsg);
- assertEquals(shortMsg, c.getFullMessage());
- assertEquals(shortMsg, c.getShortMessage());
- }
-
- @Test
- public void testParse_ShortLineOnlyEndLF() throws Exception {
- final String shortMsg = "This is a short message.";
- final String fullMsg = shortMsg + "\n";
- final RevTag c = create(fullMsg);
- assertEquals(fullMsg, c.getFullMessage());
- assertEquals(shortMsg, c.getShortMessage());
- }
-
- @Test
- public void testParse_ShortLineOnlyEmbeddedLF() throws Exception {
- final String fullMsg = "This is a\nshort message.";
- final String shortMsg = fullMsg.replace('\n', ' ');
- final RevTag c = create(fullMsg);
- assertEquals(fullMsg, c.getFullMessage());
- assertEquals(shortMsg, c.getShortMessage());
- }
-
- @Test
- public void testParse_ShortLineOnlyEmbeddedAndEndingLF() throws Exception {
- final String fullMsg = "This is a\nshort message.\n";
- final String shortMsg = "This is a short message.";
- final RevTag c = create(fullMsg);
- assertEquals(fullMsg, c.getFullMessage());
- assertEquals(shortMsg, c.getShortMessage());
- }
-
- @Test
- public void testParse_GitStyleMessage() throws Exception {
- final String shortMsg = "This fixes a bug.";
- final String body = "We do it with magic and pixie dust and stuff.\n"
- + "\n" + "Signed-off-by: A U. Thor <author@example.com>\n";
- final String fullMsg = shortMsg + "\n" + "\n" + body;
- final RevTag c = create(fullMsg);
- assertEquals(fullMsg, c.getFullMessage());
- assertEquals(shortMsg, c.getShortMessage());
- }
-
- @Test
- public void testParse_PublicParseMethod() throws CorruptObjectException {
- TagBuilder src = new TagBuilder();
- try (ObjectInserter.Formatter fmt = new ObjectInserter.Formatter()) {
- src.setObjectId(fmt.idFor(Constants.OBJ_TREE, new byte[] {}),
- Constants.OBJ_TREE);
- }
- src.setTagger(committer);
- src.setTag("a.test");
- src.setMessage("Test tag\n\nThis is a test.\n");
-
- RevTag p = RevTag.parse(src.build());
- assertEquals(src.getObjectId(), p.getObject());
- assertEquals(committer, p.getTaggerIdent());
- assertEquals("a.test", p.getTagName());
- assertEquals("Test tag", p.getShortMessage());
- assertEquals(src.getMessage(), p.getFullMessage());
- }
-
- private static ObjectId id(final String str) {
- return ObjectId.fromString(str);
- }
- }
|