summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorSasa Zivkov2011-01-03 10:03:49 (EST)
committer Sasa Zivkov2011-01-05 02:24:13 (EST)
commit7cd812940d25a78f71467ede0b2637211b2a12a1 (patch)
tree7bf2f1f7aea387ab05f076fd46dd0dc3e590d2b5
parentb7f887f1204fc14e2ef698d6d4a24d13fa6de5e6 (diff)
downloadjgit-7cd812940d25a78f71467ede0b2637211b2a12a1.zip
jgit-7cd812940d25a78f71467ede0b2637211b2a12a1.tar.gz
jgit-7cd812940d25a78f71467ede0b2637211b2a12a1.tar.bz2
NoteMap implements Iterable<Note>refs/changes/07/2207/3
We will need to iterate over all notes of a NoteMap, at least this will be needed for testing purposes. This change also implied making the Note class public. Change-Id: I9b0639f9843f457ee9de43504b2499a673cd0e77 Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java87
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java5
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java16
3 files changed, 105 insertions, 3 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java
index 6c65b53..94fdc69 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/notes/NoteMapTest.java
@@ -52,6 +52,7 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
+import java.util.Iterator;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.CommitBuilder;
@@ -444,6 +445,83 @@ public class NoteMapTest extends RepositoryTestCase {
assertEquals("empty tree", empty, n.getTree());
}
+ public void testIteratorEmptyMap() {
+ Iterator<Note> it = NoteMap.newEmptyMap().iterator();
+ assertFalse(it.hasNext());
+ }
+
+ public void testIteratorFlatTree() throws Exception {
+ RevBlob a = tr.blob("a");
+ RevBlob b = tr.blob("b");
+ RevBlob data1 = tr.blob("data1");
+ RevBlob data2 = tr.blob("data2");
+ RevBlob nonNote = tr.blob("non note");
+
+ RevCommit r = tr.commit() //
+ .add(a.name(), data1) //
+ .add(b.name(), data2) //
+ .add("nonNote", nonNote) //
+ .create();
+ tr.parseBody(r);
+
+ Iterator it = NoteMap.read(reader, r).iterator();
+ assertEquals(2, count(it));
+ }
+
+ public void testIteratorFanoutTree2_38() throws Exception {
+ RevBlob a = tr.blob("a");
+ RevBlob b = tr.blob("b");
+ RevBlob data1 = tr.blob("data1");
+ RevBlob data2 = tr.blob("data2");
+ RevBlob nonNote = tr.blob("non note");
+
+ RevCommit r = tr.commit() //
+ .add(fanout(2, a.name()), data1) //
+ .add(fanout(2, b.name()), data2) //
+ .add("nonNote", nonNote) //
+ .create();
+ tr.parseBody(r);
+
+ Iterator it = NoteMap.read(reader, r).iterator();
+ assertEquals(2, count(it));
+ }
+
+ public void testIteratorFanoutTree2_2_36() throws Exception {
+ RevBlob a = tr.blob("a");
+ RevBlob b = tr.blob("b");
+ RevBlob data1 = tr.blob("data1");
+ RevBlob data2 = tr.blob("data2");
+ RevBlob nonNote = tr.blob("non note");
+
+ RevCommit r = tr.commit() //
+ .add(fanout(4, a.name()), data1) //
+ .add(fanout(4, b.name()), data2) //
+ .add("nonNote", nonNote) //
+ .create();
+ tr.parseBody(r);
+
+ Iterator it = NoteMap.read(reader, r).iterator();
+ assertEquals(2, count(it));
+ }
+
+ public void testIteratorFullyFannedOut() throws Exception {
+ RevBlob a = tr.blob("a");
+ RevBlob b = tr.blob("b");
+ RevBlob data1 = tr.blob("data1");
+ RevBlob data2 = tr.blob("data2");
+ RevBlob nonNote = tr.blob("non note");
+
+ RevCommit r = tr.commit() //
+ .add(fanout(38, a.name()), data1) //
+ .add(fanout(38, b.name()), data2) //
+ .add("nonNote", nonNote) //
+ .create();
+ tr.parseBody(r);
+
+ Iterator it = NoteMap.read(reader, r).iterator();
+ assertEquals(2, count(it));
+ }
+
private RevCommit commitNoteMap(NoteMap map) throws IOException {
tr.tick(600);
@@ -469,4 +547,13 @@ public class NoteMapTest extends RepositoryTestCase {
}
return r.toString();
}
+
+ private static int count(Iterator it) {
+ int c = 0;
+ while (it.hasNext()) {
+ c++;
+ it.next();
+ }
+ return c;
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java
index d365f9b..00b3213 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/Note.java
@@ -47,7 +47,7 @@ import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId;
/** In-memory representation of a single note attached to one object. */
-class Note extends ObjectId {
+public class Note extends ObjectId {
private ObjectId data;
/**
@@ -63,7 +63,8 @@ class Note extends ObjectId {
data = noteData;
}
- ObjectId getData() {
+ /** @return the note content */
+ public ObjectId getData() {
return data;
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java
index 6a7b5cf..abde6db 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMap.java
@@ -44,6 +44,7 @@
package org.eclipse.jgit.notes;
import java.io.IOException;
+import java.util.Iterator;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
@@ -52,6 +53,7 @@ import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectReader;
@@ -66,7 +68,7 @@ import org.eclipse.jgit.revwalk.RevTree;
* is not released by this class. The caller should arrange for releasing the
* shared {@code ObjectReader} at the proper times.
*/
-public class NoteMap {
+public class NoteMap implements Iterable<Note> {
/**
* Construct a new empty note map.
*
@@ -166,6 +168,18 @@ public class NoteMap {
}
/**
+ * @return an iterator that iterates over notes of this NoteMap. Non note
+ * entries are ignored by this iterator.
+ */
+ public Iterator<Note> iterator() {
+ try {
+ return root.iterator(new MutableObjectId(), reader);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
* Lookup a note for a specific ObjectId.
*
* @param id