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
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
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
;
74 public void setUp() throws Exception
{
76 ow
= new ObjectWriter(db
);
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
)
96 final DirCacheEntry e
= new DirCacheEntry(path
);
97 e
.setFileMode(FileMode
.REGULAR_FILE
);
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
)
108 return rw
.lookupTree(dc
.writeTree(ow
));
111 protected RevObject
get(final RevTree tree
, final String path
)
113 final TreeWalk tw
= new TreeWalk(db
);
114 tw
.setFilter(PathFilterGroup
.createFromStrings(Collections
118 if (tw
.isSubtree() && !path
.equals(tw
.getPathString())) {
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
)
136 return commit(1, tree
, parents
);
139 protected RevCommit
commit(final int secDelta
, final RevCommit
... parents
)
141 return commit(secDelta
, emptyTree
, parents
);
144 protected RevCommit
commit(final int secDelta
, final RevTree tree
,
145 final RevCommit
... parents
) throws Exception
{
147 final Commit c
= new Commit(db
);
149 c
.setParentIds(parents
);
150 c
.setAuthor(new PersonIdent(jauthor
, new Date(nowTick
)));
151 c
.setCommitter(new PersonIdent(jcommitter
, new Date(nowTick
)));
153 return rw
.lookupCommit(ow
.writeCommit(c
));
156 protected RevTag
tag(final String name
, final RevObject dst
)
158 final Tag t
= new Tag(db
);
159 t
.setType(Constants
.typeString(dst
.getType()));
160 t
.setObjId(dst
.toObjectId());
162 t
.setTagger(new PersonIdent(jcommitter
, new Date(nowTick
)));
164 return (RevTag
) rw
.lookupAny(ow
.writeTag(t
), Constants
.OBJ_TAG
);
167 protected <T
extends RevObject
> T
parse(final T t
) throws Exception
{
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
);