From 5f227250b8661cb46967f40673374953c8e495e9 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 09 May 2012 07:59:43 -0400
Subject: [PATCH] Merge pull request #16 from jcrygier/custom_properties

---
 tests/com/gitblit/tests/RepositoryModelTest.java |   92 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 92 insertions(+), 0 deletions(-)

diff --git a/tests/com/gitblit/tests/RepositoryModelTest.java b/tests/com/gitblit/tests/RepositoryModelTest.java
new file mode 100644
index 0000000..3596c33
--- /dev/null
+++ b/tests/com/gitblit/tests/RepositoryModelTest.java
@@ -0,0 +1,92 @@
+package com.gitblit.tests;
+
+import static org.junit.Assert.assertEquals;
+
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.gitblit.Constants;
+import com.gitblit.GitBlit;
+import com.gitblit.models.RepositoryModel;
+import com.gitblit.utils.JGitUtils;
+
+public class RepositoryModelTest {
+	
+	private static String oldSection;
+	private static String oldSubSection;
+	private static boolean wasStarted = false;
+	
+	@BeforeClass
+	public static void startGitBlit() throws Exception {
+		wasStarted = GitBlitSuite.startGitblit() == false;
+		
+		oldSection = Constants.CUSTOM_FIELDS_PROP_SECTION;
+		oldSubSection = Constants.CUSTOM_FIELDS_PROP_SUBSECTION;
+		
+		Constants.CUSTOM_FIELDS_PROP_SECTION = "RepositoryModelTest";
+		Constants.CUSTOM_FIELDS_PROP_SUBSECTION = "RepositoryModelTestSubSection";
+	}
+	
+	@AfterClass
+	public static void stopGitBlit() throws Exception {
+		if (wasStarted == false)
+			GitBlitSuite.stopGitblit();
+		
+		Constants.CUSTOM_FIELDS_PROP_SECTION = oldSection;
+		Constants.CUSTOM_FIELDS_PROP_SUBSECTION = oldSubSection;
+	}
+	
+	@Before
+	public void initializeConfiguration() throws Exception{
+		Repository r = GitBlitSuite.getHelloworldRepository();
+		StoredConfig config = JGitUtils.readConfig(r);
+		
+		config.unsetSection(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION);
+		config.setString(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION, "commitMessageRegEx", "\\d");
+		config.setString(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION, "anotherProperty", "Hello");
+		
+		config.save();
+	}
+	
+	@After
+	public void teardownConfiguration() throws Exception {
+		Repository r = GitBlitSuite.getHelloworldRepository();
+		StoredConfig config = JGitUtils.readConfig(r);
+		
+		config.unsetSection(Constants.CUSTOM_FIELDS_PROP_SECTION, Constants.CUSTOM_FIELDS_PROP_SUBSECTION);
+		config.save();
+	}
+
+	@Test
+	public void testGetCustomProperty() throws Exception {
+		RepositoryModel model = GitBlit.self().getRepositoryModel(
+				GitBlitSuite.getHelloworldRepository().getDirectory().getName());
+		
+		assertEquals("\\d", model.customFields.get("commitMessageRegEx"));
+		assertEquals("Hello", model.customFields.get("anotherProperty"));
+	}
+	
+	@Test
+	public void testSetCustomProperty() throws Exception {
+		RepositoryModel model = GitBlit.self().getRepositoryModel(
+				GitBlitSuite.getHelloworldRepository().getDirectory().getName());
+		
+		assertEquals("\\d", model.customFields.get("commitMessageRegEx"));
+		assertEquals("Hello", model.customFields.get("anotherProperty"));
+		
+		assertEquals("Hello", model.customFields.put("anotherProperty", "GoodBye"));
+		GitBlit.self().updateRepositoryModel(model.name, model, false);
+		
+		model = GitBlit.self().getRepositoryModel(
+				GitBlitSuite.getHelloworldRepository().getDirectory().getName());
+		
+		assertEquals("\\d", model.customFields.get("commitMessageRegEx"));
+		assertEquals("GoodBye", model.customFields.get("anotherProperty"));
+	}
+
+}

--
Gitblit v1.9.1