Minor whitespace cleanup
[nbgit.git] / test / unit / src / org / nbgit / client / CommitBuilderTest.java
blob31febf4407d6e68452344902bd1a7fa675c42bc5
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html. See the License for the
12 * specific language governing permissions and limitations under the
13 * License. When distributing the software, include this License Header
14 * Notice in each file.
16 * This particular file is subject to the "Classpath" exception as provided
17 * by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the
19 * License Header, with the fields enclosed by brackets [] replaced by
20 * your own identifying information:
21 * "Portions Copyrighted [year] [name of copyright owner]"
23 * Contributor(s):
25 * If you wish your version of this file to be governed by only the CDDL
26 * or only the GPL Version 2, indicate your decision by adding
27 * "[Contributor] elects to include this software in this distribution
28 * under the [CDDL or GPL Version 2] license." If you do not indicate a
29 * single choice of license, a recipient has the option to distribute
30 * your version of this file under either the CDDL, the GPL Version 2 or
31 * to extend the choice of license to its licensees as provided above.
32 * However, if you add GPL Version 2 code and therefore, elected the GPL
33 * Version 2 license, then the option applies only if the new code is
34 * made subject to such option by the copyright holder.
36 package org.nbgit.client;
38 import org.nbgit.junit.RepositoryTestCase;
39 import org.eclipse.jgit.lib.Commit;
40 import org.eclipse.jgit.lib.ObjectId;
41 import org.eclipse.jgit.lib.Tree;
42 import org.eclipse.jgit.lib.TreeEntry;
44 public class CommitBuilderTest extends RepositoryTestCase {
46 public CommitBuilderTest() {
47 super(CommitBuilderTest.class.getSimpleName());
50 public void testCreate() throws Exception {
51 assertNotNull(CommitBuilder.create(repository));
52 assertNotNull(CommitBuilder.create(workDir));
55 public void testEmptyCommit() throws Exception {
56 CommitBuilder.create(repository).
57 time(getTime(), getTimeZone()).
58 log(logger).
59 message(message()).
60 write();
61 compareCommitFiles();
64 public void testNonEmptyCommit() throws Exception {
65 CommitBuilder.create(repository).
66 time(getTime(), getTimeZone()).
67 log(logger).
68 addAll(toFiles(workDir, "a")).
69 message(message()).
70 write();
71 compareCommitFiles();
74 public void testInitialEmptyCommit() throws Exception {
75 toGitDirFile("packed-refs").delete();
76 CommitBuilder.create(repository).
77 time(getTime(), getTimeZone()).
78 log(logger).
79 message(message()).
80 write();
81 compareCommitFiles();
84 public void testInitialNonEmptyCommit() throws Exception {
85 toGitDirFile("packed-refs").delete();
86 CommitBuilder.create(repository).
87 time(getTime(), getTimeZone()).
88 log(logger).
89 addAll(toFiles(workDir, "a")).
90 message(message()).
91 write();
92 compareCommitFiles();
95 private String message() {
96 return getName() + "\n";
99 private void compareCommitFiles() throws Exception {
100 refCommit(repository.mapCommit("HEAD"));
101 compareReferenceFiles();
104 private void refCommit(Commit commit) throws Exception {
105 refCommitLine("commit", commit.getCommitId().name());
106 refCommitLine("tree", commit.getTreeId().name());
107 for (ObjectId id : commit.getParentIds())
108 refCommitLine("parent", id.name());
109 refCommitLine("author", commit.getAuthor().toExternalString());
110 refCommitLine("committer", commit.getCommitter().toExternalString());
112 ref("");
113 for (String line : commit.getMessage().split("\n")) {
114 ref(" " + line);
117 if (commit.getTree().memberCount() > 0)
118 ref("");
119 refTree(commit.getTree());
122 private void refCommitLine(String name, String value) {
123 ref(name + " " + value);
126 private void refTree(Tree tree) throws Exception {
127 for (TreeEntry entry : tree.members()) {
128 if (entry instanceof Tree) {
129 refTree(tree);
130 } else {
131 refTreeEntry(entry);
136 private void refTreeEntry(TreeEntry entry) throws Exception {
137 ref(String.format("%o blob %s\t%s",
138 entry.getMode().getBits(), entry.getId().name(), entry.getFullName()));