Add JUnit tests for new DirCache API
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / dircache / DirCacheBasicTest.java
blobb3097acb6ad3be7ef9a13e5bb0a99fc029db4386
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.File;
42 import org.spearce.jgit.lib.RepositoryTestCase;
44 public class DirCacheBasicTest extends RepositoryTestCase {
45 public void testReadMissing_RealIndex() throws Exception {
46 final File idx = new File(db.getDirectory(), "index");
47 assertFalse(idx.exists());
49 final DirCache dc = DirCache.read(db);
50 assertNotNull(dc);
51 assertEquals(0, dc.getEntryCount());
54 public void testReadMissing_TempIndex() throws Exception {
55 final File idx = new File(db.getDirectory(), "tmp_index");
56 assertFalse(idx.exists());
58 final DirCache dc = DirCache.read(idx);
59 assertNotNull(dc);
60 assertEquals(0, dc.getEntryCount());
63 public void testLockMissing_RealIndex() throws Exception {
64 final File idx = new File(db.getDirectory(), "index");
65 final File lck = new File(db.getDirectory(), "index.lock");
66 assertFalse(idx.exists());
67 assertFalse(lck.exists());
69 final DirCache dc = DirCache.lock(db);
70 assertNotNull(dc);
71 assertFalse(idx.exists());
72 assertTrue(lck.exists());
73 assertEquals(0, dc.getEntryCount());
75 dc.unlock();
76 assertFalse(idx.exists());
77 assertFalse(lck.exists());
80 public void testLockMissing_TempIndex() throws Exception {
81 final File idx = new File(db.getDirectory(), "tmp_index");
82 final File lck = new File(db.getDirectory(), "tmp_index.lock");
83 assertFalse(idx.exists());
84 assertFalse(lck.exists());
86 final DirCache dc = DirCache.lock(idx);
87 assertNotNull(dc);
88 assertFalse(idx.exists());
89 assertTrue(lck.exists());
90 assertEquals(0, dc.getEntryCount());
92 dc.unlock();
93 assertFalse(idx.exists());
94 assertFalse(lck.exists());
97 public void testWriteEmptyUnlock_RealIndex() throws Exception {
98 final File idx = new File(db.getDirectory(), "index");
99 final File lck = new File(db.getDirectory(), "index.lock");
100 assertFalse(idx.exists());
101 assertFalse(lck.exists());
103 final DirCache dc = DirCache.lock(db);
104 assertEquals(0, lck.length());
105 dc.write();
106 assertEquals(12 + 20, lck.length());
108 dc.unlock();
109 assertFalse(idx.exists());
110 assertFalse(lck.exists());
113 public void testWriteEmptyCommit_RealIndex() throws Exception {
114 final File idx = new File(db.getDirectory(), "index");
115 final File lck = new File(db.getDirectory(), "index.lock");
116 assertFalse(idx.exists());
117 assertFalse(lck.exists());
119 final DirCache dc = DirCache.lock(db);
120 assertEquals(0, lck.length());
121 dc.write();
122 assertEquals(12 + 20, lck.length());
124 assertTrue(dc.commit());
125 assertTrue(idx.exists());
126 assertFalse(lck.exists());
127 assertEquals(12 + 20, idx.length());
130 public void testWriteEmptyReadEmpty_RealIndex() throws Exception {
131 final File idx = new File(db.getDirectory(), "index");
132 final File lck = new File(db.getDirectory(), "index.lock");
133 assertFalse(idx.exists());
134 assertFalse(lck.exists());
136 final DirCache dc = DirCache.lock(db);
137 dc.write();
138 assertTrue(dc.commit());
139 assertTrue(idx.exists());
142 final DirCache dc = DirCache.read(db);
143 assertEquals(0, dc.getEntryCount());
147 public void testWriteEmptyLockEmpty_RealIndex() throws Exception {
148 final File idx = new File(db.getDirectory(), "index");
149 final File lck = new File(db.getDirectory(), "index.lock");
150 assertFalse(idx.exists());
151 assertFalse(lck.exists());
153 final DirCache dc = DirCache.lock(db);
154 dc.write();
155 assertTrue(dc.commit());
156 assertTrue(idx.exists());
159 final DirCache dc = DirCache.lock(db);
160 assertEquals(0, dc.getEntryCount());
161 assertTrue(idx.exists());
162 assertTrue(lck.exists());
163 dc.unlock();
167 public void testBuildThenClear() throws Exception {
168 final DirCache dc = DirCache.read(db);
170 final String[] paths = { "a.", "a.b", "a/b", "a0b" };
171 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
172 for (int i = 0; i < paths.length; i++)
173 ents[i] = new DirCacheEntry(paths[i]);
175 final DirCacheBuilder b = dc.builder();
176 for (int i = 0; i < ents.length; i++)
177 b.add(ents[i]);
178 b.finish();
180 assertEquals(paths.length, dc.getEntryCount());
181 dc.clear();
182 assertEquals(0, dc.getEntryCount());