Added merge support to CommitCommand
[jgit/spearce.git] / org.eclipse.jgit.test / tst / org / eclipse / jgit / api / CommitAndLogCommandTests.java
bloba62045dc9fb7a49c9a5d080995d4408f1aa52246
1 /*
2 * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
43 package org.eclipse.jgit.api;
45 import java.io.File;
46 import java.io.FileWriter;
47 import java.io.IOException;
49 import org.eclipse.jgit.errors.UnmergedPathException;
50 import org.eclipse.jgit.lib.Constants;
51 import org.eclipse.jgit.lib.ObjectId;
52 import org.eclipse.jgit.lib.PersonIdent;
53 import org.eclipse.jgit.lib.RefUpdate;
54 import org.eclipse.jgit.lib.RepositoryTestCase;
55 import org.eclipse.jgit.revwalk.RevCommit;
57 public class CommitAndLogCommandTests extends RepositoryTestCase {
58 public void testSomeCommits() throws NoHeadException, NoMessageException,
59 UnmergedPathException, ConcurrentRefUpdateException,
60 JGitInternalException, WrongRepositoryStateException {
62 // do 4 commits
63 Git git = new Git(db);
64 git.commit().setMessage("initial commit").call();
65 git.commit().setMessage("second commit").setCommitter(committer).call();
66 git.commit().setMessage("third commit").setAuthor(author).call();
67 git.commit().setMessage("fourth commit").setAuthor(author)
68 .setCommitter(committer).call();
69 Iterable<RevCommit> commits = git.log().call();
71 // check that all commits came in correctly
72 PersonIdent defaultCommitter = new PersonIdent(db);
73 PersonIdent expectedAuthors[] = new PersonIdent[] { defaultCommitter,
74 committer, author, author };
75 PersonIdent expectedCommitters[] = new PersonIdent[] {
76 defaultCommitter, committer, defaultCommitter, committer };
77 String expectedMessages[] = new String[] { "initial commit",
78 "second commit", "third commit", "fourth commit" };
79 int l = expectedAuthors.length - 1;
80 for (RevCommit c : commits) {
81 assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent()
82 .getName());
83 assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent()
84 .getName());
85 assertEquals(c.getFullMessage(), expectedMessages[l]);
86 l--;
88 assertEquals(l, -1);
91 // try to do a commit without specifying a message. Should fail!
92 public void testWrongParams() throws UnmergedPathException,
93 NoHeadException, ConcurrentRefUpdateException,
94 JGitInternalException, WrongRepositoryStateException {
95 Git git = new Git(db);
96 try {
97 git.commit().setAuthor(author).call();
98 fail("Didn't get the expected exception");
99 } catch (NoMessageException e) {
103 // try to work with Commands after command has been invoked. Should throw
104 // exceptions
105 public void testMultipleInvocations() throws NoHeadException,
106 ConcurrentRefUpdateException, NoMessageException,
107 UnmergedPathException, JGitInternalException,
108 WrongRepositoryStateException {
109 Git git = new Git(db);
110 CommitCommand commitCmd = git.commit();
111 commitCmd.setMessage("initial commit").call();
112 try {
113 // check that setters can't be called after invocation
114 commitCmd.setAuthor(author);
115 fail("didn't catch the expected exception");
116 } catch (IllegalStateException e) {
118 LogCommand logCmd = git.log();
119 logCmd.call();
120 try {
121 // check that call can't be called twice
122 logCmd.call();
123 fail("didn't catch the expected exception");
124 } catch (IllegalStateException e) {
128 public void testMergeEmptyBranches() throws IOException, NoHeadException,
129 NoMessageException, ConcurrentRefUpdateException,
130 JGitInternalException, WrongRepositoryStateException {
131 Git git = new Git(db);
132 git.commit().setMessage("initial commit").call();
133 RefUpdate r = db.updateRef("refs/heads/side");
134 r.setNewObjectId(db.resolve(Constants.HEAD));
135 assertEquals(r.forceUpdate(), RefUpdate.Result.NEW);
136 RevCommit second = git.commit().setMessage("second commit").setCommitter(committer).call();
137 db.updateRef(Constants.HEAD).link("refs/heads/side");
138 RevCommit firstSide = git.commit().setMessage("first side commit").setAuthor(author).call();
140 FileWriter wr = new FileWriter(new File(db.getDirectory(),
141 Constants.MERGE_HEAD));
142 wr.write(ObjectId.toString(db.resolve("refs/heads/master")));
143 wr.close();
144 wr = new FileWriter(new File(db.getDirectory(), Constants.MERGE_MSG));
145 wr.write("merging");
146 wr.close();
148 RevCommit commit = git.commit().call();
149 RevCommit[] parents = commit.getParents();
150 assertEquals(parents[0], firstSide);
151 assertEquals(parents[1], second);
152 assertTrue(parents.length==2);