From 5200b359257c2ab5e29781c2b05c6092fc8338f8 Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 19 Nov 2013 08:01:23 -0500 Subject: [PATCH] Eliminate static singleton calls from user services Change-Id: Ieec34483822f033b19d3ff3259d071c9bc091ed1 --- .../java/com/gitblit/ConfigUserService.java | 7 +- src/main/java/com/gitblit/GitBlit.java | 2 +- .../java/com/gitblit/GitblitUserService.java | 3 +- .../java/com/gitblit/HtpasswdUserService.java | 12 +- src/main/java/com/gitblit/IUserService.java | 7 +- .../java/com/gitblit/LdapUserService.java | 5 +- src/main/java/com/gitblit/PAMUserService.java | 5 +- .../java/com/gitblit/RedmineUserService.java | 5 +- .../com/gitblit/SalesforceUserService.java | 5 +- .../java/com/gitblit/WindowsUserService.java | 5 +- .../tests/HtpasswdUserServiceTest.java | 5 +- .../gitblit/tests/LdapUserServiceTest.java | 7 +- .../gitblit/tests/RedmineUserServiceTest.java | 10 +- .../tests/mock/MockRuntimeManager.java | 133 ++++++++++++++++++ 14 files changed, 170 insertions(+), 41 deletions(-) create mode 100644 src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java diff --git a/src/main/java/com/gitblit/ConfigUserService.java b/src/main/java/com/gitblit/ConfigUserService.java index 6b721a83..79cbdba7 100644 --- a/src/main/java/com/gitblit/ConfigUserService.java +++ b/src/main/java/com/gitblit/ConfigUserService.java @@ -35,6 +35,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.gitblit.Constants.AccessPermission; +import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; import com.gitblit.models.UserRepositoryPreferences; @@ -115,11 +116,11 @@ public class ConfigUserService implements IUserService { /** * Setup the user service. * - * @param settings - * @since 0.7.0 + * @param runtimeManager + * @since 1.4.0 */ @Override - public void setup(IStoredSettings settings) { + public void setup(IRuntimeManager runtimeManager) { } /** diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 8be50fb6..737167fe 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -611,7 +611,7 @@ public class GitBlit extends DaggerContextListener public void setUserService(IUserService userService) { logger.info("Setting up user service " + userService.toString()); this.userService = userService; - this.userService.setup(settings); + this.userService.setup(getManager(IRuntimeManager.class)); } @Override diff --git a/src/main/java/com/gitblit/GitblitUserService.java b/src/main/java/com/gitblit/GitblitUserService.java index ba4c9c5d..7278b222 100644 --- a/src/main/java/com/gitblit/GitblitUserService.java +++ b/src/main/java/com/gitblit/GitblitUserService.java @@ -59,8 +59,7 @@ public class GitblitUserService implements IUserService { } @Override - public void setup(IStoredSettings settings) { - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); + public void setup(IRuntimeManager runtimeManager) { File realmFile = runtimeManager.getFileOrFolder(Keys.realm.userService, "${baseFolder}/users.conf"); serviceImpl = createUserService(realmFile); logger.info("GUS delegating to " + serviceImpl.toString()); diff --git a/src/main/java/com/gitblit/HtpasswdUserService.java b/src/main/java/com/gitblit/HtpasswdUserService.java index a98777b9..3b7120f0 100644 --- a/src/main/java/com/gitblit/HtpasswdUserService.java +++ b/src/main/java/com/gitblit/HtpasswdUserService.java @@ -80,6 +80,7 @@ public class HtpasswdUserService extends GitblitUserService private final boolean SUPPORT_PLAINTEXT_PWD; + private IRuntimeManager runtimeManager; private IStoredSettings settings; private File htpasswdFile; @@ -117,16 +118,16 @@ public class HtpasswdUserService extends GitblitUserService * In addition the setup tries to read and parse the htpasswd file to be used * for authentication. * - * @param settings - * @since 0.7.0 + * @param runtimeManager + * @since 1.4.0 */ @Override - public void setup(IStoredSettings settings) + public void setup(IRuntimeManager runtimeManager) { - this.settings = settings; + this.runtimeManager = runtimeManager; + this.settings = runtimeManager.getSettings(); // This is done in two steps in order to avoid calling GitBlit.getFileOrFolder(String, String) which will segfault for unit tests. - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); String file = settings.getString(KEY_BACKING_US, DEFAULT_BACKING_US); File realmFile = runtimeManager.getFileOrFolder(file); serviceImpl = createUserService(realmFile); @@ -293,7 +294,6 @@ public class HtpasswdUserService extends GitblitUserService if ( !file.equals(htpasswdFilePath) ) { // The htpasswd file setting changed. Rediscover the file. this.htpasswdFilePath = file; - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); this.htpasswdFile = runtimeManager.getFileOrFolder(file); this.htUsers.clear(); this.forceReload = true; diff --git a/src/main/java/com/gitblit/IUserService.java b/src/main/java/com/gitblit/IUserService.java index 628c1650..33f519f0 100644 --- a/src/main/java/com/gitblit/IUserService.java +++ b/src/main/java/com/gitblit/IUserService.java @@ -18,6 +18,7 @@ package com.gitblit; import java.util.Collection; import java.util.List; +import com.gitblit.manager.IRuntimeManager; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; @@ -35,10 +36,10 @@ public interface IUserService { * retrieve settings from gitblit.properties or the web.xml file without * relying on the GitBlit static singleton. * - * @param settings - * @since 0.7.0 + * @param runtimeManager + * @since 1.4.0 */ - void setup(IStoredSettings settings); + void setup(IRuntimeManager runtimeManager); /** * Does the user service support changes to credentials? diff --git a/src/main/java/com/gitblit/LdapUserService.java b/src/main/java/com/gitblit/LdapUserService.java index e6951b40..5619dadd 100644 --- a/src/main/java/com/gitblit/LdapUserService.java +++ b/src/main/java/com/gitblit/LdapUserService.java @@ -81,10 +81,9 @@ public class LdapUserService extends GitblitUserService { } @Override - public void setup(IStoredSettings settings) { - this.settings = settings; + public void setup(IRuntimeManager runtimeManager) { + this.settings = runtimeManager.getSettings(); String file = settings.getString(Keys.realm.ldap.backingUserService, "${baseFolder}/users.conf"); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File realmFile = runtimeManager.getFileOrFolder(file); serviceImpl = createUserService(realmFile); diff --git a/src/main/java/com/gitblit/PAMUserService.java b/src/main/java/com/gitblit/PAMUserService.java index e6ab04a8..b348e64c 100644 --- a/src/main/java/com/gitblit/PAMUserService.java +++ b/src/main/java/com/gitblit/PAMUserService.java @@ -45,11 +45,10 @@ public class PAMUserService extends GitblitUserService { } @Override - public void setup(IStoredSettings settings) { - this.settings = settings; + public void setup(IRuntimeManager runtimeManager) { + this.settings = runtimeManager.getSettings(); String file = settings.getString(Keys.realm.pam.backingUserService, "${baseFolder}/users.conf"); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File realmFile = runtimeManager.getFileOrFolder(file); serviceImpl = createUserService(realmFile); diff --git a/src/main/java/com/gitblit/RedmineUserService.java b/src/main/java/com/gitblit/RedmineUserService.java index 1148925b..62322ebc 100644 --- a/src/main/java/com/gitblit/RedmineUserService.java +++ b/src/main/java/com/gitblit/RedmineUserService.java @@ -60,11 +60,10 @@ public class RedmineUserService extends GitblitUserService { } @Override - public void setup(IStoredSettings settings) { - this.settings = settings; + public void setup(IRuntimeManager runtimeManager) { + this.settings = runtimeManager.getSettings(); String file = settings.getString(Keys.realm.redmine.backingUserService, "${baseFolder}/users.conf"); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File realmFile = runtimeManager.getFileOrFolder(file); serviceImpl = createUserService(realmFile); diff --git a/src/main/java/com/gitblit/SalesforceUserService.java b/src/main/java/com/gitblit/SalesforceUserService.java index bfc96be0..0eca6c9c 100644 --- a/src/main/java/com/gitblit/SalesforceUserService.java +++ b/src/main/java/com/gitblit/SalesforceUserService.java @@ -27,12 +27,11 @@ public class SalesforceUserService extends GitblitUserService { } @Override - public void setup(IStoredSettings settings) { - this.settings = settings; + public void setup(IRuntimeManager runtimeManager) { + this.settings = runtimeManager.getSettings(); String file = settings.getString( Keys.realm.salesforce.backingUserService, "${baseFolder}/users.conf"); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File realmFile = runtimeManager.getFileOrFolder(file); serviceImpl = createUserService(realmFile); diff --git a/src/main/java/com/gitblit/WindowsUserService.java b/src/main/java/com/gitblit/WindowsUserService.java index 03d65e02..9b25efbe 100644 --- a/src/main/java/com/gitblit/WindowsUserService.java +++ b/src/main/java/com/gitblit/WindowsUserService.java @@ -53,11 +53,10 @@ public class WindowsUserService extends GitblitUserService { } @Override - public void setup(IStoredSettings settings) { - this.settings = settings; + public void setup(IRuntimeManager runtimeManager) { + this.settings = runtimeManager.getSettings(); String file = settings.getString(Keys.realm.windows.backingUserService, "${baseFolder}/users.conf"); - IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class); File realmFile = runtimeManager.getFileOrFolder(file); serviceImpl = createUserService(realmFile); 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())); + 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())); + 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())); + 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 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 updatedSettings) { + return settings.saveSettings(updatedSettings); + } + +} -- 2.39.5