From: Simon Brandhof Date: Fri, 3 Jun 2016 07:03:42 +0000 (+0200) Subject: Move org.sonar.core.user classes outside sonar-db X-Git-Tag: 6.0-RC1~381 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=056b5b562e0c97e5e8c8fe598883c94b07a2bb27;p=sonarqube.git Move org.sonar.core.user classes outside sonar-db --- diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 4fe2f430a13..689e56ee4d7 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -50,8 +50,6 @@ import org.sonar.core.platform.Module; import org.sonar.core.platform.PluginClassloaderFactory; import org.sonar.core.platform.PluginLoader; import org.sonar.core.timemachine.Periods; -import org.sonar.core.user.DefaultUserFinder; -import org.sonar.core.user.DeprecatedUserFinder; import org.sonar.core.util.UuidFactoryImpl; import org.sonar.db.DaoModule; import org.sonar.db.DatabaseChecker; @@ -126,6 +124,8 @@ import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.search.EsSearchModule; import org.sonar.server.startup.LogServerId; import org.sonar.server.test.index.TestIndexer; +import org.sonar.server.user.DefaultUserFinder; +import org.sonar.server.user.DeprecatedUserFinder; import org.sonar.server.user.index.UserIndex; import org.sonar.server.user.index.UserIndexer; import org.sonar.server.view.index.ViewIndex; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 9e70631f342..ef7a11a4c0c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -32,8 +32,6 @@ import org.sonar.api.server.rule.RulesDefinitionXmlLoader; import org.sonar.ce.CeModule; import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.timemachine.Periods; -import org.sonar.core.user.DefaultUserFinder; -import org.sonar.core.user.DeprecatedUserFinder; import org.sonar.db.permission.PermissionRepository; import org.sonar.server.activity.ActivityService; import org.sonar.server.activity.RubyQProfileActivityService; @@ -278,7 +276,9 @@ import org.sonar.server.ui.ws.GlobalNavigationAction; import org.sonar.server.ui.ws.NavigationWs; import org.sonar.server.ui.ws.SettingsNavigationAction; import org.sonar.server.updatecenter.ws.UpdateCenterWs; +import org.sonar.server.user.DefaultUserFinder; import org.sonar.server.user.DefaultUserService; +import org.sonar.server.user.DeprecatedUserFinder; import org.sonar.server.user.GroupMembershipFinder; import org.sonar.server.user.GroupMembershipService; import org.sonar.server.user.NewUserNotifier; diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/DefaultUserFinder.java b/server/sonar-server/src/main/java/org/sonar/server/user/DefaultUserFinder.java new file mode 100644 index 00000000000..83e93033573 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/user/DefaultUserFinder.java @@ -0,0 +1,69 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import com.google.common.collect.Lists; +import java.util.Collection; +import java.util.List; +import javax.annotation.CheckForNull; +import org.sonar.api.user.User; +import org.sonar.api.user.UserFinder; +import org.sonar.api.user.UserQuery; +import org.sonar.db.user.UserDao; +import org.sonar.db.user.UserDto; + +/** + * @since 3.6 + */ +public class DefaultUserFinder implements UserFinder { + + private final UserDao userDao; + + public DefaultUserFinder(UserDao userDao) { + this.userDao = userDao; + } + + @Override + @CheckForNull + public User findByLogin(String login) { + UserDto dto = userDao.selectActiveUserByLogin(login); + return dto != null ? dto.toUser() : null; + } + + @Override + public List findByLogins(List logins) { + List dtos = userDao.selectByLogins(logins); + return toUsers(dtos); + } + + @Override + public List find(UserQuery query) { + List dtos = userDao.selectUsers(query); + return toUsers(dtos); + } + + private static List toUsers(Collection dtos) { + List users = Lists.newArrayList(); + for (UserDto dto : dtos) { + users.add(dto.toUser()); + } + return users; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/DeprecatedUserFinder.java b/server/sonar-server/src/main/java/org/sonar/server/user/DeprecatedUserFinder.java new file mode 100644 index 00000000000..31a0174d198 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/user/DeprecatedUserFinder.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import javax.annotation.Nullable; +import org.sonar.api.database.model.User; +import org.sonar.api.security.UserFinder; +import org.sonar.db.user.UserDao; +import org.sonar.db.user.UserDto; + +/** + * @since 2.10 + */ +public class DeprecatedUserFinder implements UserFinder { + + private final UserDao userDao; + + public DeprecatedUserFinder(UserDao userDao) { + this.userDao = userDao; + } + + @Override + public User findById(int id) { + return copy(userDao.selectUserById(id)); + } + + @Override + public User findByLogin(String login) { + return copy(userDao.selectActiveUserByLogin(login)); + } + + private static User copy(@Nullable UserDto dto) { + if (dto != null) { + User user = new User().setEmail(dto.getEmail()).setLogin(dto.getLogin()).setName(dto.getName()); + user.setId(dto.getId().intValue()); + return user; + } + return null; + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembership.java b/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembership.java new file mode 100644 index 00000000000..9c384d1f7af --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembership.java @@ -0,0 +1,93 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; + +public class GroupMembership { + + private Long id; + private String name; + private String description; + private boolean isMember; + + public Long id() { + return id; + } + + public GroupMembership setId(Long id) { + this.id = id; + return this; + } + + public String name() { + return name; + } + + public GroupMembership setName(String name) { + this.name = name; + return this; + } + + @CheckForNull + public String description() { + return description; + } + + public GroupMembership setDescription(@Nullable String description) { + this.description = description; + return this; + } + + public boolean isMember() { + return isMember; + } + + public GroupMembership setMember(boolean isMember) { + this.isMember = isMember; + return this; + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GroupMembership that = (GroupMembership) o; + return name.equals(that.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembershipFinder.java b/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembershipFinder.java index f6dfe9e4cc8..6dccec36024 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembershipFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/GroupMembershipFinder.java @@ -21,7 +21,6 @@ package org.sonar.server.user; import java.util.List; import org.sonar.api.server.ServerSide; -import org.sonar.core.user.GroupMembership; import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.GroupMembershipDto; import org.sonar.db.user.GroupMembershipQuery; @@ -88,8 +87,12 @@ public class GroupMembershipFinder { private static List toGroupMembership(List dtos) { List groups = newArrayList(); - for (GroupMembershipDto groupMembershipDto : dtos) { - groups.add(groupMembershipDto.toGroupMembership()); + for (GroupMembershipDto dto : dtos) { + groups.add(new GroupMembership() + .setId(dto.getId()) + .setName(dto.getName()) + .setDescription(dto.getDescription()) + .setMember(dto.getUserId() != null)); } return groups; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/DefaultUserFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/DefaultUserFinderTest.java new file mode 100644 index 00000000000..e8bdd7d059e --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/DefaultUserFinderTest.java @@ -0,0 +1,66 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import java.util.Arrays; +import java.util.Collection; +import org.junit.Test; +import org.sonar.api.user.User; +import org.sonar.api.user.UserQuery; +import org.sonar.db.user.UserDao; +import org.sonar.db.user.UserDto; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class DefaultUserFinderTest { + UserDao dao = mock(UserDao.class); + DefaultUserFinder finder = new DefaultUserFinder(dao); + + @Test + public void findByLogin() { + UserDto dto = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com"); + when(dao.selectActiveUserByLogin("david")).thenReturn(dto); + + assertThat(finder.findByLogin("david").name()).isEqualTo("David"); + } + + @Test + public void findByLogins() { + UserDto david = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com"); + UserDto john = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com"); + when(dao.selectByLogins(Arrays.asList("david", "john"))).thenReturn(Arrays.asList(david, john)); + + Collection users = finder.findByLogins(Arrays.asList("david", "john")); + assertThat(users).hasSize(2); + for (User user : users) { + assertThat(user.login()).isIn("david", "john"); + } + } + + @Test + public void findByQuery() { + UserQuery query = UserQuery.builder().logins("simon").build(); + finder.find(query); + verify(dao).selectUsers(query); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/DefaultUserTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/DefaultUserTest.java new file mode 100644 index 00000000000..ed7ad739a2f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/DefaultUserTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import org.junit.Test; +import org.sonar.core.user.DefaultUser; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DefaultUserTest { + @Test + public void test_object_methods() throws Exception { + DefaultUser john = new DefaultUser().setLogin("john").setName("John"); + DefaultUser eric = new DefaultUser().setLogin("eric").setName("Eric"); + + assertThat(john).isEqualTo(john); + assertThat(john).isNotEqualTo(eric); + assertThat(john.hashCode()).isEqualTo(john.hashCode()); + assertThat(john.toString()).contains("login=john").contains("name=John"); + } + + @Test + public void test_email() { + DefaultUser user = new DefaultUser(); + assertThat(user.email()).isNull(); + + user.setEmail(""); + assertThat(user.email()).isNull(); + + user.setEmail(" "); + assertThat(user.email()).isNull(); + + user.setEmail("s@b.com"); + assertThat(user.email()).isEqualTo("s@b.com"); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java new file mode 100644 index 00000000000..02e1419375f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.database.model.User; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.db.user.UserDao; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + + +public class DeprecatedUserFinderTest { + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + @Before + public void init() { + dbTester.prepareDbUnit(DeprecatedUserFinderTest.class, "fixture.xml"); + } + + @Test + public void shouldFindUserByLogin() { + DeprecatedUserFinder finder = new DeprecatedUserFinder(new UserDao(dbTester.myBatis(), mock(System2.class))); + User user = finder.findByLogin("simon"); + assertThat(user.getId(), is(1)); + assertThat(user.getLogin(), is("simon")); + assertThat(user.getName(), is("Simon Brandhof")); + assertThat(user.getEmail(), is("simon.brandhof@sonarsource.com")); + + user = finder.findByLogin("godin"); + assertThat(user.getId(), is(2)); + assertThat(user.getLogin(), is("godin")); + assertThat(user.getName(), is("Evgeny Mandrikov")); + assertThat(user.getEmail(), is("evgeny.mandrikov@sonarsource.com")); + + user = finder.findByLogin("user"); + assertThat(user, nullValue()); + } + + @Test + public void shouldFindUserById() { + DeprecatedUserFinder finder = new DeprecatedUserFinder(new UserDao(dbTester.myBatis(), mock(System2.class))); + User user = finder.findById(1); + assertThat(user.getId(), is(1)); + assertThat(user.getLogin(), is("simon")); + assertThat(user.getName(), is("Simon Brandhof")); + assertThat(user.getEmail(), is("simon.brandhof@sonarsource.com")); + + user = finder.findById(2); + assertThat(user.getId(), is(2)); + assertThat(user.getLogin(), is("godin")); + assertThat(user.getName(), is("Evgeny Mandrikov")); + assertThat(user.getEmail(), is("evgeny.mandrikov@sonarsource.com")); + + user = finder.findById(3); + assertThat(user, nullValue()); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipFinderTest.java index a786ccf32ae..9a15c64e648 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipFinderTest.java @@ -22,7 +22,6 @@ package org.sonar.server.user; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; -import org.sonar.core.user.GroupMembership; import org.sonar.db.user.GroupMembershipDao; import org.sonar.db.user.GroupMembershipDto; import org.sonar.db.user.GroupMembershipQuery; diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipServiceTest.java index c62315399b1..b89c6cc7936 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipServiceTest.java @@ -24,7 +24,6 @@ import java.util.List; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; -import org.sonar.core.user.GroupMembership; import org.sonar.db.DbTester; import org.sonar.server.exceptions.NotFoundException; diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipTest.java new file mode 100644 index 00000000000..6f5e10a111f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/GroupMembershipTest.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.user; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GroupMembershipTest { + + @Test + public void test_setters_and_getters() throws Exception { + GroupMembership group = new GroupMembership() + .setId(1L) + .setName("users") + .setMember(true); + + assertThat(group.id()).isEqualTo(1L); + assertThat(group.name()).isEqualTo("users"); + assertThat(group.isMember()).isTrue(); + } + + @Test + public void test_equals() throws Exception { + assertThat(new GroupMembership().setName("users")).isEqualTo(new GroupMembership().setName("users")); + assertThat(new GroupMembership().setName("users")).isNotEqualTo(new GroupMembership().setName("reviewers")); + + GroupMembership group = new GroupMembership() + .setId(1L) + .setName("users") + .setMember(true); + assertThat(group).isEqualTo(group); + } + +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/DeprecatedUserFinderTest/fixture.xml b/server/sonar-server/src/test/resources/org/sonar/server/user/DeprecatedUserFinderTest/fixture.xml new file mode 100644 index 00000000000..9d370e86a70 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/DeprecatedUserFinderTest/fixture.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java new file mode 100644 index 00000000000..914278a9955 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java @@ -0,0 +1,203 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.core.timemachine; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.sonar.api.config.Settings; +import org.sonar.api.i18n.I18n; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Locale.ENGLISH; +import static org.apache.commons.lang.StringUtils.isNotBlank; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_PERIOD_PREFIX; + +public class Periods { + + private final Settings settings; + private final I18n i18n; + + public Periods(Settings settings, I18n i18n) { + this.settings = settings; + this.i18n = i18n; + } + + @CheckForNull + private static String convertDate(@Nullable Date date) { + if (date != null) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd"); + return dateFormat.format(date); + } + return null; + } + + @CheckForNull + public String label(int periodIndex) { + String periodProperty = settings.getString(TIMEMACHINE_PERIOD_PREFIX + periodIndex); + PeriodParameters periodParameters = new PeriodParameters(periodProperty); + return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); + } + + @CheckForNull + public String abbreviation(int periodIndex) { + String periodProperty = settings.getString(TIMEMACHINE_PERIOD_PREFIX + periodIndex); + PeriodParameters periodParameters = new PeriodParameters(periodProperty); + return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); + } + + @CheckForNull + public String label(String mode, @Nullable String param, @Nullable Date date) { + return label(mode, param, convertDate(date), false); + } + + @CheckForNull + public String label(String mode, @Nullable String param, @Nullable String date) { + return label(mode, param, date, false); + } + + @CheckForNull + public String abbreviation(String mode, @Nullable String param, @Nullable Date date) { + return label(mode, param, convertDate(date), true); + } + + @CheckForNull + private String label(String mode, @Nullable String param, @Nullable String date, boolean shortLabel) { + switch (mode) { + case TIMEMACHINE_MODE_DAYS: + return labelForDays(param, date, shortLabel); + case TIMEMACHINE_MODE_VERSION: + return labelForVersion(param, date, shortLabel); + case TIMEMACHINE_MODE_PREVIOUS_ANALYSIS: + return labelForPreviousAnalysis(date, shortLabel); + case TIMEMACHINE_MODE_PREVIOUS_VERSION: + return labelForPreviousVersion(param, date, shortLabel); + case TIMEMACHINE_MODE_DATE: + return label("since_x", shortLabel, date); + default: + throw new IllegalArgumentException("This mode is not supported : " + mode); + } + } + + private String labelForDays(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (date == null) { + return label("over_x_days", shortLabel, param); + } + return label("over_x_days_detailed", shortLabel, param, date); + } + + private String labelForVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (date == null) { + return label("since_version", shortLabel, param); + } + return label("since_version_detailed", shortLabel, param, date); + } + + private String labelForPreviousAnalysis(@Nullable String date, boolean shortLabel) { + if (date == null) { + return label("since_previous_analysis", shortLabel); + } + return label("since_previous_analysis_detailed", shortLabel, date); + } + + private String labelForPreviousVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { + if (param == null && date == null) { + return label("since_previous_version", shortLabel); + } + if (param == null) { + // Special case when no snapshot for previous version is found. The first analysis is then returned -> Display only the date. + return label("since_previous_version_with_only_date", shortLabel, date); + } + if (date == null) { + return label("since_previous_version_detailed", shortLabel, param); + } + return label("since_previous_version_detailed", shortLabel, param, date); + } + + private String label(String key, boolean shortLabel, Object... parameters) { + String msgKey = key; + if (shortLabel) { + msgKey += ".short"; + } + return i18n.message(ENGLISH, msgKey, null, parameters); + } + + private static class PeriodParameters { + + private String mode = null; + private String param = null; + private Date date = null; + + public PeriodParameters(String periodProperty) { + checkArgument(isNotBlank(periodProperty), "Period property should not be empty"); + Integer possibleDaysValue = findByDays(periodProperty); + Date possibleDatesValue = findByDate(periodProperty); + if (TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(periodProperty)) { + mode = periodProperty; + } else if (possibleDaysValue != null) { + mode = TIMEMACHINE_MODE_DAYS; + param = Integer.toString(possibleDaysValue); + } else if (possibleDatesValue != null) { + mode = TIMEMACHINE_MODE_DATE; + date = possibleDatesValue; + } else { + mode = TIMEMACHINE_MODE_VERSION; + param = periodProperty; + } + } + + private static Integer findByDays(String property) { + try { + return Integer.parseInt(property); + } catch (NumberFormatException e) { + return null; + } + } + + private static Date findByDate(String property) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + try { + return format.parse(property); + } catch (ParseException e) { + return null; + } + } + + public String getMode() { + return mode; + } + + public String getParam() { + return param; + } + + public Date getDate() { + return date; + } + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/timemachine/package-info.java b/sonar-core/src/main/java/org/sonar/core/timemachine/package-info.java new file mode 100644 index 00000000000..7d65a931ce8 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.core.timemachine; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java new file mode 100644 index 00000000000..67867b40f7f --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/timemachine/PeriodsTest.java @@ -0,0 +1,286 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.core.timemachine; + +import java.util.Date; +import java.util.Locale; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.Settings; +import org.sonar.api.i18n.I18n; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.sonar.api.utils.DateUtils.parseDate; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; +import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_PERIOD_PREFIX; + +public class PeriodsTest { + + static String NUMBER_OF_DAYS = "5"; + static String STRING_DATE = "2015-01-01"; + static Date DATE = parseDate(STRING_DATE); + static String VERSION = "1.1"; + static int PERIOD_INDEX = 1; + @Rule + public ExpectedException thrown = ExpectedException.none(); + Settings settings = new Settings(); + I18n i18n = mock(I18n.class); + Periods periods = new Periods(settings, i18n); + + @Test + public void return_over_x_days_label_when_no_date() { + periods.label(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, (String) null); + + verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void return_over_x_days_abbreviation_when_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, null); + + verify(i18n).message(any(Locale.class), eq("over_x_days.short"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void return_over_x_days_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("over_x_days_detailed"), isNull(String.class), eq(NUMBER_OF_DAYS), eq(STRING_DATE)); + } + + @Test + public void return_over_x_days_detailed_abbreviation_when_date_is_set() { + periods.abbreviation(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, DATE); + + verify(i18n).message(any(Locale.class), eq("over_x_days_detailed.short"), isNull(String.class), eq(NUMBER_OF_DAYS), anyString()); + } + + @Test + public void return_over_x_days_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, NUMBER_OF_DAYS); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void return_since_version_label_when_no_date() { + periods.label(TIMEMACHINE_MODE_VERSION, VERSION, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_version_abbreviation_when_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_VERSION, VERSION, null); + + verify(i18n).message(any(Locale.class), eq("since_version.short"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_version_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_VERSION, VERSION, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_version_detailed"), isNull(String.class), eq(VERSION), eq(STRING_DATE)); + } + + @Test + public void return_since_version_detailed_abbreviation_when_date_is_set() { + periods.abbreviation(TIMEMACHINE_MODE_VERSION, VERSION, DATE); + + verify(i18n).message(any(Locale.class), eq("since_version_detailed.short"), isNull(String.class), eq(VERSION), anyString()); + } + + @Test + public void return_since_version_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, VERSION); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_previous_analysis_label_when_no_date() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); + } + + @Test + public void return_since_previous_analysis_abbreviation_when_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, null); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis.short"), isNull(String.class)); + } + + @Test + public void return_since_previous_analysis_detailed_label_when_date_is_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed"), isNull(String.class), eq(STRING_DATE)); + } + + @Test + public void return_since_previous_analysis_detailed_abbreviation_when_date_is_set() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed.short"), isNull(String.class), anyString()); + } + + @Test + public void return_since_previous_analysis_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); + } + + @Test + public void return_since_previous_version_label_when_no_param() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); + } + + @Test + public void return_since_previous_version_abbreviation_when_no_param() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version.short"), isNull(String.class)); + } + + @Test + public void return_since_previous_version_detailed_label_when_param_is_set_and_no_date() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, (String) null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_previous_version_detailed_abbreviation_when_param_is_set_and_no_date() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, null); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION)); + } + + @Test + public void return_since_previous_version_detailed_label_when_param_and_date_are_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed"), isNull(String.class), eq(VERSION), eq(STRING_DATE)); + } + + @Test + public void return_since_previous_version_with_only_date_label_when_no_param_and_date_is_set() { + periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_with_only_date"), isNull(String.class), eq(STRING_DATE)); + } + + @Test + public void return_since_previous_version_detailed_abbreviation_when_param_and_date_are_set() { + periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, DATE); + + verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION), anyString()); + } + + @Test + public void return_since_previous_version_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, TIMEMACHINE_MODE_PREVIOUS_VERSION); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); + } + + @Test + public void return_since_x_label() { + periods.label(TIMEMACHINE_MODE_DATE, null, STRING_DATE); + + verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), eq(STRING_DATE)); + } + + @Test + public void return_since_x_label_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, STRING_DATE); + + periods.label(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), anyString()); + } + + @Test + public void return_since_x_abbreviation() { + periods.abbreviation(TIMEMACHINE_MODE_DATE, null, DATE); + + verify(i18n).message(any(Locale.class), eq("since_x.short"), isNull(String.class), anyString()); + } + + @Test + public void throw_IAE_when_mode_is_unknown() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("This mode is not supported : unknown"); + + periods.label("unknown", null, (String) null); + } + + @Test + public void return_abbreviation_using_settings() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, NUMBER_OF_DAYS); + + periods.abbreviation(PERIOD_INDEX); + + verify(i18n).message(any(Locale.class), eq("over_x_days.short"), isNull(String.class), eq(NUMBER_OF_DAYS)); + } + + @Test + public void throw_IAE_when_period_property_is_empty() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, ""); + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Period property should not be empty"); + + periods.label(PERIOD_INDEX); + } + + @Test + public void throw_IAE_when_period_property_is_null() { + settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, (String) null); + + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Period property should not be empty"); + + periods.label(PERIOD_INDEX); + } + +} diff --git a/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java deleted file mode 100644 index 914278a9955..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/timemachine/Periods.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.timemachine; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.config.Settings; -import org.sonar.api.i18n.I18n; - -import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Locale.ENGLISH; -import static org.apache.commons.lang.StringUtils.isNotBlank; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_PERIOD_PREFIX; - -public class Periods { - - private final Settings settings; - private final I18n i18n; - - public Periods(Settings settings, I18n i18n) { - this.settings = settings; - this.i18n = i18n; - } - - @CheckForNull - private static String convertDate(@Nullable Date date) { - if (date != null) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd"); - return dateFormat.format(date); - } - return null; - } - - @CheckForNull - public String label(int periodIndex) { - String periodProperty = settings.getString(TIMEMACHINE_PERIOD_PREFIX + periodIndex); - PeriodParameters periodParameters = new PeriodParameters(periodProperty); - return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); - } - - @CheckForNull - public String abbreviation(int periodIndex) { - String periodProperty = settings.getString(TIMEMACHINE_PERIOD_PREFIX + periodIndex); - PeriodParameters periodParameters = new PeriodParameters(periodProperty); - return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); - } - - @CheckForNull - public String label(String mode, @Nullable String param, @Nullable Date date) { - return label(mode, param, convertDate(date), false); - } - - @CheckForNull - public String label(String mode, @Nullable String param, @Nullable String date) { - return label(mode, param, date, false); - } - - @CheckForNull - public String abbreviation(String mode, @Nullable String param, @Nullable Date date) { - return label(mode, param, convertDate(date), true); - } - - @CheckForNull - private String label(String mode, @Nullable String param, @Nullable String date, boolean shortLabel) { - switch (mode) { - case TIMEMACHINE_MODE_DAYS: - return labelForDays(param, date, shortLabel); - case TIMEMACHINE_MODE_VERSION: - return labelForVersion(param, date, shortLabel); - case TIMEMACHINE_MODE_PREVIOUS_ANALYSIS: - return labelForPreviousAnalysis(date, shortLabel); - case TIMEMACHINE_MODE_PREVIOUS_VERSION: - return labelForPreviousVersion(param, date, shortLabel); - case TIMEMACHINE_MODE_DATE: - return label("since_x", shortLabel, date); - default: - throw new IllegalArgumentException("This mode is not supported : " + mode); - } - } - - private String labelForDays(@Nullable String param, @Nullable String date, boolean shortLabel) { - if (date == null) { - return label("over_x_days", shortLabel, param); - } - return label("over_x_days_detailed", shortLabel, param, date); - } - - private String labelForVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { - if (date == null) { - return label("since_version", shortLabel, param); - } - return label("since_version_detailed", shortLabel, param, date); - } - - private String labelForPreviousAnalysis(@Nullable String date, boolean shortLabel) { - if (date == null) { - return label("since_previous_analysis", shortLabel); - } - return label("since_previous_analysis_detailed", shortLabel, date); - } - - private String labelForPreviousVersion(@Nullable String param, @Nullable String date, boolean shortLabel) { - if (param == null && date == null) { - return label("since_previous_version", shortLabel); - } - if (param == null) { - // Special case when no snapshot for previous version is found. The first analysis is then returned -> Display only the date. - return label("since_previous_version_with_only_date", shortLabel, date); - } - if (date == null) { - return label("since_previous_version_detailed", shortLabel, param); - } - return label("since_previous_version_detailed", shortLabel, param, date); - } - - private String label(String key, boolean shortLabel, Object... parameters) { - String msgKey = key; - if (shortLabel) { - msgKey += ".short"; - } - return i18n.message(ENGLISH, msgKey, null, parameters); - } - - private static class PeriodParameters { - - private String mode = null; - private String param = null; - private Date date = null; - - public PeriodParameters(String periodProperty) { - checkArgument(isNotBlank(periodProperty), "Period property should not be empty"); - Integer possibleDaysValue = findByDays(periodProperty); - Date possibleDatesValue = findByDate(periodProperty); - if (TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(periodProperty) || TIMEMACHINE_MODE_PREVIOUS_VERSION.equals(periodProperty)) { - mode = periodProperty; - } else if (possibleDaysValue != null) { - mode = TIMEMACHINE_MODE_DAYS; - param = Integer.toString(possibleDaysValue); - } else if (possibleDatesValue != null) { - mode = TIMEMACHINE_MODE_DATE; - date = possibleDatesValue; - } else { - mode = TIMEMACHINE_MODE_VERSION; - param = periodProperty; - } - } - - private static Integer findByDays(String property) { - try { - return Integer.parseInt(property); - } catch (NumberFormatException e) { - return null; - } - } - - private static Date findByDate(String property) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - try { - return format.parse(property); - } catch (ParseException e) { - return null; - } - } - - public String getMode() { - return mode; - } - - public String getParam() { - return param; - } - - public Date getDate() { - return date; - } - } - -} diff --git a/sonar-db/src/main/java/org/sonar/core/timemachine/package-info.java b/sonar-db/src/main/java/org/sonar/core/timemachine/package-info.java deleted file mode 100644 index 7d65a931ce8..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/timemachine/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.core.timemachine; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java b/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java deleted file mode 100644 index 380124f80aa..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/user/DefaultUserFinder.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.user; - -import com.google.common.collect.Lists; -import java.util.Collection; -import java.util.List; -import javax.annotation.CheckForNull; -import org.sonar.api.user.User; -import org.sonar.api.user.UserFinder; -import org.sonar.api.user.UserQuery; -import org.sonar.db.user.UserDao; -import org.sonar.db.user.UserDto; - -/** - * @since 3.6 - */ -public class DefaultUserFinder implements UserFinder { - - private final UserDao userDao; - - public DefaultUserFinder(UserDao userDao) { - this.userDao = userDao; - } - - @Override - @CheckForNull - public User findByLogin(String login) { - UserDto dto = userDao.selectActiveUserByLogin(login); - return dto != null ? dto.toUser() : null; - } - - @Override - public List findByLogins(List logins) { - List dtos = userDao.selectByLogins(logins); - return toUsers(dtos); - } - - @Override - public List find(UserQuery query) { - List dtos = userDao.selectUsers(query); - return toUsers(dtos); - } - - private static List toUsers(Collection dtos) { - List users = Lists.newArrayList(); - for (UserDto dto : dtos) { - users.add(dto.toUser()); - } - return users; - } -} diff --git a/sonar-db/src/main/java/org/sonar/core/user/DeprecatedUserFinder.java b/sonar-db/src/main/java/org/sonar/core/user/DeprecatedUserFinder.java deleted file mode 100644 index b10733c9225..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/user/DeprecatedUserFinder.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.user; - -import javax.annotation.Nullable; -import org.sonar.api.database.model.User; -import org.sonar.api.security.UserFinder; -import org.sonar.db.user.UserDao; -import org.sonar.db.user.UserDto; - -/** - * @since 2.10 - */ -public class DeprecatedUserFinder implements UserFinder { - - private final UserDao userDao; - - public DeprecatedUserFinder(UserDao userDao) { - this.userDao = userDao; - } - - @Override - public User findById(int id) { - return copy(userDao.selectUserById(id)); - } - - @Override - public User findByLogin(String login) { - return copy(userDao.selectActiveUserByLogin(login)); - } - - private static User copy(@Nullable UserDto dto) { - if (dto != null) { - User user = new User().setEmail(dto.getEmail()).setLogin(dto.getLogin()).setName(dto.getName()); - user.setId(dto.getId().intValue()); - return user; - } - return null; - } - -} diff --git a/sonar-db/src/main/java/org/sonar/core/user/GroupMembership.java b/sonar-db/src/main/java/org/sonar/core/user/GroupMembership.java deleted file mode 100644 index 9bd2420ec0a..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/user/GroupMembership.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.user; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; - -public class GroupMembership { - - private Long id; - private String name; - private String description; - private boolean isMember; - - public Long id() { - return id; - } - - public GroupMembership setId(Long id) { - this.id = id; - return this; - } - - public String name() { - return name; - } - - public GroupMembership setName(String name) { - this.name = name; - return this; - } - - @CheckForNull - public String description() { - return description; - } - - public GroupMembership setDescription(@Nullable String description) { - this.description = description; - return this; - } - - public boolean isMember() { - return isMember; - } - - public GroupMembership setMember(boolean isMember) { - this.isMember = isMember; - return this; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - GroupMembership that = (GroupMembership) o; - return name.equals(that.name); - } - - @Override - public int hashCode() { - return name.hashCode(); - } -} diff --git a/sonar-db/src/main/java/org/sonar/core/user/package-info.java b/sonar-db/src/main/java/org/sonar/core/user/package-info.java deleted file mode 100644 index 1b87cf81b81..00000000000 --- a/sonar-db/src/main/java/org/sonar/core/user/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.core.user; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDto.java b/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDto.java index f20b7162f34..517363111d1 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDto.java +++ b/sonar-db/src/main/java/org/sonar/db/user/GroupMembershipDto.java @@ -21,7 +21,6 @@ package org.sonar.db.user; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.sonar.core.user.GroupMembership; /** * @since 4.1 @@ -71,11 +70,4 @@ public class GroupMembershipDto { return this; } - public GroupMembership toGroupMembership() { - return new GroupMembership() - .setId(id) - .setName(name) - .setDescription(description) - .setMember(userId != null); - } } diff --git a/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java b/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java deleted file mode 100644 index 67867b40f7f..00000000000 --- a/sonar-db/src/test/java/org/sonar/core/timemachine/PeriodsTest.java +++ /dev/null @@ -1,286 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.timemachine; - -import java.util.Date; -import java.util.Locale; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.config.Settings; -import org.sonar.api.i18n.I18n; - -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.eq; -import static org.mockito.Mockito.isNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.sonar.api.utils.DateUtils.parseDate; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DAYS; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; -import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_PERIOD_PREFIX; - -public class PeriodsTest { - - static String NUMBER_OF_DAYS = "5"; - static String STRING_DATE = "2015-01-01"; - static Date DATE = parseDate(STRING_DATE); - static String VERSION = "1.1"; - static int PERIOD_INDEX = 1; - @Rule - public ExpectedException thrown = ExpectedException.none(); - Settings settings = new Settings(); - I18n i18n = mock(I18n.class); - Periods periods = new Periods(settings, i18n); - - @Test - public void return_over_x_days_label_when_no_date() { - periods.label(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, (String) null); - - verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(NUMBER_OF_DAYS)); - } - - @Test - public void return_over_x_days_abbreviation_when_no_date() { - periods.abbreviation(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, null); - - verify(i18n).message(any(Locale.class), eq("over_x_days.short"), isNull(String.class), eq(NUMBER_OF_DAYS)); - } - - @Test - public void return_over_x_days_detailed_label_when_date_is_set() { - periods.label(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, STRING_DATE); - - verify(i18n).message(any(Locale.class), eq("over_x_days_detailed"), isNull(String.class), eq(NUMBER_OF_DAYS), eq(STRING_DATE)); - } - - @Test - public void return_over_x_days_detailed_abbreviation_when_date_is_set() { - periods.abbreviation(TIMEMACHINE_MODE_DAYS, NUMBER_OF_DAYS, DATE); - - verify(i18n).message(any(Locale.class), eq("over_x_days_detailed.short"), isNull(String.class), eq(NUMBER_OF_DAYS), anyString()); - } - - @Test - public void return_over_x_days_label_using_settings() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, NUMBER_OF_DAYS); - - periods.label(PERIOD_INDEX); - - verify(i18n).message(any(Locale.class), eq("over_x_days"), isNull(String.class), eq(NUMBER_OF_DAYS)); - } - - @Test - public void return_since_version_label_when_no_date() { - periods.label(TIMEMACHINE_MODE_VERSION, VERSION, (String) null); - - verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(VERSION)); - } - - @Test - public void return_since_version_abbreviation_when_no_date() { - periods.abbreviation(TIMEMACHINE_MODE_VERSION, VERSION, null); - - verify(i18n).message(any(Locale.class), eq("since_version.short"), isNull(String.class), eq(VERSION)); - } - - @Test - public void return_since_version_detailed_label_when_date_is_set() { - periods.label(TIMEMACHINE_MODE_VERSION, VERSION, STRING_DATE); - - verify(i18n).message(any(Locale.class), eq("since_version_detailed"), isNull(String.class), eq(VERSION), eq(STRING_DATE)); - } - - @Test - public void return_since_version_detailed_abbreviation_when_date_is_set() { - periods.abbreviation(TIMEMACHINE_MODE_VERSION, VERSION, DATE); - - verify(i18n).message(any(Locale.class), eq("since_version_detailed.short"), isNull(String.class), eq(VERSION), anyString()); - } - - @Test - public void return_since_version_label_using_settings() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, VERSION); - - periods.label(PERIOD_INDEX); - - verify(i18n).message(any(Locale.class), eq("since_version"), isNull(String.class), eq(VERSION)); - } - - @Test - public void return_since_previous_analysis_label_when_no_date() { - periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, (String) null); - - verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); - } - - @Test - public void return_since_previous_analysis_abbreviation_when_no_date() { - periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, null); - - verify(i18n).message(any(Locale.class), eq("since_previous_analysis.short"), isNull(String.class)); - } - - @Test - public void return_since_previous_analysis_detailed_label_when_date_is_set() { - periods.label(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, STRING_DATE); - - verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed"), isNull(String.class), eq(STRING_DATE)); - } - - @Test - public void return_since_previous_analysis_detailed_abbreviation_when_date_is_set() { - periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, null, DATE); - - verify(i18n).message(any(Locale.class), eq("since_previous_analysis_detailed.short"), isNull(String.class), anyString()); - } - - @Test - public void return_since_previous_analysis_label_using_settings() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); - - periods.label(PERIOD_INDEX); - - verify(i18n).message(any(Locale.class), eq("since_previous_analysis"), isNull(String.class)); - } - - @Test - public void return_since_previous_version_label_when_no_param() { - periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, (String) null); - - verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); - } - - @Test - public void return_since_previous_version_abbreviation_when_no_param() { - periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, null); - - verify(i18n).message(any(Locale.class), eq("since_previous_version.short"), isNull(String.class)); - } - - @Test - public void return_since_previous_version_detailed_label_when_param_is_set_and_no_date() { - periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, (String) null); - - verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed"), isNull(String.class), eq(VERSION)); - } - - @Test - public void return_since_previous_version_detailed_abbreviation_when_param_is_set_and_no_date() { - periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, null); - - verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION)); - } - - @Test - public void return_since_previous_version_detailed_label_when_param_and_date_are_set() { - periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, STRING_DATE); - - verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed"), isNull(String.class), eq(VERSION), eq(STRING_DATE)); - } - - @Test - public void return_since_previous_version_with_only_date_label_when_no_param_and_date_is_set() { - periods.label(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, STRING_DATE); - - verify(i18n).message(any(Locale.class), eq("since_previous_version_with_only_date"), isNull(String.class), eq(STRING_DATE)); - } - - @Test - public void return_since_previous_version_detailed_abbreviation_when_param_and_date_are_set() { - periods.abbreviation(TIMEMACHINE_MODE_PREVIOUS_VERSION, VERSION, DATE); - - verify(i18n).message(any(Locale.class), eq("since_previous_version_detailed.short"), isNull(String.class), eq(VERSION), anyString()); - } - - @Test - public void return_since_previous_version_label_using_settings() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, TIMEMACHINE_MODE_PREVIOUS_VERSION); - - periods.label(PERIOD_INDEX); - - verify(i18n).message(any(Locale.class), eq("since_previous_version"), isNull(String.class)); - } - - @Test - public void return_since_x_label() { - periods.label(TIMEMACHINE_MODE_DATE, null, STRING_DATE); - - verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), eq(STRING_DATE)); - } - - @Test - public void return_since_x_label_using_settings() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, STRING_DATE); - - periods.label(PERIOD_INDEX); - - verify(i18n).message(any(Locale.class), eq("since_x"), isNull(String.class), anyString()); - } - - @Test - public void return_since_x_abbreviation() { - periods.abbreviation(TIMEMACHINE_MODE_DATE, null, DATE); - - verify(i18n).message(any(Locale.class), eq("since_x.short"), isNull(String.class), anyString()); - } - - @Test - public void throw_IAE_when_mode_is_unknown() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("This mode is not supported : unknown"); - - periods.label("unknown", null, (String) null); - } - - @Test - public void return_abbreviation_using_settings() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, NUMBER_OF_DAYS); - - periods.abbreviation(PERIOD_INDEX); - - verify(i18n).message(any(Locale.class), eq("over_x_days.short"), isNull(String.class), eq(NUMBER_OF_DAYS)); - } - - @Test - public void throw_IAE_when_period_property_is_empty() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, ""); - - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Period property should not be empty"); - - periods.label(PERIOD_INDEX); - } - - @Test - public void throw_IAE_when_period_property_is_null() { - settings.setProperty(TIMEMACHINE_PERIOD_PREFIX + PERIOD_INDEX, (String) null); - - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Period property should not be empty"); - - periods.label(PERIOD_INDEX); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java b/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java deleted file mode 100644 index 680fd79bb36..00000000000 --- a/sonar-db/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.user; - -import java.util.Arrays; -import java.util.Collection; -import org.junit.Test; -import org.sonar.api.user.User; -import org.sonar.api.user.UserQuery; -import org.sonar.db.user.UserDao; -import org.sonar.db.user.UserDto; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class DefaultUserFinderTest { - UserDao dao = mock(UserDao.class); - DefaultUserFinder finder = new DefaultUserFinder(dao); - - @Test - public void findByLogin() { - UserDto dto = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com"); - when(dao.selectActiveUserByLogin("david")).thenReturn(dto); - - assertThat(finder.findByLogin("david").name()).isEqualTo("David"); - } - - @Test - public void findByLogins() { - UserDto david = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com"); - UserDto john = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com"); - when(dao.selectByLogins(Arrays.asList("david", "john"))).thenReturn(Arrays.asList(david, john)); - - Collection users = finder.findByLogins(Arrays.asList("david", "john")); - assertThat(users).hasSize(2); - for (User user : users) { - assertThat(user.login()).isIn("david", "john"); - } - } - - @Test - public void findByQuery() { - UserQuery query = UserQuery.builder().logins("simon").build(); - finder.find(query); - verify(dao).selectUsers(query); - } -} diff --git a/sonar-db/src/test/java/org/sonar/core/user/DefaultUserTest.java b/sonar-db/src/test/java/org/sonar/core/user/DefaultUserTest.java deleted file mode 100644 index 91fe424c3d1..00000000000 --- a/sonar-db/src/test/java/org/sonar/core/user/DefaultUserTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.user; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DefaultUserTest { - @Test - public void test_object_methods() throws Exception { - DefaultUser john = new DefaultUser().setLogin("john").setName("John"); - DefaultUser eric = new DefaultUser().setLogin("eric").setName("Eric"); - - assertThat(john).isEqualTo(john); - assertThat(john).isNotEqualTo(eric); - assertThat(john.hashCode()).isEqualTo(john.hashCode()); - assertThat(john.toString()).contains("login=john").contains("name=John"); - } - - @Test - public void test_email() { - DefaultUser user = new DefaultUser(); - assertThat(user.email()).isNull(); - - user.setEmail(""); - assertThat(user.email()).isNull(); - - user.setEmail(" "); - assertThat(user.email()).isNull(); - - user.setEmail("s@b.com"); - assertThat(user.email()).isEqualTo("s@b.com"); - } -} diff --git a/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java b/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java deleted file mode 100644 index 427b0473118..00000000000 --- a/sonar-db/src/test/java/org/sonar/core/user/DeprecatedUserFinderTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.core.user; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.database.model.User; -import org.sonar.api.utils.System2; -import org.sonar.db.DbTester; -import org.sonar.db.user.UserDao; - -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - - -public class DeprecatedUserFinderTest { - - @Rule - public DbTester dbTester = DbTester.create(System2.INSTANCE); - - @Before - public void init() { - dbTester.prepareDbUnit(DeprecatedUserFinderTest.class, "fixture.xml"); - } - - @Test - public void shouldFindUserByLogin() { - DeprecatedUserFinder finder = new DeprecatedUserFinder(new UserDao(dbTester.myBatis(), mock(System2.class))); - User user = finder.findByLogin("simon"); - assertThat(user.getId(), is(1)); - assertThat(user.getLogin(), is("simon")); - assertThat(user.getName(), is("Simon Brandhof")); - assertThat(user.getEmail(), is("simon.brandhof@sonarsource.com")); - - user = finder.findByLogin("godin"); - assertThat(user.getId(), is(2)); - assertThat(user.getLogin(), is("godin")); - assertThat(user.getName(), is("Evgeny Mandrikov")); - assertThat(user.getEmail(), is("evgeny.mandrikov@sonarsource.com")); - - user = finder.findByLogin("user"); - assertThat(user, nullValue()); - } - - @Test - public void shouldFindUserById() { - DeprecatedUserFinder finder = new DeprecatedUserFinder(new UserDao(dbTester.myBatis(), mock(System2.class))); - User user = finder.findById(1); - assertThat(user.getId(), is(1)); - assertThat(user.getLogin(), is("simon")); - assertThat(user.getName(), is("Simon Brandhof")); - assertThat(user.getEmail(), is("simon.brandhof@sonarsource.com")); - - user = finder.findById(2); - assertThat(user.getId(), is(2)); - assertThat(user.getLogin(), is("godin")); - assertThat(user.getName(), is("Evgeny Mandrikov")); - assertThat(user.getEmail(), is("evgeny.mandrikov@sonarsource.com")); - - user = finder.findById(3); - assertThat(user, nullValue()); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDtoTest.java b/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDtoTest.java deleted file mode 100644 index 6d3e97f3adb..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipDtoTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.db.user; - -import org.junit.Test; -import org.sonar.core.user.GroupMembership; - -import static org.assertj.core.api.Assertions.assertThat; - -public class GroupMembershipDtoTest { - - @Test - public void to_group_with_permission_having_permission() { - GroupMembership group = new GroupMembershipDto() - .setId(1L) - .setName("users") - .setDescription("description") - .setUserId(10L) - .toGroupMembership(); - - assertThat(group.id()).isEqualTo(1); - assertThat(group.name()).isEqualTo("users"); - assertThat(group.description()).isEqualTo("description"); - assertThat(group.isMember()).isTrue(); - } - - @Test - public void to_group_with_permission_not_having_permission() { - GroupMembership group = new GroupMembershipDto() - .setId(1L) - .setName("users") - .setDescription("description") - .setUserId(null) - .toGroupMembership(); - - assertThat(group.id()).isEqualTo(1); - assertThat(group.name()).isEqualTo("users"); - assertThat(group.description()).isEqualTo("description"); - assertThat(group.isMember()).isFalse(); - } - -} diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipTest.java b/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipTest.java deleted file mode 100644 index 8441f3f7018..00000000000 --- a/sonar-db/src/test/java/org/sonar/db/user/GroupMembershipTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.db.user; - -import org.junit.Test; -import org.sonar.core.user.GroupMembership; - -import static org.assertj.core.api.Assertions.assertThat; - -public class GroupMembershipTest { - - @Test - public void test_setters_and_getters() throws Exception { - GroupMembership group = new GroupMembership() - .setId(1L) - .setName("users") - .setMember(true); - - assertThat(group.id()).isEqualTo(1L); - assertThat(group.name()).isEqualTo("users"); - assertThat(group.isMember()).isTrue(); - } - - @Test - public void test_equals() throws Exception { - assertThat(new GroupMembership().setName("users")).isEqualTo(new GroupMembership().setName("users")); - assertThat(new GroupMembership().setName("users")).isNotEqualTo(new GroupMembership().setName("reviewers")); - - GroupMembership group = new GroupMembership() - .setId(1L) - .setName("users") - .setMember(true); - assertThat(group).isEqualTo(group); - } -} diff --git a/sonar-db/src/test/resources/org/sonar/core/user/DeprecatedUserFinderTest/fixture.xml b/sonar-db/src/test/resources/org/sonar/core/user/DeprecatedUserFinderTest/fixture.xml deleted file mode 100644 index 9d370e86a70..00000000000 --- a/sonar-db/src/test/resources/org/sonar/core/user/DeprecatedUserFinderTest/fixture.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - -