Move C git compatability test vectors to tst directory
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / dircache / DirCacheCGitCompatabilityTest.java
blob43b23f67c2dbff2c017ee76e1d3d95e9c68d7949
1 /*
2 * Copyright (C) 2008, 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.dircache;
40 import java.io.BufferedReader;
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.InputStreamReader;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.LinkedHashMap;
47 import java.util.Map;
49 import org.spearce.jgit.lib.FileMode;
50 import org.spearce.jgit.lib.ObjectId;
51 import org.spearce.jgit.lib.RepositoryTestCase;
52 import org.spearce.jgit.treewalk.TreeWalk;
54 public class DirCacheCGitCompatabilityTest extends RepositoryTestCase {
55 private final File index = pathOf("gitgit.index");
57 public void testReadIndex_LsFiles() throws Exception {
58 final Map<String, CGitIndexRecord> ls = readLsFiles();
59 final DirCache dc = new DirCache(index);
60 assertEquals(0, dc.getEntryCount());
61 dc.read();
62 assertEquals(ls.size(), dc.getEntryCount());
64 final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
65 for (int i = 0; rItr.hasNext(); i++)
66 assertEqual(rItr.next(), dc.getEntry(i));
70 public void testTreeWalk_LsFiles() throws Exception {
71 final Map<String, CGitIndexRecord> ls = readLsFiles();
72 final DirCache dc = new DirCache(index);
73 assertEquals(0, dc.getEntryCount());
74 dc.read();
75 assertEquals(ls.size(), dc.getEntryCount());
77 final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
78 final TreeWalk tw = new TreeWalk(db);
79 tw.reset();
80 tw.setRecursive(true);
81 tw.addTree(new DirCacheIterator(dc));
82 while (rItr.hasNext()) {
83 final DirCacheIterator dcItr;
85 assertTrue(tw.next());
86 dcItr = tw.getTree(0, DirCacheIterator.class);
87 assertNotNull(dcItr);
89 assertEqual(rItr.next(), dcItr.getDirCacheEntry());
94 private static void assertEqual(final CGitIndexRecord c,
95 final DirCacheEntry j) {
96 assertNotNull(c);
97 assertNotNull(j);
99 assertEquals(c.path, j.getPathString());
100 assertEquals(c.id, j.getObjectId());
101 assertEquals(c.mode, j.getRawMode());
102 assertEquals(c.stage, j.getStage());
105 public void testReadIndex_DirCacheTree() throws Exception {
106 final Map<String, CGitIndexRecord> cList = readLsFiles();
107 final Map<String, CGitLsTreeRecord> cTree = readLsTree();
108 final DirCache dc = new DirCache(index);
109 assertEquals(0, dc.getEntryCount());
110 dc.read();
111 assertEquals(cList.size(), dc.getEntryCount());
113 final DirCacheTree jTree = dc.getCacheTree(false);
114 assertNotNull(jTree);
115 assertEquals("", jTree.getNameString());
116 assertEquals("", jTree.getPathString());
117 assertTrue(jTree.isValid());
118 assertEquals(ObjectId
119 .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree
120 .getObjectId());
121 assertEquals(cList.size(), jTree.getEntrySpan());
123 final ArrayList<CGitLsTreeRecord> subtrees = new ArrayList<CGitLsTreeRecord>();
124 for (final CGitLsTreeRecord r : cTree.values()) {
125 if (FileMode.TREE.equals(r.mode))
126 subtrees.add(r);
128 assertEquals(subtrees.size(), jTree.getChildCount());
130 for (int i = 0; i < jTree.getChildCount(); i++) {
131 final DirCacheTree sj = jTree.getChild(i);
132 final CGitLsTreeRecord sc = subtrees.get(i);
133 assertEquals(sc.path, sj.getNameString());
134 assertEquals(sc.path + "/", sj.getPathString());
135 assertTrue(sj.isValid());
136 assertEquals(sc.id, sj.getObjectId());
140 private File pathOf(final String name) {
141 return new File("tst", name);
144 private Map<String, CGitIndexRecord> readLsFiles() throws Exception {
145 final LinkedHashMap<String, CGitIndexRecord> r = new LinkedHashMap<String, CGitIndexRecord>();
146 final BufferedReader br = new BufferedReader(new InputStreamReader(
147 new FileInputStream(pathOf("gitgit.lsfiles")), "UTF-8"));
148 String line;
149 while ((line = br.readLine()) != null) {
150 final CGitIndexRecord cr = new CGitIndexRecord(line);
151 r.put(cr.path, cr);
153 return r;
156 private Map<String, CGitLsTreeRecord> readLsTree() throws Exception {
157 final LinkedHashMap<String, CGitLsTreeRecord> r = new LinkedHashMap<String, CGitLsTreeRecord>();
158 final BufferedReader br = new BufferedReader(new InputStreamReader(
159 new FileInputStream(pathOf("gitgit.lstree")), "UTF-8"));
160 String line;
161 while ((line = br.readLine()) != null) {
162 final CGitLsTreeRecord cr = new CGitLsTreeRecord(line);
163 r.put(cr.path, cr);
165 return r;
168 private static class CGitIndexRecord {
169 final int mode;
171 final ObjectId id;
173 final int stage;
175 final String path;
177 CGitIndexRecord(final String line) {
178 final int tab = line.indexOf('\t');
179 final int sp1 = line.indexOf(' ');
180 final int sp2 = line.indexOf(' ', sp1 + 1);
181 mode = Integer.parseInt(line.substring(0, sp1), 8);
182 id = ObjectId.fromString(line.substring(sp1 + 1, sp2));
183 stage = Integer.parseInt(line.substring(sp2 + 1, tab));
184 path = line.substring(tab + 1);
188 private static class CGitLsTreeRecord {
189 final int mode;
191 final ObjectId id;
193 final String path;
195 CGitLsTreeRecord(final String line) {
196 final int tab = line.indexOf('\t');
197 final int sp1 = line.indexOf(' ');
198 final int sp2 = line.indexOf(' ', sp1 + 1);
199 mode = Integer.parseInt(line.substring(0, sp1), 8);
200 id = ObjectId.fromString(line.substring(sp2 + 1, tab));
201 path = line.substring(tab + 1);