diff --git a/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java b/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java index e443905ff4a2..463aec3534db 100644 --- a/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java +++ b/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java @@ -11,6 +11,7 @@ package org.openmrs.web.filter.util; import java.util.ArrayList; + import java.util.Collections; import java.util.List; import java.util.Map; @@ -35,12 +36,10 @@ public class CurrentUsers { * @param servletContext */ - public static Map init(ServletContext servletContext){ - - Map currentUserMap = Collections.synchronizedMap(new TreeMap()); - servletContext.setAttribute(WebConstants.CURRENT_USERS,currentUserMap); - return currentUserMap; - + public static Map init(ServletContext servletContext) { + Map currentUserMap = Collections.synchronizedMap(new TreeMap()); + servletContext.setAttribute(WebConstants.CURRENT_USERS, currentUserMap); + return currentUserMap; } /** * Get the current list of map of users stored in the session @@ -49,15 +48,14 @@ public static Map init(ServletContext servletContext){ * @return map of users logged in */ @SuppressWarnings("unchecked") - private static Map getCurrentUsers(HttpSession httpSession){ - Map currentUsers = (Map) httpSession.getServletContext().getAttribute(WebConstants.CURRENT_USERS); - - if(currentUsers == null) { - currentUsers = init(httpSession.getServletContext()); - } - - return currentUsers; - } + private static Map getCurrentUsers(HttpSession httpSession) { + Map currentUsers = (Map) httpSession.getServletContext().getAttribute( + WebConstants.CURRENT_USERS); + if (currentUsers == null) { + currentUsers = init(httpSession.getServletContext()); + } + return currentUsers; + } /** * Add the user to the current users. @@ -91,17 +89,16 @@ public static void addUser(HttpSession httpSession,User user) { * @param httpSession * @return sorted user names */ - public static List getCurrentUsernames(HttpSession httpSession){ - Map currentUsers = getCurrentUsers(httpSession); - List userNames = new ArrayList(); - synchronized(currentUsers) { - for(String value:currentUsers.values()) { - userNames.add(value); - } - Collections.sort(userNames); - - } - return userNames; - } + public static List getCurrentUsernames(HttpSession httpSession) { + Map currentUsers = getCurrentUsers(httpSession); + List userNames = new ArrayList(); + synchronized (currentUsers) { + for (String value : currentUsers.values()) { + userNames.add(value); + } + } + Collections.sort(userNames); + return userNames; + } } diff --git a/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java b/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java index 3fe89a24de31..26033d50136a 100644 --- a/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java +++ b/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java @@ -9,40 +9,33 @@ */ package org.openmrs.web.filter.update.util; -import java.util.Date; import java.util.List; - import org.junit.Assert; import org.junit.Test; import org.openmrs.User; import org.openmrs.api.UserService; import org.openmrs.test.BaseContextSensitiveTest; + import org.openmrs.web.filter.util.CurrentUsers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpSession; - public class CurrentUserTest extends BaseContextSensitiveTest { - + protected static final String USER_SET = "org/openmrs/CurrentUserTest.xml"; - @Autowired UserService userService; - + @SuppressWarnings("deprecation") @Test public void getCurrentUsernames_shoulReturnCurrentUserNamesForAgivenSession() { executeDataSet(USER_SET); - MockHttpSession session = new MockHttpSession(); - User user = userService.getUser(5510); - - session.setAttribute("user",user); - + User user = userService.getUser(5508); + CurrentUsers.addUser(session,user); List currentUserNames = CurrentUsers.getCurrentUsernames(session); - Assert.assertEquals(1,currentUserNames.size()); - - + Assert.assertTrue(currentUserNames.contains("firstaccount")); + } } diff --git a/web/src/test/resources/org/openmrs/CurrentUserTest.xml b/web/src/test/resources/org/openmrs/CurrentUserTest.xml index 3b3513e24078..3524c74406ac 100644 --- a/web/src/test/resources/org/openmrs/CurrentUserTest.xml +++ b/web/src/test/resources/org/openmrs/CurrentUserTest.xml @@ -11,56 +11,6 @@ --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -