summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/gitblit/tests/ModelUtilsTest.java
blob: 9dd29b64b96c14430f3d086fd907525efc25f135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.gitblit.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Test;

import com.gitblit.Constants;
import com.gitblit.utils.ModelUtils;

public class ModelUtilsTest {

	@After
	public void resetPrefix()
	{
		ModelUtils.setUserRepoPrefix(null);
	}


	@Test
	public void testGetUserRepoPrefix()
	{
		assertEquals(Constants.DEFAULT_USER_REPOSITORY_PREFIX, ModelUtils.getUserRepoPrefix());
	}


	@Test
	public void testSetUserRepoPrefix()
	{

		assertEquals(Constants.DEFAULT_USER_REPOSITORY_PREFIX, ModelUtils.getUserRepoPrefix());

		ModelUtils.setUserRepoPrefix("@");
		assertEquals("@", ModelUtils.getUserRepoPrefix());

		ModelUtils.setUserRepoPrefix("");
		assertEquals(Constants.DEFAULT_USER_REPOSITORY_PREFIX, ModelUtils.getUserRepoPrefix());

		ModelUtils.setUserRepoPrefix("user/");
		assertEquals("user/", ModelUtils.getUserRepoPrefix());

		ModelUtils.setUserRepoPrefix("u_");
		assertEquals("u_", ModelUtils.getUserRepoPrefix());

		ModelUtils.setUserRepoPrefix(null);
		assertEquals(Constants.DEFAULT_USER_REPOSITORY_PREFIX, ModelUtils.getUserRepoPrefix());

		ModelUtils.setUserRepoPrefix("/somedir/otherdir/");
		assertEquals("somedir/otherdir/", ModelUtils.getUserRepoPrefix());
	}


	@Test
	public void testGetPersonalPath()
	{
		String username = "rob";
		assertEquals(Constants.DEFAULT_USER_REPOSITORY_PREFIX+username.toLowerCase(), ModelUtils.getPersonalPath(username));

		username = "James";
		assertEquals(Constants.DEFAULT_USER_REPOSITORY_PREFIX+username.toLowerCase(), ModelUtils.getPersonalPath(username));

		ModelUtils.setUserRepoPrefix("usr/");
		username = "noMan";
		assertEquals("usr/"+username.toLowerCase(), ModelUtils.getPersonalPath(username));
	}


	@Test
	public void testIsPersonalRepository()
	{
		String reponame = Constants.DEFAULT_USER_REPOSITORY_PREFIX + "one";
		assertTrue(ModelUtils.isPersonalRepository(reponame));

		reponame = "none";
		assertFalse(ModelUtils.isPersonalRepository(reponame));

		ModelUtils.setUserRepoPrefix("@@");
		reponame = "@@two";
		assertTrue(ModelUtils.isPersonalRepository(reponame));

		ModelUtils.setUserRepoPrefix("users/");
		reponame = "users/three";
		assertTrue(ModelUtils.isPersonalRepository(reponame));

		reponame = "project/four";
		assertFalse(ModelUtils.isPersonalRepository(reponame));
	}


	@Test
	public void testIsUsersPersonalRepository()
	{
		String reponame = Constants.DEFAULT_USER_REPOSITORY_PREFIX + "lynn";
		assertTrue(ModelUtils.isUsersPersonalRepository("lynn", reponame));

		reponame = "prjB";
		assertFalse(ModelUtils.isUsersPersonalRepository("lynn", reponame));

		ModelUtils.setUserRepoPrefix("@@");
		reponame = "@@newton";
		assertTrue(ModelUtils.isUsersPersonalRepository("newton", reponame));
		assertFalse(ModelUtils.isUsersPersonalRepository("hertz", reponame));

		ModelUtils.setUserRepoPrefix("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 = Constants.DEFAULT_USER_REPOSITORY_PREFIX + "lynn";
		assertEquals("lynn", ModelUtils.getUserNameFromRepoPath(reponame));

		ModelUtils.setUserRepoPrefix("@@");
		reponame = "@@newton";
		assertEquals("newton", ModelUtils.getUserNameFromRepoPath(reponame));

		ModelUtils.setUserRepoPrefix("users/");
		reponame = "users/fee";
		assertEquals("fee", ModelUtils.getUserNameFromRepoPath(reponame));
	}

}