2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
4 * and other copyright owners as documented in the project's IP log.
6 * This program and the accompanying materials are made available
7 * under the terms of the Eclipse Distribution License v1.0 which
8 * accompanies this distribution, is reproduced below, and is
9 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
25 * - Neither the name of the Eclipse Foundation, Inc. nor the
26 * names of its contributors may be used to endorse or promote
27 * products derived from this software without specific prior
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 package org
.eclipse
.jgit
.lib
;
48 import java
.io
.IOException
;
49 import java
.util
.ArrayList
;
51 import org
.eclipse
.jgit
.lib
.GitIndex
.Entry
;
53 public class IndexTreeWalkerTest
extends RepositoryTestCase
{
54 private ArrayList
<String
> treeOnlyEntriesVisited
= new ArrayList
<String
>();
55 private ArrayList
<String
> bothVisited
= new ArrayList
<String
>();
56 private ArrayList
<String
> indexOnlyEntriesVisited
= new ArrayList
<String
>();
58 private class TestIndexTreeVisitor
extends AbstractIndexTreeVisitor
{
59 public void visitEntry(TreeEntry treeEntry
, Entry indexEntry
, File file
) {
60 if (treeEntry
== null)
61 indexOnlyEntriesVisited
.add(indexEntry
.getName());
62 else if (indexEntry
== null)
63 treeOnlyEntriesVisited
.add(treeEntry
.getFullName());
64 else bothVisited
.add(indexEntry
.getName());
69 * Need to think about what I really need to be able to do....
71 * 1) Visit all entries in index and tree
72 * 2) Get all directories that exist in the index, but not in the tree
73 * -- I'm pretty sure that I don't need to do the other way around
77 public void testTreeOnlyOneLevel() throws IOException
{
78 GitIndex index
= new GitIndex(db
);
79 Tree tree
= new Tree(db
);
83 new IndexTreeWalker(index
, tree
, trash
, new TestIndexTreeVisitor()).walk();
85 assertTrue(treeOnlyEntriesVisited
.get(0).equals("bar"));
86 assertTrue(treeOnlyEntriesVisited
.get(1).equals("foo"));
89 public void testIndexOnlyOneLevel() throws IOException
{
90 GitIndex index
= new GitIndex(db
);
91 Tree tree
= new Tree(db
);
93 index
.add(trash
, writeTrashFile("foo", "foo"));
94 index
.add(trash
, writeTrashFile("bar", "bar"));
95 new IndexTreeWalker(index
, tree
, trash
, new TestIndexTreeVisitor()).walk();
97 assertTrue(indexOnlyEntriesVisited
.get(0).equals("bar"));
98 assertTrue(indexOnlyEntriesVisited
.get(1).equals("foo"));
101 public void testBoth() throws IOException
{
102 GitIndex index
= new GitIndex(db
);
103 Tree tree
= new Tree(db
);
105 index
.add(trash
, writeTrashFile("a", "a"));
107 index
.add(trash
, writeTrashFile("c", "c"));
110 new IndexTreeWalker(index
, tree
, trash
, new TestIndexTreeVisitor()).walk();
111 assertTrue(indexOnlyEntriesVisited
.contains("a"));
112 assertTrue(treeOnlyEntriesVisited
.contains("b/b"));
113 assertTrue(bothVisited
.contains("c"));
117 public void testIndexOnlySubDirs() throws IOException
{
118 GitIndex index
= new GitIndex(db
);
119 Tree tree
= new Tree(db
);
121 index
.add(trash
, writeTrashFile("foo/bar/baz", "foobar"));
122 index
.add(trash
, writeTrashFile("asdf", "asdf"));
123 new IndexTreeWalker(index
, tree
, trash
, new TestIndexTreeVisitor()).walk();
125 assertEquals("asdf", indexOnlyEntriesVisited
.get(0));
126 assertEquals("foo/bar/baz", indexOnlyEntriesVisited
.get(1));
129 public void testLeavingTree() throws IOException
{
130 GitIndex index
= new GitIndex(db
);
131 index
.add(trash
, writeTrashFile("foo/bar", "foo/bar"));
132 index
.add(trash
, writeTrashFile("foobar", "foobar"));
134 new IndexTreeWalker(index
, db
.mapTree(index
.writeTree()), trash
, new AbstractIndexTreeVisitor() {
136 public void visitEntry(TreeEntry entry
, Entry indexEntry
, File f
) {
137 if (entry
== null || indexEntry
== null)
142 public void finishVisitTree(Tree tree
, int i
, String curDir
)
144 if (tree
.memberCount() == 0)