Add tests for ObjectWalk
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / revwalk / RevWalkTestCase.java
blob0a1e7b8aa0afa9a6ad0fd83d7eb4ac38d5607c31
1 /*
2 * Copyright (C) 2009, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.revwalk;
40 import java.util.Collections;
41 import java.util.Date;
43 import org.spearce.jgit.dircache.DirCache;
44 import org.spearce.jgit.dircache.DirCacheBuilder;
45 import org.spearce.jgit.dircache.DirCacheEntry;
46 import org.spearce.jgit.lib.Commit;
47 import org.spearce.jgit.lib.Constants;
48 import org.spearce.jgit.lib.FileMode;
49 import org.spearce.jgit.lib.ObjectId;
50 import org.spearce.jgit.lib.ObjectWriter;
51 import org.spearce.jgit.lib.PersonIdent;
52 import org.spearce.jgit.lib.RepositoryTestCase;
53 import org.spearce.jgit.lib.Tag;
54 import org.spearce.jgit.lib.Tree;
55 import org.spearce.jgit.treewalk.TreeWalk;
56 import org.spearce.jgit.treewalk.filter.PathFilterGroup;
58 /** Support for tests of the {@link RevWalk} class. */
59 public abstract class RevWalkTestCase extends RepositoryTestCase {
60 protected ObjectWriter ow;
62 protected RevTree emptyTree;
64 protected long nowTick;
66 protected RevWalk rw;
68 public void setUp() throws Exception {
69 super.setUp();
70 ow = new ObjectWriter(db);
71 rw = createRevWalk();
72 emptyTree = rw.parseTree(ow.writeTree(new Tree(db)));
73 nowTick = 1236977987000L;
76 protected RevWalk createRevWalk() {
77 return new RevWalk(db);
80 protected void tick(final int secDelta) {
81 nowTick += secDelta * 1000L;
84 protected RevBlob blob(final String content) throws Exception {
85 return rw.lookupBlob(ow.writeBlob(content
86 .getBytes(Constants.CHARACTER_ENCODING)));
89 protected DirCacheEntry file(final String path, final RevBlob blob)
90 throws Exception {
91 final DirCacheEntry e = new DirCacheEntry(path);
92 e.setFileMode(FileMode.REGULAR_FILE);
93 e.setObjectId(blob);
94 return e;
97 protected RevTree tree(final DirCacheEntry... entries) throws Exception {
98 final DirCache dc = DirCache.newInCore();
99 final DirCacheBuilder b = dc.builder();
100 for (final DirCacheEntry e : entries)
101 b.add(e);
102 b.finish();
103 return rw.lookupTree(dc.writeTree(ow));
106 protected RevObject get(final RevTree tree, final String path)
107 throws Exception {
108 final TreeWalk tw = new TreeWalk(db);
109 tw.setFilter(PathFilterGroup.createFromStrings(Collections
110 .singleton(path)));
111 tw.reset(tree);
112 while (tw.next()) {
113 if (tw.isSubtree() && !path.equals(tw.getPathString())) {
114 tw.enterSubtree();
115 continue;
117 final ObjectId entid = tw.getObjectId(0);
118 final FileMode entmode = tw.getFileMode(0);
119 return rw.lookupAny(entid, entmode.getObjectType());
121 fail("Can't find " + path + " in tree " + tree.name());
122 return null; // never reached.
125 protected RevCommit commit(final RevCommit... parents) throws Exception {
126 return commit(1, emptyTree, parents);
129 protected RevCommit commit(final RevTree tree, final RevCommit... parents)
130 throws Exception {
131 return commit(1, tree, parents);
134 protected RevCommit commit(final int secDelta, final RevCommit... parents)
135 throws Exception {
136 return commit(secDelta, emptyTree, parents);
139 protected RevCommit commit(final int secDelta, final RevTree tree,
140 final RevCommit... parents) throws Exception {
141 tick(secDelta);
142 final Commit c = new Commit(db);
143 c.setTreeId(tree);
144 c.setParentIds(parents);
145 c.setAuthor(new PersonIdent(jauthor, new Date(nowTick)));
146 c.setCommitter(new PersonIdent(jcommitter, new Date(nowTick)));
147 c.setMessage("");
148 return rw.lookupCommit(ow.writeCommit(c));
151 protected RevTag tag(final String name, final RevObject dst)
152 throws Exception {
153 final Tag t = new Tag(db);
154 t.setType(Constants.typeString(dst.getType()));
155 t.setObjId(dst.toObjectId());
156 t.setTag(name);
157 t.setTagger(new PersonIdent(jcommitter, new Date(nowTick)));
158 t.setMessage("");
159 return (RevTag) rw.lookupAny(ow.writeTag(t), Constants.OBJ_TAG);
162 protected <T extends RevObject> T parse(final T t) throws Exception {
163 rw.parse(t);
164 return t;
167 protected void markStart(final RevCommit commit) throws Exception {
168 rw.markStart(commit);
171 protected void markUninteresting(final RevCommit commit) throws Exception {
172 rw.markUninteresting(commit);
175 protected void assertCommit(final RevCommit exp, final RevCommit act) {
176 assertSame(exp, act);