summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFlorian Zschocke <florian.zschocke@cycos.com>2013-08-21 21:53:08 +0200
committerFlorian Zschocke <florian.zschocke@cycos.com>2013-08-26 12:30:53 +0200
commit0a16a66452e425a19755c6b7a629c8ff0c7ade50 (patch)
treee8dc1cd075ec00cf40ddcc4ee8b68d95f3def79b /src
parent0c5c1c872ace327087ba665a51d03a81bcba60f0 (diff)
downloadgitblit-0a16a66452e425a19755c6b7a629c8ff0c7ade50.tar.gz
gitblit-0a16a66452e425a19755c6b7a629c8ff0c7ade50.zip
Check user prefix for invalid options. Add JavaDoc. Add unit tests.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/gitblit/utils/ModelUtils.java143
-rw-r--r--src/test/java/com/gitblit/tests/ModelUtilsTest.java167
2 files changed, 266 insertions, 44 deletions
diff --git a/src/main/java/com/gitblit/utils/ModelUtils.java b/src/main/java/com/gitblit/utils/ModelUtils.java
index 6316dc5d..8f929aad 100644
--- a/src/main/java/com/gitblit/utils/ModelUtils.java
+++ b/src/main/java/com/gitblit/utils/ModelUtils.java
@@ -1,52 +1,107 @@
package com.gitblit.utils;
import com.gitblit.IStoredSettings;
+import com.gitblit.Keys;
+/**
+ * Utility functions for model classes that do not fit in any other category.
+ *
+ * @author Florian Zschocke
+ */
public class ModelUtils
{
- private static final String DEFAULT_USER_REPO_PREFIX = "~";
-
- private static String userRepoPrefix = DEFAULT_USER_REPO_PREFIX;
-
-
-
- public static void setUserRepoPrefix(IStoredSettings settings)
- {
- userRepoPrefix = settings.getString("repo.userPrefix", DEFAULT_USER_REPO_PREFIX);
- }
-
-
- public static String getUserRepoPrefix()
- {
- return userRepoPrefix;
- }
-
-
- public static String getPersonalPath(String username)
- {
- return userRepoPrefix + username.toLowerCase();
- }
-
-
- public static boolean isPersonalRepository(String name)
- {
- if ( name.startsWith(getUserRepoPrefix()) ) return true;
- return false;
- }
-
-
- public static boolean isUsersPersonalRepository(String username, String name)
- {
- if ( name.equalsIgnoreCase(getPersonalPath(username)) ) return true;
- return false;
- }
-
-
- public static String getUserNameFromRepoPath(String path)
- {
- if ( !isPersonalRepository(path) ) return "";
-
- return path.substring(getUserRepoPrefix().length());
- }
+ /**
+ * Default value for the prefix for user repository directories.
+ */
+ private static final String DEFAULT_USER_REPO_PREFIX = "~";
+
+ private static String userRepoPrefix = DEFAULT_USER_REPO_PREFIX;
+
+
+
+ /**
+ * Set the user repository prefix from configuration settings.
+ * @param settings
+ */
+ public static void setUserRepoPrefix(IStoredSettings settings)
+ {
+ String newPrefix = DEFAULT_USER_REPO_PREFIX;
+ if (settings != null) {
+ String prefix = settings.getString(Keys.git.userRepositoryPrefix, DEFAULT_USER_REPO_PREFIX);
+ if (prefix != null && !prefix.trim().isEmpty()) {
+ if (prefix.charAt(0) == '/') prefix = prefix.substring(1);
+ newPrefix = prefix;
+ }
+ }
+
+ userRepoPrefix = newPrefix;
+ }
+
+
+ /**
+ * Get the active user repository project prefix.
+ */
+ public static String getUserRepoPrefix()
+ {
+ return userRepoPrefix;
+ }
+
+
+ /**
+ * Get the user project name for a user.
+ *
+ * @param username name of user
+ * @return the active user repository project prefix concatenated with the user name
+ */
+ public static String getPersonalPath(String username)
+ {
+ return userRepoPrefix + username.toLowerCase();
+ }
+
+
+ /**
+ * Test if a repository path is for a personal repository.
+ *
+ * @param name
+ * A project name, a relative path to a repository.
+ * @return true, if the name starts with the active user repository project prefix. False, otherwise.
+ */
+ public static boolean isPersonalRepository(String name)
+ {
+ if ( name.startsWith(getUserRepoPrefix()) ) return true;
+ return false;
+ }
+
+
+ /**
+ * Test if a repository path is for a personal repository of a specific user.
+ *
+ * @param username
+ * Name of a user
+ * @param name
+ * A project name, a relative path to a repository.
+ * @return true, if the name starts with the active user repository project prefix. False, otherwise.
+ */
+ public static boolean isUsersPersonalRepository(String username, String name)
+ {
+ if ( name.equalsIgnoreCase(getPersonalPath(username)) ) return true;
+ return false;
+ }
+
+
+ /**
+ * Exrtract a user's name from a personal repository path.
+ *
+ * @param path
+ * A project name, a relative path to a repository.
+ * @return If the path does not point to a personal repository, an empty string is returned.
+ * Otherwise the name of the user the personal repository belongs to is returned.
+ */
+ public static String getUserNameFromRepoPath(String path)
+ {
+ if ( !isPersonalRepository(path) ) return "";
+
+ return path.substring(getUserRepoPrefix().length());
+ }
}
diff --git a/src/test/java/com/gitblit/tests/ModelUtilsTest.java b/src/test/java/com/gitblit/tests/ModelUtilsTest.java
new file mode 100644
index 00000000..988f681c
--- /dev/null
+++ b/src/test/java/com/gitblit/tests/ModelUtilsTest.java
@@ -0,0 +1,167 @@
+package com.gitblit.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Test;
+
+import com.gitblit.Keys;
+import com.gitblit.tests.mock.MemorySettings;
+import com.gitblit.utils.ModelUtils;
+
+public class ModelUtilsTest {
+
+ private static final String DEFAULT_USER_REPO_PREFIX = "~";
+
+ private static final Map<String, Object> backingMap = new HashMap<String, Object>();
+ private static final MemorySettings ms = new MemorySettings(backingMap);
+
+
+ private static void setPrefix(String prefix)
+ {
+ backingMap.put(Keys.git.userRepositoryPrefix, prefix);
+ }
+
+
+ private static void setRepoPrefix(String prefix)
+ {
+ backingMap.put(Keys.git.userRepositoryPrefix, prefix);
+ ModelUtils.setUserRepoPrefix(ms);
+ }
+
+
+ @After
+ public void resetPrefix()
+ {
+ setRepoPrefix(DEFAULT_USER_REPO_PREFIX);
+ }
+
+
+ @Test
+ public void testGetUserRepoPrefix()
+ {
+ assertEquals(DEFAULT_USER_REPO_PREFIX, ModelUtils.getUserRepoPrefix());
+ }
+
+
+ @Test
+ public void testSetUserRepoPrefix()
+ {
+
+ assertEquals(DEFAULT_USER_REPO_PREFIX, ModelUtils.getUserRepoPrefix());
+
+ setPrefix("@");
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals("@", ModelUtils.getUserRepoPrefix());
+
+ backingMap.remove(Keys.git.userRepositoryPrefix);
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals(DEFAULT_USER_REPO_PREFIX, ModelUtils.getUserRepoPrefix());
+
+ setPrefix("user/");
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals("user/", ModelUtils.getUserRepoPrefix());
+
+ setPrefix("");
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals(DEFAULT_USER_REPO_PREFIX, ModelUtils.getUserRepoPrefix());
+
+ setPrefix("u_");
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals("u_", ModelUtils.getUserRepoPrefix());
+
+ ModelUtils.setUserRepoPrefix(null);
+ assertEquals(DEFAULT_USER_REPO_PREFIX, ModelUtils.getUserRepoPrefix());
+
+ setPrefix("/somedir/otherdir/");
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals("somedir/otherdir/", ModelUtils.getUserRepoPrefix());
+
+ setPrefix(DEFAULT_USER_REPO_PREFIX);
+ ModelUtils.setUserRepoPrefix(ms);
+ assertEquals(DEFAULT_USER_REPO_PREFIX, ModelUtils.getUserRepoPrefix());
+ }
+
+
+ @Test
+ public void testGetPersonalPath()
+ {
+ String username = "rob";
+ assertEquals(DEFAULT_USER_REPO_PREFIX+username.toLowerCase(), ModelUtils.getPersonalPath(username));
+
+ username = "James";
+ assertEquals(DEFAULT_USER_REPO_PREFIX+username.toLowerCase(), ModelUtils.getPersonalPath(username));
+
+ setRepoPrefix("usr/");
+ username = "noMan";
+ assertEquals("usr/"+username.toLowerCase(), ModelUtils.getPersonalPath(username));
+ }
+
+
+ @Test
+ public void testIsPersonalRepository()
+ {
+ String reponame = DEFAULT_USER_REPO_PREFIX + "one";
+ assertTrue(ModelUtils.isPersonalRepository(reponame));
+
+ reponame = "none";
+ assertFalse(ModelUtils.isPersonalRepository(reponame));
+
+ setRepoPrefix("@@");
+ reponame = "@@two";
+ assertTrue(ModelUtils.isPersonalRepository(reponame));
+
+ setRepoPrefix("users/");
+ reponame = "users/three";
+ assertTrue(ModelUtils.isPersonalRepository(reponame));
+
+ reponame = "project/four";
+ assertFalse(ModelUtils.isPersonalRepository(reponame));
+ }
+
+
+ @Test
+ public void testIsUsersPersonalRepository()
+ {
+ String reponame = DEFAULT_USER_REPO_PREFIX + "lynn";
+ assertTrue(ModelUtils.isUsersPersonalRepository("lynn", reponame));
+
+ reponame = "prjB";
+ assertFalse(ModelUtils.isUsersPersonalRepository("lynn", reponame));
+
+ setRepoPrefix("@@");
+ reponame = "@@newton";
+ assertTrue(ModelUtils.isUsersPersonalRepository("newton", reponame));
+ assertFalse(ModelUtils.isUsersPersonalRepository("hertz", reponame));
+
+ setRepoPrefix("users/");
+ reponame = "users/fee";
+ assertTrue(ModelUtils.isUsersPersonalRepository("fee", reponame));
+ assertFalse(ModelUtils.isUsersPersonalRepository("gnome", reponame));
+
+ reponame = "project/nsbl";
+ assertFalse(ModelUtils.isUsersPersonalRepository("fee", reponame));
+ }
+
+
+ @Test
+ public void testGetUserNameFromRepoPath()
+ {
+ String reponame = DEFAULT_USER_REPO_PREFIX + "lynn";
+ assertEquals("lynn", ModelUtils.getUserNameFromRepoPath(reponame));
+
+ setRepoPrefix("@@");
+ reponame = "@@newton";
+ assertEquals("newton", ModelUtils.getUserNameFromRepoPath(reponame));
+
+ setRepoPrefix("users/");
+ reponame = "users/fee";
+ assertEquals("fee", ModelUtils.getUserNameFromRepoPath(reponame));
+ }
+
+}