diff options
Diffstat (limited to 'src/test/java/com/gitblit/tests')
4 files changed, 144 insertions, 11 deletions
diff --git a/src/test/java/com/gitblit/tests/HtpasswdUserServiceTest.java b/src/test/java/com/gitblit/tests/HtpasswdUserServiceTest.java index e20c82a7..282debb3 100644 --- a/src/test/java/com/gitblit/tests/HtpasswdUserServiceTest.java +++ b/src/test/java/com/gitblit/tests/HtpasswdUserServiceTest.java @@ -28,6 +28,7 @@ import org.junit.Test; import com.gitblit.HtpasswdUserService; import com.gitblit.models.UserModel; import com.gitblit.tests.mock.MemorySettings; +import com.gitblit.tests.mock.MockRuntimeManager; import com.gitblit.utils.StringUtils; /** @@ -72,13 +73,13 @@ public class HtpasswdUserServiceTest extends GitblitUnitTest { private void setupUS() { htpwdUserService = new HtpasswdUserService(); - htpwdUserService.setup(getSettings()); + htpwdUserService.setup(new MockRuntimeManager(getSettings())); } private void setupUS(boolean overrideLA) { htpwdUserService = new HtpasswdUserService(); - htpwdUserService.setup(getSettings(overrideLA)); + htpwdUserService.setup(new MockRuntimeManager(getSettings(overrideLA))); } diff --git a/src/test/java/com/gitblit/tests/LdapUserServiceTest.java b/src/test/java/com/gitblit/tests/LdapUserServiceTest.java index 86d1f3ca..8bd90147 100644 --- a/src/test/java/com/gitblit/tests/LdapUserServiceTest.java +++ b/src/test/java/com/gitblit/tests/LdapUserServiceTest.java @@ -26,6 +26,7 @@ import org.junit.Test; import com.gitblit.LdapUserService; import com.gitblit.models.UserModel; import com.gitblit.tests.mock.MemorySettings; +import com.gitblit.tests.mock.MockRuntimeManager; import com.gitblit.utils.StringUtils; import com.unboundid.ldap.listener.InMemoryDirectoryServer; import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; @@ -60,7 +61,7 @@ public class LdapUserServiceTest extends GitblitUnitTest { @Before public void createLdapUserService() { ldapUserService = new LdapUserService(); - ldapUserService.setup(getSettings()); + ldapUserService.setup(new MockRuntimeManager(getSettings())); } private MemorySettings getSettings() { @@ -118,7 +119,7 @@ public class LdapUserServiceTest extends GitblitUnitTest { MemorySettings ms = getSettings(); ms.put("realm.ldap.displayName", "${personalTitle}. ${givenName} ${surname}"); ldapUserService = new LdapUserService(); - ldapUserService.setup(ms); + ldapUserService.setup(new MockRuntimeManager(ms)); userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray()); assertNotNull(userOneModel); @@ -135,7 +136,7 @@ public class LdapUserServiceTest extends GitblitUnitTest { MemorySettings ms = getSettings(); ms.put("realm.ldap.email", "${givenName}.${surname}@gitblit.com"); ldapUserService = new LdapUserService(); - ldapUserService.setup(ms); + ldapUserService.setup(new MockRuntimeManager(ms)); userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray()); assertNotNull(userOneModel); diff --git a/src/test/java/com/gitblit/tests/RedmineUserServiceTest.java b/src/test/java/com/gitblit/tests/RedmineUserServiceTest.java index 75376239..3c6769f0 100644 --- a/src/test/java/com/gitblit/tests/RedmineUserServiceTest.java +++ b/src/test/java/com/gitblit/tests/RedmineUserServiceTest.java @@ -2,13 +2,11 @@ package com.gitblit.tests; import static org.hamcrest.CoreMatchers.is;
-import java.util.HashMap;
-
import org.junit.Test;
import com.gitblit.RedmineUserService;
import com.gitblit.models.UserModel;
-import com.gitblit.tests.mock.MemorySettings;
+import com.gitblit.tests.mock.MockRuntimeManager;
import com.gitblit.utils.StringUtils;
public class RedmineUserServiceTest extends GitblitUnitTest {
@@ -24,7 +22,7 @@ public class RedmineUserServiceTest extends GitblitUnitTest { @Test
public void testAuthenticate() throws Exception {
RedmineUserService redmineUserService = new RedmineUserService();
- redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));
+ redmineUserService.setup(new MockRuntimeManager());
redmineUserService.setTestingCurrentUserAsJson(JSON);
UserModel userModel = redmineUserService.authenticate("RedmineAdminId", "RedmineAPIKey".toCharArray());
assertThat(userModel.getName(), is("redmineadminid"));
@@ -37,7 +35,7 @@ public class RedmineUserServiceTest extends GitblitUnitTest { @Test
public void testAuthenticateNotAdminUser() throws Exception {
RedmineUserService redmineUserService = new RedmineUserService();
- redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));
+ redmineUserService.setup(new MockRuntimeManager());
redmineUserService.setTestingCurrentUserAsJson(NOT_ADMIN_JSON);
UserModel userModel = redmineUserService.authenticate("RedmineUserId", "RedmineAPIKey".toCharArray());
assertThat(userModel.getName(), is("redmineuserid"));
@@ -50,7 +48,7 @@ public class RedmineUserServiceTest extends GitblitUnitTest { @Test
public void testLocalAccount() {
RedmineUserService redmineUserService = new RedmineUserService();
- redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));
+ redmineUserService.setup(new MockRuntimeManager());
UserModel localAccount = new UserModel("bruce");
localAccount.displayName = "Bruce Campbell";
diff --git a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java new file mode 100644 index 00000000..b42d29a5 --- /dev/null +++ b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java @@ -0,0 +1,133 @@ +/* + * Copyright 2013 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.tests.mock; + +import java.io.File; +import java.util.Date; +import java.util.Map; +import java.util.TimeZone; + +import com.gitblit.Constants; +import com.gitblit.IStoredSettings; +import com.gitblit.manager.IRuntimeManager; +import com.gitblit.models.ServerSettings; +import com.gitblit.models.ServerStatus; +import com.gitblit.models.SettingModel; + +public class MockRuntimeManager implements IRuntimeManager { + + File baseFolder; + + IStoredSettings settings; + + ServerStatus serverStatus; + + ServerSettings serverSettings; + + public MockRuntimeManager() { + this(new MemorySettings()); + } + + public MockRuntimeManager(Map<String, Object> settings) { + this(new MemorySettings(settings)); + } + + public MockRuntimeManager(IStoredSettings settings) { + this.settings = settings; + + this.serverStatus = new ServerStatus(true); + this.serverStatus.servletContainer = "MockServer"; + + this.serverSettings = new ServerSettings(); + } + + @Override + public void setBaseFolder(File folder) { + this.baseFolder = folder; + } + + @Override + public File getBaseFolder() { + return baseFolder; + } + + @Override + public TimeZone getTimezone() { + return TimeZone.getDefault(); + } + + @Override + public boolean isServingRepositories() { + return true; + } + + @Override + public boolean isDebugMode() { + return true; + } + + @Override + public Date getBootDate() { + return serverStatus.bootDate; + } + + @Override + public ServerStatus getStatus() { + // update heap memory status + serverStatus.heapAllocated = Runtime.getRuntime().totalMemory(); + serverStatus.heapFree = Runtime.getRuntime().freeMemory(); + return serverStatus; + } + + @Override + public ServerSettings getSettingsModel() { + // ensure that the current values are updated in the setting models + for (String key : settings.getAllKeys(null)) { + SettingModel setting = serverSettings.get(key); + if (setting == null) { + // unreferenced setting, create a setting model + setting = new SettingModel(); + setting.name = key; + serverSettings.add(setting); + } + setting.currentValue = settings.getString(key, ""); + } + return serverSettings; + } + + @Override + public File getFileOrFolder(String key, String defaultFileOrFolder) { + String fileOrFolder = settings.getString(key, defaultFileOrFolder); + return getFileOrFolder(fileOrFolder); + } + + @Override + public File getFileOrFolder(String fileOrFolder) { + return com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, + baseFolder, fileOrFolder); + } + + @Override + public IStoredSettings getSettings() { + return settings; + } + + @Override + public boolean updateSettings(Map<String, String> updatedSettings) { + return settings.saveSettings(updatedSettings); + } + +} |