diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-07 15:53:02 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-10 09:16:08 +0200 |
commit | 2f0ebdafec8a47bea4a37ba9e5782df3b0ea315c (patch) | |
tree | eba7e671209189900a259a0b83fd2f1cd6f13c23 /server | |
parent | c644a4df6cb5d86ca51374e14010e883cb22ca5b (diff) | |
download | sonarqube-2f0ebdafec8a47bea4a37ba9e5782df3b0ea315c.tar.gz sonarqube-2f0ebdafec8a47bea4a37ba9e5782df3b0ea315c.zip |
Create and use I18nRule
Diffstat (limited to 'server')
4 files changed, 164 insertions, 96 deletions
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/search_global_permissions-example.json b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/search_global_permissions-example.json index 57672905f3c..a46301087ea 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/search_global_permissions-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/search_global_permissions-example.json @@ -4,43 +4,43 @@ "key": "admin", "name": "Administer System", "description": "Ability to perform all administration functions for the instance: global configuration and personalization of default dashboards.", - "usersCount": 4, + "usersCount": 0, "groupsCount": 1 }, { "key": "profileadmin", "name": "Administer Quality Profiles and Gates", "description": "Ability to perform any action on the quality profiles and gates.", - "usersCount": 10, - "groupsCount": 2 + "usersCount": 2, + "groupsCount": 0 }, { "key": "shareDashboard", "name": "Share Dashboards And Filters", "description": "Ability to share dashboards, issue filters and measure filters.", - "usersCount": 25, - "groupsCount": 3 + "usersCount": 0, + "groupsCount": 1 }, { "key": "scan", "name": "Execute Analysis", "description": "Ability to execute analyses, and to get all settings required to perform the analysis, even the secured ones like the scm account password, the jira account password, and so on.", - "usersCount": 42, - "groupsCount": 1 + "usersCount": 0, + "groupsCount": 2 }, { "key": "dryRunScan", "name": "Execute Preview Analysis", "description": "Ability to execute preview analysis (results are not pushed to the server). This permission does not include the ability to access secured settings such as the scm account password, the jira account password, and so on. This permission is required to execute preview analysis in Eclipse or via the Issues Report plugin.", - "usersCount": 5, - "groupsCount": 2 + "usersCount": 2, + "groupsCount": 0 }, { "key": "provisioning", "name": "Provision Projects", "description": "Ability to initialize project structure before first analysis.", - "usersCount": 9, - "groupsCount": 21 + "usersCount": 1, + "groupsCount": 1 } ] } diff --git a/server/sonar-server/src/test/java/org/sonar/server/i18n/I18nRule.java b/server/sonar-server/src/test/java/org/sonar/server/i18n/I18nRule.java new file mode 100644 index 00000000000..682184d95bc --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/i18n/I18nRule.java @@ -0,0 +1,89 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.i18n; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.sonar.api.i18n.I18n; + +public class I18nRule implements I18n { + private final Map<String, String> messages = new HashMap<>(); + + public I18nRule put(String key, String value) { + messages.put(key, value); + return this; + } + + @Override + public String message(Locale locale, String key, @Nullable String defaultValue, Object... parameters) { + String messageInMap = messages.get(key); + String message = messageInMap != null ? messageInMap : defaultValue; + return formatMessage(message, parameters); + } + + @CheckForNull + private static String formatMessage(@Nullable String message, Object... parameters) { + if (message == null || parameters.length == 0) { + return message; + } + return MessageFormat.format(message.replaceAll("'", "''"), parameters); + } + + @Override + public String age(Locale locale, long durationInMillis) { + throw new UnsupportedOperationException(); + } + + @Override + public String age(Locale locale, Date fromDate, Date toDate) { + throw new UnsupportedOperationException(); + } + + @Override + public String ageFromNow(Locale locale, Date date) { + throw new UnsupportedOperationException(); + } + + @Override + public String formatDateTime(Locale locale, Date date) { + throw new UnsupportedOperationException(); + } + + @Override + public String formatDate(Locale locale, Date date) { + throw new UnsupportedOperationException(); + } + + @Override + public String formatDouble(Locale locale, Double value) { + throw new UnsupportedOperationException(); + } + + @Override + public String formatInteger(Locale locale, Integer value) { + throw new UnsupportedOperationException(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java index e619c841581..94df2895d9e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest.java @@ -20,33 +20,26 @@ package org.sonar.server.permission.ws; -import com.google.common.io.Resources; -import java.util.Locale; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; -import org.sonar.api.i18n.I18n; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; -import org.sonar.db.user.GroupDao; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupRoleDto; -import org.sonar.db.user.RoleDao; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserRoleDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; +import org.sonar.server.i18n.I18nRule; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import org.sonar.test.DbTests; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.sonar.core.permission.GlobalPermissions.DASHBOARD_SHARING; import static org.sonar.core.permission.GlobalPermissions.PREVIEW_EXECUTION; import static org.sonar.core.permission.GlobalPermissions.PROVISIONING; import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN; @@ -64,22 +57,11 @@ public class SearchGlobalPermissionsActionTest { @Rule public UserSessionRule userSession = UserSessionRule.standalone(); WsActionTester ws; + I18nRule i18n = new I18nRule(); @Before public void setUp() { - I18n i18n = mock(I18n.class); - when(i18n.message(any(Locale.class), eq("global_permissions.admin"), any(String.class))).thenReturn("admin"); - when(i18n.message(any(Locale.class), eq("global_permissions.admin.desc"), any(String.class))).thenReturn("admin-description"); - when(i18n.message(any(Locale.class), eq("global_permissions.profileadmin"), any(String.class))).thenReturn("profileadmin"); - when(i18n.message(any(Locale.class), eq("global_permissions.profileadmin.desc"), any(String.class))).thenReturn("profileadmine-description"); - when(i18n.message(any(Locale.class), eq("global_permissions.shareDashboard"), any(String.class))).thenReturn("shareDashboard"); - when(i18n.message(any(Locale.class), eq("global_permissions.shareDashboard.desc"), any(String.class))).thenReturn("shareDashboard-description"); - when(i18n.message(any(Locale.class), eq("global_permissions.scan"), any(String.class))).thenReturn("scan"); - when(i18n.message(any(Locale.class), eq("global_permissions.scan.desc"), any(String.class))).thenReturn("scan-description"); - when(i18n.message(any(Locale.class), eq("global_permissions.dryRunScan"), any(String.class))).thenReturn("dryRunScan"); - when(i18n.message(any(Locale.class), eq("global_permissions.dryRunScan.desc"), any(String.class))).thenReturn("dryRunScan-description"); - when(i18n.message(any(Locale.class), eq("global_permissions.provisioning"), any(String.class))).thenReturn("provisioning"); - when(i18n.message(any(Locale.class), eq("global_permissions.provisioning.desc"), any(String.class))).thenReturn("provisioning-description"); + initI18nMessages(); ws = new WsActionTester(new SearchGlobalPermissionsAction(db.getDbClient(), userSession, i18n)); userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN); @@ -87,27 +69,27 @@ public class SearchGlobalPermissionsActionTest { @Test public void search() { - RoleDao roleDao = db.getDbClient().roleDao(); - GroupDao groupDao = db.getDbClient().groupDao(); - - GroupDto adminGroup = groupDao.insert(db.getSession(), new GroupDto().setName("sonar-admins").setDescription("Administrators")); - GroupDto userGroup = groupDao.insert(db.getSession(), new GroupDto().setName("sonar-users").setDescription("Users")); - roleDao.insertGroupRole(db.getSession(), newGroupRoleDto(SCAN_EXECUTION, userGroup.getId())); - roleDao.insertGroupRole(db.getSession(), newGroupRoleDto(SYSTEM_ADMIN, adminGroup.getId())); - roleDao.insertGroupRole(db.getSession(), newGroupRoleDto(SCAN_EXECUTION, null)); - - UserDto user = db.getDbClient().userDao().insert(db.getSession(), new UserDto().setLogin("user").setName("user-name").setActive(true)); - UserDto adminUser = db.getDbClient().userDao().insert(db.getSession(), new UserDto().setLogin("admin").setName("admin-name").setActive(true)); - roleDao.insertUserRole(db.getSession(), newUserRoleDto(PROVISIONING, user.getId())); - roleDao.insertUserRole(db.getSession(), newUserRoleDto(QUALITY_PROFILE_ADMIN, adminUser.getId())); - roleDao.insertUserRole(db.getSession(), newUserRoleDto(PREVIEW_EXECUTION, adminUser.getId())); - roleDao.insertUserRole(db.getSession(), newUserRoleDto(PREVIEW_EXECUTION, user.getId())); + GroupDto adminGroup = insertGroup(newGroupDto("sonar-admins", "Administrators")); + GroupDto userGroup = insertGroup(newGroupDto("sonar-users", "Users")); + insertGroupRole(newGroupRole(SCAN_EXECUTION, null)); + insertGroupRole(newGroupRole(SCAN_EXECUTION, userGroup.getId())); + insertGroupRole(newGroupRole(SYSTEM_ADMIN, adminGroup.getId())); + insertGroupRole(newGroupRole(PROVISIONING, userGroup.getId())); + insertGroupRole(newGroupRole(DASHBOARD_SHARING, null)); + + UserDto user = insertUser(newUserDto("user", "user-name")); + UserDto adminUser = insertUser(newUserDto("admin", "admin-name")); + insertUserRole(newUserRoleDto(PROVISIONING, user.getId())); + insertUserRole(newUserRoleDto(QUALITY_PROFILE_ADMIN, user.getId())); + insertUserRole(newUserRoleDto(QUALITY_PROFILE_ADMIN, adminUser.getId())); + insertUserRole(newUserRoleDto(PREVIEW_EXECUTION, adminUser.getId())); + insertUserRole(newUserRoleDto(PREVIEW_EXECUTION, user.getId())); db.getSession().commit(); String result = ws.newRequest().execute().getInput(); - assertJson(result).isSimilarTo(Resources.getResource(getClass(), "SearchGlobalPermissionsActionTest/permissions.json")); + assertJson(result).isSimilarTo(getClass().getResource("search_global_permissions-example.json")); } @Test @@ -126,7 +108,50 @@ public class SearchGlobalPermissionsActionTest { ws.newRequest().execute(); } - private static GroupRoleDto newGroupRoleDto(String role, @Nullable Long groupId) { + private void initI18nMessages() { + i18n.put("global_permissions.admin", "Administer System"); + i18n.put("global_permissions.admin.desc", "Ability to perform all administration functions for the instance: " + + "global configuration and personalization of default dashboards."); + i18n.put("global_permissions.profileadmin", "Administer Quality Profiles and Gates"); + i18n.put("global_permissions.profileadmin.desc", "Ability to perform any action on the quality profiles and gates."); + i18n.put("global_permissions.shareDashboard", "Share Dashboards And Filters"); + i18n.put("global_permissions.shareDashboard.desc", "Ability to share dashboards, issue filters and measure filters."); + i18n.put("global_permissions.scan", "Execute Analysis"); + i18n.put("global_permissions.scan.desc", "Ability to execute analyses, and to get all settings required to perform the analysis, " + + "even the secured ones like the scm account password, the jira account password, and so on."); + i18n.put("global_permissions.dryRunScan", "Execute Preview Analysis"); + i18n.put("global_permissions.dryRunScan.desc", "Ability to execute preview analysis (results are not pushed to the server). " + + "This permission does not include the ability to access secured settings such as the scm account password, the jira account password, and so on. " + + "This permission is required to execute preview analysis in Eclipse or via the Issues Report plugin."); + i18n.put("global_permissions.provisioning", "Provision Projects"); + i18n.put("global_permissions.provisioning.desc", "Ability to initialize project structure before first analysis."); + } + + private UserDto insertUser(UserDto user) { + return db.getDbClient().userDao().insert(db.getSession(), user); + } + + private void insertUserRole(UserRoleDto userRole) { + db.getDbClient().roleDao().insertUserRole(db.getSession(), userRole); + } + + private GroupDto insertGroup(GroupDto groupDto) { + return db.getDbClient().groupDao().insert(db.getSession(), groupDto); + } + + private void insertGroupRole(GroupRoleDto group) { + db.getDbClient().roleDao().insertGroupRole(db.getSession(), group); + } + + private static UserDto newUserDto(String login, String name) { + return new UserDto().setLogin(login).setName(name).setActive(true); + } + + private static GroupDto newGroupDto(String name, String description) { + return new GroupDto().setName(name).setDescription(description); + } + + private static GroupRoleDto newGroupRole(String role, @Nullable Long groupId) { GroupRoleDto groupRole = new GroupRoleDto().setRole(role); if (groupId != null) { groupRole.setGroupId(groupId); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest/permissions.json b/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest/permissions.json deleted file mode 100644 index 0e7376a6e1b..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/SearchGlobalPermissionsActionTest/permissions.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "globalPermissions": [ - { - "key": "admin", - "name": "admin", - "description": "admin-description", - "usersCount": 0, - "groupsCount": 1 - }, - { - "key": "profileadmin", - "name": "profileadmin", - "description": "profileadmine-description", - "usersCount": 1, - "groupsCount": 0 - }, - { - "key": "shareDashboard", - "name": "shareDashboard", - "description": "shareDashboard-description", - "usersCount": 0, - "groupsCount": 0 - }, - { - "key": "scan", - "name": "scan", - "description": "scan-description", - "usersCount": 0, - "groupsCount": 2 - }, - { - "key": "dryRunScan", - "name": "dryRunScan", - "description": "dryRunScan-description", - "usersCount": 2, - "groupsCount": 0 - }, - { - "key": "provisioning", - "name": "provisioning", - "description": "provisioning-description", - "usersCount": 1, - "groupsCount": 0 - } - ] -} |