123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- /*
- * Copyright (C) 2010, Google Inc.
- * and other copyright owners as documented in the project's IP log.
- *
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
- package org.eclipse.jgit.notes;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertNotSame;
- import static org.junit.Assert.assertNull;
- 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.RepositoryTestCase;
- import org.eclipse.jgit.junit.TestRepository;
- import org.eclipse.jgit.lib.CommitBuilder;
- import org.eclipse.jgit.lib.Constants;
- import org.eclipse.jgit.lib.MutableObjectId;
- import org.eclipse.jgit.lib.ObjectInserter;
- import org.eclipse.jgit.lib.ObjectReader;
- import org.eclipse.jgit.lib.Repository;
- import org.eclipse.jgit.revwalk.RevBlob;
- import org.eclipse.jgit.revwalk.RevCommit;
- import org.eclipse.jgit.revwalk.RevTree;
- import org.eclipse.jgit.treewalk.TreeWalk;
- import org.eclipse.jgit.util.RawParseUtils;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
-
- public class NoteMapTest extends RepositoryTestCase {
- private TestRepository<Repository> tr;
-
- private ObjectReader reader;
-
- private ObjectInserter inserter;
-
- @Override
- @Before
- public void setUp() throws Exception {
- super.setUp();
-
- tr = new TestRepository<Repository>(db);
- reader = db.newObjectReader();
- inserter = db.newObjectInserter();
- }
-
- @Override
- @After
- public void tearDown() throws Exception {
- reader.close();
- inserter.close();
- super.tearDown();
- }
-
- @Test
- public void testReadFlatTwoNotes() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(a.name(), data1) //
- .add(b.name(), data2) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- assertNotNull("have map", map);
-
- assertTrue("has note for a", map.contains(a));
- assertTrue("has note for b", map.contains(b));
- assertEquals(data1, map.get(a));
- assertEquals(data2, map.get(b));
-
- assertFalse("no note for data1", map.contains(data1));
- assertNull("no note for data1", map.get(data1));
- }
-
- @Test
- public void testReadFanout2_38() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(fanout(2, a.name()), data1) //
- .add(fanout(2, b.name()), data2) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- assertNotNull("have map", map);
-
- assertTrue("has note for a", map.contains(a));
- assertTrue("has note for b", map.contains(b));
- assertEquals(data1, map.get(a));
- assertEquals(data2, map.get(b));
-
- assertFalse("no note for data1", map.contains(data1));
- assertNull("no note for data1", map.get(data1));
- }
-
- @Test
- public void testReadFanout2_2_36() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(fanout(4, a.name()), data1) //
- .add(fanout(4, b.name()), data2) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- assertNotNull("have map", map);
-
- assertTrue("has note for a", map.contains(a));
- assertTrue("has note for b", map.contains(b));
- assertEquals(data1, map.get(a));
- assertEquals(data2, map.get(b));
-
- assertFalse("no note for data1", map.contains(data1));
- assertNull("no note for data1", map.get(data1));
- }
-
- @Test
- public void testReadFullyFannedOut() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(fanout(38, a.name()), data1) //
- .add(fanout(38, b.name()), data2) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- assertNotNull("have map", map);
-
- assertTrue("has note for a", map.contains(a));
- assertTrue("has note for b", map.contains(b));
- assertEquals(data1, map.get(a));
- assertEquals(data2, map.get(b));
-
- assertFalse("no note for data1", map.contains(data1));
- assertNull("no note for data1", map.get(data1));
- }
-
- @Test
- public void testGetCachedBytes() throws Exception {
- final String exp = "this is test data";
- RevBlob a = tr.blob("a");
- RevBlob data = tr.blob(exp);
-
- RevCommit r = tr.commit() //
- .add(a.name(), data) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- byte[] act = map.getCachedBytes(a, exp.length() * 4);
- assertNotNull("has data for a", act);
- assertEquals(exp, RawParseUtils.decode(act));
- }
-
- @Test
- public void testWriteUnchangedFlat() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(a.name(), data1) //
- .add(b.name(), data2) //
- .add(".gitignore", "") //
- .add("zoo-animals.txt", "") //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- assertTrue("has note for a", map.contains(a));
- assertTrue("has note for b", map.contains(b));
-
- RevCommit n = commitNoteMap(map);
- assertNotSame("is new commit", r, n);
- assertSame("same tree", r.getTree(), n.getTree());
- }
-
- @Test
- public void testWriteUnchangedFanout2_38() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(fanout(2, a.name()), data1) //
- .add(fanout(2, b.name()), data2) //
- .add(".gitignore", "") //
- .add("zoo-animals.txt", "") //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- assertTrue("has note for a", map.contains(a));
- assertTrue("has note for b", map.contains(b));
-
- // This is a non-lazy map, so we'll be looking at the leaf buckets.
- RevCommit n = commitNoteMap(map);
- assertNotSame("is new commit", r, n);
- assertSame("same tree", r.getTree(), n.getTree());
-
- // Use a lazy-map for the next round of the same test.
- map = NoteMap.read(reader, r);
- n = commitNoteMap(map);
- assertNotSame("is new commit", r, n);
- assertSame("same tree", r.getTree(), n.getTree());
- }
-
- @Test
- public void testCreateFromEmpty() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- NoteMap map = NoteMap.newEmptyMap();
- assertFalse("no a", map.contains(a));
- assertFalse("no b", map.contains(b));
-
- map.set(a, data1);
- map.set(b, data2);
-
- assertEquals(data1, map.get(a));
- assertEquals(data2, map.get(b));
-
- map.remove(a);
- map.remove(b);
-
- assertFalse("no a", map.contains(a));
- assertFalse("no b", map.contains(b));
-
- map.set(a, "data1", inserter);
- assertEquals(data1, map.get(a));
-
- map.set(a, null, inserter);
- assertFalse("no a", map.contains(a));
- }
-
- @Test
- public void testEditFlat() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(a.name(), data1) //
- .add(b.name(), data2) //
- .add(".gitignore", "") //
- .add("zoo-animals.txt", b) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- map.set(a, data2);
- map.set(b, null);
- map.set(data1, b);
- map.set(data2, null);
-
- assertEquals(data2, map.get(a));
- assertEquals(b, map.get(data1));
- assertFalse("no b", map.contains(b));
- assertFalse("no data2", map.contains(data2));
-
- MutableObjectId id = new MutableObjectId();
- for (int p = 42; p > 0; p--) {
- id.setByte(1, p);
- map.set(id, data1);
- }
-
- for (int p = 42; p > 0; p--) {
- id.setByte(1, p);
- assertTrue("contains " + id, map.contains(id));
- }
-
- RevCommit n = commitNoteMap(map);
- map = NoteMap.read(reader, n);
- assertEquals(data2, map.get(a));
- assertEquals(b, map.get(data1));
- assertFalse("no b", map.contains(b));
- assertFalse("no data2", map.contains(data2));
- assertEquals(b, TreeWalk
- .forPath(reader, "zoo-animals.txt", n.getTree()).getObjectId(0));
- }
-
- @Test
- public void testEditFanout2_38() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob b = tr.blob("b");
- RevBlob data1 = tr.blob("data1");
- RevBlob data2 = tr.blob("data2");
-
- RevCommit r = tr.commit() //
- .add(fanout(2, a.name()), data1) //
- .add(fanout(2, b.name()), data2) //
- .add(".gitignore", "") //
- .add("zoo-animals.txt", b) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- map.set(a, data2);
- map.set(b, null);
- map.set(data1, b);
- map.set(data2, null);
-
- assertEquals(data2, map.get(a));
- assertEquals(b, map.get(data1));
- assertFalse("no b", map.contains(b));
- assertFalse("no data2", map.contains(data2));
- RevCommit n = commitNoteMap(map);
-
- map.set(a, null);
- map.set(data1, null);
- assertFalse("no a", map.contains(a));
- assertFalse("no data1", map.contains(data1));
-
- map = NoteMap.read(reader, n);
- assertEquals(data2, map.get(a));
- assertEquals(b, map.get(data1));
- assertFalse("no b", map.contains(b));
- assertFalse("no data2", map.contains(data2));
- assertEquals(b, TreeWalk
- .forPath(reader, "zoo-animals.txt", n.getTree()).getObjectId(0));
- }
-
- @Test
- public void testLeafSplitsWhenFull() throws Exception {
- RevBlob data1 = tr.blob("data1");
- MutableObjectId idBuf = new MutableObjectId();
-
- RevCommit r = tr.commit() //
- .add(data1.name(), data1) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- for (int i = 0; i < 254; i++) {
- idBuf.setByte(Constants.OBJECT_ID_LENGTH - 1, i);
- map.set(idBuf, data1);
- }
-
- RevCommit n = commitNoteMap(map);
- try (TreeWalk tw = new TreeWalk(reader)) {
- tw.reset(n.getTree());
- while (tw.next()) {
- assertFalse("no fan-out subtree", tw.isSubtree());
- }
- }
-
- for (int i = 254; i < 256; i++) {
- idBuf.setByte(Constants.OBJECT_ID_LENGTH - 1, i);
- map.set(idBuf, data1);
- }
- idBuf.setByte(Constants.OBJECT_ID_LENGTH - 2, 1);
- map.set(idBuf, data1);
- n = commitNoteMap(map);
-
- // The 00 bucket is fully split.
- String path = fanout(38, idBuf.name());
- try (TreeWalk tw = TreeWalk.forPath(reader, path, n.getTree())) {
- assertNotNull("has " + path, tw);
- }
-
- // The other bucket is not.
- path = fanout(2, data1.name());
- try (TreeWalk tw = TreeWalk.forPath(reader, path, n.getTree())) {
- assertNotNull("has " + path, tw);
- }
- }
-
- @Test
- public void testRemoveDeletesTreeFanout2_38() throws Exception {
- RevBlob a = tr.blob("a");
- RevBlob data1 = tr.blob("data1");
- RevTree empty = tr.tree();
-
- RevCommit r = tr.commit() //
- .add(fanout(2, a.name()), data1) //
- .create();
- tr.parseBody(r);
-
- NoteMap map = NoteMap.read(reader, r);
- map.set(a, null);
-
- RevCommit n = commitNoteMap(map);
- assertEquals("empty tree", empty, n.getTree());
- }
-
- @Test
- public void testIteratorEmptyMap() {
- Iterator<Note> it = NoteMap.newEmptyMap().iterator();
- assertFalse(it.hasNext());
- }
-
- @Test
- 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));
- }
-
- @Test
- 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));
- }
-
- @Test
- 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));
- }
-
- @Test
- 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));
- }
-
- @Test
- public void testShorteningNoteRefName() throws Exception {
- String expectedShortName = "review";
- String noteRefName = Constants.R_NOTES + expectedShortName;
- assertEquals(expectedShortName, NoteMap.shortenRefName(noteRefName));
- String nonNoteRefName = Constants.R_HEADS + expectedShortName;
- assertEquals(nonNoteRefName, NoteMap.shortenRefName(nonNoteRefName));
- }
-
- private RevCommit commitNoteMap(NoteMap map) throws IOException {
- tr.tick(600);
-
- CommitBuilder builder = new CommitBuilder();
- builder.setTreeId(map.writeTree(inserter));
- tr.setAuthorAndCommitter(builder);
- return tr.getRevWalk().parseCommit(inserter.insert(builder));
- }
-
- private static String fanout(int prefix, String name) {
- StringBuilder r = new StringBuilder();
- int i = 0;
- for (; i < prefix && i < name.length(); i += 2) {
- if (i != 0)
- r.append('/');
- r.append(name.charAt(i + 0));
- r.append(name.charAt(i + 1));
- }
- if (i < name.length()) {
- if (i != 0)
- r.append('/');
- r.append(name.substring(i));
- }
- return r.toString();
- }
-
- private static int count(Iterator it) {
- int c = 0;
- while (it.hasNext()) {
- c++;
- it.next();
- }
- return c;
- }
- }
|