src/com/gitblit/models/RepositoryModel.java | ●●●●● patch | view | raw | blame | history | |
tests/com/gitblit/tests/GitBlitSuite.java | ●●●●● patch | view | raw | blame | history | |
tests/com/gitblit/tests/RepositoryModelTest.java | ●●●●● patch | view | raw | blame | history |
src/com/gitblit/models/RepositoryModel.java
@@ -20,9 +20,16 @@ import java.util.Date; import java.util.List; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.Constants.FederationStrategy; import com.gitblit.GitBlit; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.StringUtils; /** @@ -35,6 +42,11 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel> { private static final long serialVersionUID = 1L; public static String CUSTOM_DEFINED_PROP_SECTION = "gitblit"; public static String CUSTOM_DEFINED_PROP_SUBSECTION = "customDefinedProperties"; private final Logger logger = LoggerFactory.getLogger(RepositoryModel.class); // field names are reflectively mapped in EditRepository page public String name; @@ -91,6 +103,37 @@ } return localBranches; } public String getCustomProperty(String propertyKey) { try { Repository r = GitBlit.self().getRepository(name); StoredConfig config = JGitUtils.readConfig(r); return config.getString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey); } catch (Exception e) { logger.error("Error getting Custom Property", e); return null; } } public String setCustomProperty(String propertyKey, String propertyValue) { try { Repository r = GitBlit.self().getRepository(name); StoredConfig config = JGitUtils.readConfig(r); String oldValue = config.getString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey); config.setString(CUSTOM_DEFINED_PROP_SECTION, CUSTOM_DEFINED_PROP_SUBSECTION, propertyKey, propertyValue); config.save(); return oldValue; } catch (Exception e) { logger.error("Error getting Custom Property", e); return null; } } @Override public String toString() { tests/com/gitblit/tests/GitBlitSuite.java
@@ -53,7 +53,7 @@ MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class, DiffUtilsTest.class, MetricUtilsTest.class, TicgitUtilsTest.class, GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class }) GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class, RepositoryModelTest.class }) public class GitBlitSuite { public static final File REPOSITORIES = new File("git"); tests/com/gitblit/tests/RepositoryModelTest.java
New file @@ -0,0 +1,87 @@ package com.gitblit.tests; import static org.junit.Assert.*; 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.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 = RepositoryModel.CUSTOM_DEFINED_PROP_SECTION; oldSubSection = RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION; RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = "RepositoryModelTest"; RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = "RepositoryModelTestSubSection"; } @AfterClass public static void stopGitBlit() throws Exception { if (wasStarted == false) GitBlitSuite.stopGitblit(); RepositoryModel.CUSTOM_DEFINED_PROP_SECTION = oldSection; RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION = oldSubSection; } @Before public void initializeConfiguration() throws Exception{ Repository r = GitBlitSuite.getHelloworldRepository(); StoredConfig config = JGitUtils.readConfig(r); config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION); config.setString(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION, "commitMessageRegEx", "\\d"); config.setString(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION, "anotherProperty", "Hello"); config.save(); } @After public void teardownConfiguration() throws Exception { Repository r = GitBlitSuite.getHelloworldRepository(); StoredConfig config = JGitUtils.readConfig(r); config.unsetSection(RepositoryModel.CUSTOM_DEFINED_PROP_SECTION, RepositoryModel.CUSTOM_DEFINED_PROP_SUBSECTION); config.save(); } @Test public void testGetCustomProperty() throws Exception { RepositoryModel model = GitBlit.self().getRepositoryModel( GitBlitSuite.getHelloworldRepository().getDirectory().getName()); assertEquals("\\d", model.getCustomProperty("commitMessageRegEx")); assertEquals("Hello", model.getCustomProperty("anotherProperty")); } @Test public void testSetCustomProperty() throws Exception { RepositoryModel model = GitBlit.self().getRepositoryModel( GitBlitSuite.getHelloworldRepository().getDirectory().getName()); assertEquals("\\d", model.getCustomProperty("commitMessageRegEx")); assertEquals("Hello", model.getCustomProperty("anotherProperty")); assertEquals("Hello", model.setCustomProperty("anotherProperty", "GoodBye")); assertEquals("\\d", model.getCustomProperty("commitMessageRegEx")); assertEquals("GoodBye", model.getCustomProperty("anotherProperty")); } }