Browse Source

Add blob-data to utility method indexState()

indexState() encodes the complete state of the index
into one readable String. This helps to write tests
against the index. indexState() is enhanced to optionally
also contain the content of the files in the index.

Change-Id: Ie988f93768d864f4cbd55809a786bd5759fc24a5
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
stable-0.9
Christian Halstrick 14 years ago committed by Shawn O. Pearce
parent
commit
07a1d17858
  1. 86
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java
  2. 12
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java
  3. 36
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java

86
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java

@ -91,8 +91,8 @@ public class AddCommandTest extends RepositoryTestCase {
git.add().addFilepattern("a.txt").call(); git.add().addFilepattern("a.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", "[a.txt, mode:100644, content:content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException { public void testAddExistingSingleFileInSubDir() throws IOException, NoFilepatternException {
@ -108,8 +108,8 @@ public class AddCommandTest extends RepositoryTestCase {
git.add().addFilepattern("sub/a.txt").call(); git.add().addFilepattern("sub/a.txt").call();
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", "[sub/a.txt, mode:100644, content:content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException { public void testAddExistingSingleFileTwice() throws IOException, NoFilepatternException {
@ -131,8 +131,8 @@ public class AddCommandTest extends RepositoryTestCase {
dc = git.add().addFilepattern("a.txt").call(); dc = git.add().addFilepattern("a.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:4f41554f6e0045ef53848fc0c3f33b6a9abc24a9]", "[a.txt, mode:100644, content:other content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddExistingSingleFileTwiceWithCommit() throws Exception { public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
@ -156,8 +156,8 @@ public class AddCommandTest extends RepositoryTestCase {
dc = git.add().addFilepattern("a.txt").call(); dc = git.add().addFilepattern("a.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:4f41554f6e0045ef53848fc0c3f33b6a9abc24a9]", "[a.txt, mode:100644, content:other content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddRemovedFile() throws Exception { public void testAddRemovedFile() throws Exception {
@ -177,8 +177,8 @@ public class AddCommandTest extends RepositoryTestCase {
dc = git.add().addFilepattern("a.txt").call(); dc = git.add().addFilepattern("a.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", "[a.txt, mode:100644, content:content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddRemovedCommittedFile() throws Exception { public void testAddRemovedCommittedFile() throws Exception {
@ -200,8 +200,8 @@ public class AddCommandTest extends RepositoryTestCase {
dc = git.add().addFilepattern("a.txt").call(); dc = git.add().addFilepattern("a.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", "[a.txt, mode:100644, content:content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddWithConflicts() throws Exception { public void testAddWithConflicts() throws Exception {
@ -240,11 +240,11 @@ public class AddCommandTest extends RepositoryTestCase {
builder.commit(); builder.commit();
assertEquals( assertEquals(
"[a.txt, mode:100644, stage:1, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + "[a.txt, mode:100644, stage:1, content:content]" +
"[a.txt, mode:100644, stage:2, sha1:b9f89ff733bdaf49e02711535867bb821f9db55e]" + "[a.txt, mode:100644, stage:2, content:our content]" +
"[a.txt, mode:100644, stage:3, sha1:4f41554f6e0045ef53848fc0c3f33b6a9abc24a9]" + "[a.txt, mode:100644, stage:3, content:other content]" +
"[b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
// now the test begins // now the test begins
@ -252,9 +252,9 @@ public class AddCommandTest extends RepositoryTestCase {
dc = git.add().addFilepattern("a.txt").call(); dc = git.add().addFilepattern("a.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:b9f89ff733bdaf49e02711535867bb821f9db55e]" + "[a.txt, mode:100644, content:our content]" +
"[b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddTwoFiles() throws Exception { public void testAddTwoFiles() throws Exception {
@ -273,9 +273,9 @@ public class AddCommandTest extends RepositoryTestCase {
Git git = new Git(db); Git git = new Git(db);
git.add().addFilepattern("a.txt").addFilepattern("b.txt").call(); git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
assertEquals( assertEquals(
"[a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + "[a.txt, mode:100644, content:content]" +
"[b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddFolder() throws Exception { public void testAddFolder() throws Exception {
@ -295,9 +295,9 @@ public class AddCommandTest extends RepositoryTestCase {
Git git = new Git(db); Git git = new Git(db);
git.add().addFilepattern("sub").call(); git.add().addFilepattern("sub").call();
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + "[sub/a.txt, mode:100644, content:content]" +
"[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[sub/b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddIgnoredFile() throws Exception { public void testAddIgnoredFile() throws Exception {
@ -324,8 +324,8 @@ public class AddCommandTest extends RepositoryTestCase {
git.add().addFilepattern("sub").call(); git.add().addFilepattern("sub").call();
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]", "[sub/a.txt, mode:100644, content:content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
public void testAddWholeRepo() throws Exception { public void testAddWholeRepo() throws Exception {
@ -345,9 +345,9 @@ public class AddCommandTest extends RepositoryTestCase {
Git git = new Git(db); Git git = new Git(db);
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + "[sub/a.txt, mode:100644, content:content]" +
"[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[sub/b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
// the same three cases as in testAddWithParameterUpdate // the same three cases as in testAddWithParameterUpdate
@ -372,9 +372,9 @@ public class AddCommandTest extends RepositoryTestCase {
git.add().addFilepattern("sub").call(); git.add().addFilepattern("sub").call();
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + "[sub/a.txt, mode:100644, content:content]" +
"[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[sub/b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
git.commit().setMessage("commit").call(); git.commit().setMessage("commit").call();
@ -398,10 +398,10 @@ public class AddCommandTest extends RepositoryTestCase {
// deletion of sub/b.txt is not staged // deletion of sub/b.txt is not staged
// sub/c.txt is staged // sub/c.txt is staged
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:268af4e306cfcf6e79edd50fed9c553d211f68e3]" + "[sub/a.txt, mode:100644, content:modified content]" +
"[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]" + "[sub/b.txt, mode:100644, content:content b]" +
"[sub/c.txt, mode:100644, sha1:fa08654474ae2ddc4f61ee3a43d017ba65a439c3]", "[sub/c.txt, mode:100644, content:content c]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
// file a exists in workdir and in index -> added // file a exists in workdir and in index -> added
@ -425,9 +425,9 @@ public class AddCommandTest extends RepositoryTestCase {
git.add().addFilepattern("sub").call(); git.add().addFilepattern("sub").call();
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:6b584e8ece562ebffc15d38808cd6b98fc3d97ea]" + "[sub/a.txt, mode:100644, content:content]" +
"[sub/b.txt, mode:100644, sha1:50e9cdb03f9719261dd39d7f2920b906db3711a3]", "[sub/b.txt, mode:100644, content:content b]",
indexState(CONTENT_ID)); indexState(CONTENT));
git.commit().setMessage("commit").call(); git.commit().setMessage("commit").call();
@ -451,8 +451,8 @@ public class AddCommandTest extends RepositoryTestCase {
git.add().addFilepattern("sub").setUpdate(true).call(); git.add().addFilepattern("sub").setUpdate(true).call();
// change in sub/a.txt is staged // change in sub/a.txt is staged
assertEquals( assertEquals(
"[sub/a.txt, mode:100644, sha1:268af4e306cfcf6e79edd50fed9c553d211f68e3]", "[sub/a.txt, mode:100644, content:modified content]",
indexState(CONTENT_ID)); indexState(CONTENT));
} }
private DirCacheEntry addEntryToBuilder(String path, File file, private DirCacheEntry addEntryToBuilder(String path, File file,

12
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RacyGitTests.java

@ -133,9 +133,9 @@ public class RacyGitTests extends RepositoryTestCase {
resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes)); resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
assertEquals( assertEquals(
"[a, mode:100644, time:t0, length:1, sha1:2e65efe2a145dda7ee51d1741299f848e5bf752e]" + "[a, mode:100644, time:t0, length:1, content:a]" +
"[b, mode:100644, time:t0, length:1, sha1:63d8dbd40c23542e740659a7168a0ce3138ea748]", "[b, mode:100644, time:t0, length:1, content:b]",
indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT_ID)); indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));
// Remember the last modTime of index file. All modifications times of // Remember the last modTime of index file. All modifications times of
// further modification are translated to this value so it looks that // further modification are translated to this value so it looks that
@ -152,9 +152,9 @@ public class RacyGitTests extends RepositoryTestCase {
db.readDirCache(); db.readDirCache();
// although racily clean a should not be reported as being dirty // although racily clean a should not be reported as being dirty
assertEquals( assertEquals(
"[a, mode:100644, time:t1, smudged, length:0]" + "[a, mode:100644, time:t1, smudged, length:0, content:a2]" +
"[b, mode:100644, time:t0, length:1]", "[b, mode:100644, time:t0, length:1, content:b]",
indexState(SMUDGE|MOD_TIME|LENGTH)); indexState(SMUDGE|MOD_TIME|LENGTH|CONTENT));
} }
private File addToWorkDir(String path, String content) throws IOException { private File addToWorkDir(String path, String content) throws IOException {

36
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java

@ -59,11 +59,9 @@ import java.util.TreeSet;
import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheBuilder;
import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.storage.file.FileRepository; import org.eclipse.jgit.storage.file.FileRepository;
import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
/** /**
* Base class for most JGit unit tests. * Base class for most JGit unit tests.
@ -132,6 +130,8 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
public static final int CONTENT_ID = 8; public static final int CONTENT_ID = 8;
public static final int CONTENT = 16;
/** /**
* Represent the state of the index in one String. This representation is * Represent the state of the index in one String. This representation is
* useful when writing tests which do assertions on the state of the index. * useful when writing tests which do assertions on the state of the index.
@ -142,13 +142,15 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
* The format of the returned string is described with this BNF: * The format of the returned string is described with this BNF:
* *
* <pre> * <pre>
* result = ( "[" path mode stage? time? smudge? length? sha1? "]" )* . * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
* mode = ", mode:" number . * mode = ", mode:" number .
* stage = ", stage:" number . * stage = ", stage:" number .
* time = ", time:t" timestamp-index . * time = ", time:t" timestamp-index .
* smudge = "" | ", smudged" . * smudge = "" | ", smudged" .
* length = ", length:" number . * length = ", length:" number .
* sha1 = ", sha1:" hex-sha1 . * sha1 = ", sha1:" hex-sha1 .
* content = ", content:" blob-data .
* </pre>
* *
* 'stage' is only presented when the stage is different from 0. All * 'stage' is only presented when the stage is different from 0. All
* reported time stamps are mapped to strings like "t0", "t1", ... "tn". The * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
@ -158,7 +160,7 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
* *
* @param includedOptions * @param includedOptions
* a bitmask constructed out of the constants {@link #MOD_TIME}, * a bitmask constructed out of the constants {@link #MOD_TIME},
* {@link #SMUDGE}, {@link #LENGTH} and {@link #CONTENT_ID} * {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and {@link #CONTENT}
* controlling which info is present in the resulting string. * controlling which info is present in the resulting string.
* @return a string encoding the index state * @return a string encoding the index state
* @throws IllegalStateException * @throws IllegalStateException
@ -178,29 +180,29 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
} }
// iterate again, now produce the result string // iterate again, now produce the result string
NameConflictTreeWalk tw = new NameConflictTreeWalk(db); for (int i=0; i<dc.getEntryCount(); ++i) {
tw.setRecursive(true); DirCacheEntry entry = dc.getEntry(i);
tw.reset(); sb.append("["+entry.getPathString()+", mode:" + entry.getFileMode());
tw.addTree(new DirCacheIterator(dc)); int stage = entry.getStage();
while (tw.next()) {
DirCacheIterator dcIt = tw.getTree(0, DirCacheIterator.class);
sb.append("["+tw.getPathString()+", mode:" + dcIt.getEntryFileMode());
int stage = dcIt.getDirCacheEntry().getStage();
if (stage != 0) if (stage != 0)
sb.append(", stage:" + stage); sb.append(", stage:" + stage);
if (0 != (includedOptions & MOD_TIME)) { if (0 != (includedOptions & MOD_TIME)) {
sb.append(", time:t"+ sb.append(", time:t"+
timeStamps.headSet(Long.valueOf(dcIt.getDirCacheEntry().getLastModified())).size()); timeStamps.headSet(Long.valueOf(entry.getLastModified())).size());
} }
if (0 != (includedOptions & SMUDGE)) if (0 != (includedOptions & SMUDGE))
if (dcIt.getDirCacheEntry().isSmudged()) if (entry.isSmudged())
sb.append(", smudged"); sb.append(", smudged");
if (0 != (includedOptions & LENGTH)) if (0 != (includedOptions & LENGTH))
sb.append(", length:" sb.append(", length:"
+ Integer.toString(dcIt.getDirCacheEntry().getLength())); + Integer.toString(entry.getLength()));
if (0 != (includedOptions & CONTENT_ID)) if (0 != (includedOptions & CONTENT_ID))
sb.append(", sha1:" + ObjectId.toString(dcIt sb.append(", sha1:" + ObjectId.toString(entry.getObjectId()));
.getEntryObjectId())); if (0 != (includedOptions & CONTENT)) {
sb.append(", content:"
+ new String(db.open(entry.getObjectId(),
Constants.OBJ_BLOB).getCachedBytes(), "UTF-8"));
}
sb.append("]"); sb.append("]");
} }
return sb.toString(); return sb.toString();

Loading…
Cancel
Save