Initial JGit contribution to eclipse.org
[jgit/MarioXXX.git] / org.eclipse.jgit.test / tst / org / eclipse / jgit / revwalk / RevWalkTestCase.java
blob50fbce41aa1b8f1de42f1a778e433ec6cd874f9e
1 /*
2 * Copyright (C) 2009, Google Inc.
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.
44 package org.eclipse.jgit.revwalk;
46 import java.util.Collections;
47 import java.util.Date;
49 import org.eclipse.jgit.dircache.DirCache;
50 import org.eclipse.jgit.dircache.DirCacheBuilder;
51 import org.eclipse.jgit.dircache.DirCacheEntry;
52 import org.eclipse.jgit.lib.Commit;
53 import org.eclipse.jgit.lib.Constants;
54 import org.eclipse.jgit.lib.FileMode;
55 import org.eclipse.jgit.lib.ObjectId;
56 import org.eclipse.jgit.lib.ObjectWriter;
57 import org.eclipse.jgit.lib.PersonIdent;
58 import org.eclipse.jgit.lib.RepositoryTestCase;
59 import org.eclipse.jgit.lib.Tag;
60 import org.eclipse.jgit.lib.Tree;
61 import org.eclipse.jgit.treewalk.TreeWalk;
62 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
64 /** Support for tests of the {@link RevWalk} class. */
65 public abstract class RevWalkTestCase extends RepositoryTestCase {
66 protected ObjectWriter ow;
68 protected RevTree emptyTree;
70 protected long nowTick;
72 protected RevWalk rw;
74 public void setUp() throws Exception {
75 super.setUp();
76 ow = new ObjectWriter(db);
77 rw = createRevWalk();
78 emptyTree = rw.parseTree(ow.writeTree(new Tree(db)));
79 nowTick = 1236977987000L;
82 protected RevWalk createRevWalk() {
83 return new RevWalk(db);
86 protected void tick(final int secDelta) {
87 nowTick += secDelta * 1000L;
90 protected RevBlob blob(final String content) throws Exception {
91 return rw.lookupBlob(ow.writeBlob(Constants.encode(content)));
94 protected DirCacheEntry file(final String path, final RevBlob blob)
95 throws Exception {
96 final DirCacheEntry e = new DirCacheEntry(path);
97 e.setFileMode(FileMode.REGULAR_FILE);
98 e.setObjectId(blob);
99 return e;
102 protected RevTree tree(final DirCacheEntry... entries) throws Exception {
103 final DirCache dc = DirCache.newInCore();
104 final DirCacheBuilder b = dc.builder();
105 for (final DirCacheEntry e : entries)
106 b.add(e);
107 b.finish();
108 return rw.lookupTree(dc.writeTree(ow));
111 protected RevObject get(final RevTree tree, final String path)
112 throws Exception {
113 final TreeWalk tw = new TreeWalk(db);
114 tw.setFilter(PathFilterGroup.createFromStrings(Collections
115 .singleton(path)));
116 tw.reset(tree);
117 while (tw.next()) {
118 if (tw.isSubtree() && !path.equals(tw.getPathString())) {
119 tw.enterSubtree();
120 continue;
122 final ObjectId entid = tw.getObjectId(0);
123 final FileMode entmode = tw.getFileMode(0);
124 return rw.lookupAny(entid, entmode.getObjectType());
126 fail("Can't find " + path + " in tree " + tree.name());
127 return null; // never reached.
130 protected RevCommit commit(final RevCommit... parents) throws Exception {
131 return commit(1, emptyTree, parents);
134 protected RevCommit commit(final RevTree tree, final RevCommit... parents)
135 throws Exception {
136 return commit(1, tree, parents);
139 protected RevCommit commit(final int secDelta, final RevCommit... parents)
140 throws Exception {
141 return commit(secDelta, emptyTree, parents);
144 protected RevCommit commit(final int secDelta, final RevTree tree,
145 final RevCommit... parents) throws Exception {
146 tick(secDelta);
147 final Commit c = new Commit(db);
148 c.setTreeId(tree);
149 c.setParentIds(parents);
150 c.setAuthor(new PersonIdent(jauthor, new Date(nowTick)));
151 c.setCommitter(new PersonIdent(jcommitter, new Date(nowTick)));
152 c.setMessage("");
153 return rw.lookupCommit(ow.writeCommit(c));
156 protected RevTag tag(final String name, final RevObject dst)
157 throws Exception {
158 final Tag t = new Tag(db);
159 t.setType(Constants.typeString(dst.getType()));
160 t.setObjId(dst.toObjectId());
161 t.setTag(name);
162 t.setTagger(new PersonIdent(jcommitter, new Date(nowTick)));
163 t.setMessage("");
164 return (RevTag) rw.lookupAny(ow.writeTag(t), Constants.OBJ_TAG);
167 protected <T extends RevObject> T parse(final T t) throws Exception {
168 rw.parseBody(t);
169 return t;
172 protected void markStart(final RevCommit commit) throws Exception {
173 rw.markStart(commit);
176 protected void markUninteresting(final RevCommit commit) throws Exception {
177 rw.markUninteresting(commit);
180 protected void assertCommit(final RevCommit exp, final RevCommit act) {
181 assertSame(exp, act);