[PATCH JGit 16/19] added tests for the file based info cache update and made pass

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: mike.gaffney <mike.gaffney@xxxxxxxxxxxxxx>

---
 .../jgit/lib/InfoDirectoryDatabaseTest.java        |   30 ++++++++++++++++++++
 .../src/org/spearce/jgit/lib/InfoDatabase.java     |   15 ++++++++++
 .../spearce/jgit/lib/InfoDirectoryDatabase.java    |   15 ++++++++++
 3 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/InfoDirectoryDatabaseTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/InfoDirectoryDatabaseTest.java
index 2b7fb5b..22972fa 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/InfoDirectoryDatabaseTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/InfoDirectoryDatabaseTest.java
@@ -1,6 +1,10 @@
 package org.spearce.jgit.lib;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
 
 import org.spearce.jgit.util.JGitTestUtil;
 
@@ -27,4 +31,30 @@ public void testCreateCreatesDirectory() throws Exception {
 		new InfoDirectoryDatabase(testDir).create();
 		assertTrue(testDir.exists());
 	}
+	
+	public void testUpdateInfoCache() throws Exception {
+		Collection<Ref> refs = new ArrayList<Ref>();
+		refs.add(new Ref(Ref.Storage.LOOSE, "refs/heads/master", ObjectId.fromString("32aae7aef7a412d62192f710f2130302997ec883")));
+		refs.add(new Ref(Ref.Storage.LOOSE, "refs/heads/development", ObjectId.fromString("184063c9b594f8968d61a686b2f6052779551613")));
+
+		File expectedFile = new File(testDir, "refs");
+		assertFalse(expectedFile.exists());
+		
+		
+		final StringWriter expectedString = new StringWriter();
+		new RefWriter(refs) {
+			@Override
+			protected void writeFile(String file, byte[] content) throws IOException {
+				expectedString.write(new String(content));
+			}
+		}.writeInfoRefs();
+		
+		InfoDirectoryDatabase out = new InfoDirectoryDatabase(testDir);
+		out.create();
+		out.updateInfoCache(refs);
+		assertTrue(expectedFile.exists());
+		
+		String actual = JGitTestUtil.readFileAsString(expectedFile);
+		assertEquals(expectedString.toString(), actual);
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDatabase.java
index 2f1f398..26f8f22 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDatabase.java
@@ -1,8 +1,23 @@
 package org.spearce.jgit.lib;
 
+import java.io.IOException;
+import java.util.Collection;
+
 public abstract class InfoDatabase {
 
+	/**
+	 * Create the info database
+	 */
 	public void create() {
 	}
 
+	/**
+	 * Updates the info cache typically done by update-server-info command.
+	 * This writes THIS repository's refs out to the info/refs file.
+	 * @param collection the collections of refs to update the info cache with
+	 * @throws IOException for any type of failure on the local or remote 
+	 * 					   data store
+	 */
+	public abstract void updateInfoCache(Collection<Ref> collection) throws IOException;
+
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDirectoryDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDirectoryDatabase.java
index 20d8a70..f95be2f 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDirectoryDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/InfoDirectoryDatabase.java
@@ -1,6 +1,9 @@
 package org.spearce.jgit.lib;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Collection;
 
 public class InfoDirectoryDatabase extends InfoDatabase {
 
@@ -15,4 +18,16 @@ public void create() {
 		info.mkdirs();
 	}
 
+	@Override
+	public void updateInfoCache(Collection<Ref> refs) throws IOException {
+		new RefWriter(refs) {
+			@Override
+			protected void writeFile(String file, byte[] content) throws IOException {
+				FileOutputStream fos = new FileOutputStream(new File(info, "refs"));
+				fos.write(content);
+				fos.close();
+			}
+		}.writeInfoRefs();
+	}
+
 }
-- 
1.6.4.2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]