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;
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;
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;
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;
--- /dev/null
+/*
+ * 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<User> findByLogins(List<String> logins) {
+ List<UserDto> dtos = userDao.selectByLogins(logins);
+ return toUsers(dtos);
+ }
+
+ @Override
+ public List<User> find(UserQuery query) {
+ List<UserDto> dtos = userDao.selectUsers(query);
+ return toUsers(dtos);
+ }
+
+ private static List<User> toUsers(Collection<UserDto> dtos) {
+ List<User> users = Lists.newArrayList();
+ for (UserDto dto : dtos) {
+ users.add(dto.toUser());
+ }
+ return users;
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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();
+ }
+
+}
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;
private static List<GroupMembership> toGroupMembership(List<GroupMembershipDto> dtos) {
List<GroupMembership> 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;
}
--- /dev/null
+/*
+ * 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<User> 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);
+ }
+}
--- /dev/null
+/*
+ * 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");
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+
+}
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;
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;
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+<dataset>
+
+ <users id="1" login="simon" name="Simon Brandhof" email="simon.brandhof@sonarsource.com"/>
+ <users id="2" login="godin" name="Evgeny Mandrikov" email="evgeny.mandrikov@sonarsource.com"/>
+
+</dataset>
--- /dev/null
+/*
+ * 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;
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+
--- /dev/null
+/*
+ * 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);
+ }
+
+}
+++ /dev/null
-/*
- * 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;
- }
- }
-
-}
+++ /dev/null
-/*
- * 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;
-
+++ /dev/null
-/*
- * 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<User> findByLogins(List<String> logins) {
- List<UserDto> dtos = userDao.selectByLogins(logins);
- return toUsers(dtos);
- }
-
- @Override
- public List<User> find(UserQuery query) {
- List<UserDto> dtos = userDao.selectUsers(query);
- return toUsers(dtos);
- }
-
- private static List<User> toUsers(Collection<UserDto> dtos) {
- List<User> users = Lists.newArrayList();
- for (UserDto dto : dtos) {
- users.add(dto.toUser());
- }
- return users;
- }
-}
+++ /dev/null
-/*
- * 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;
- }
-
-}
+++ /dev/null
-/*
- * 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();
- }
-}
+++ /dev/null
-/*
- * 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;
-
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.sonar.core.user.GroupMembership;
/**
* @since 4.1
return this;
}
- public GroupMembership toGroupMembership() {
- return new GroupMembership()
- .setId(id)
- .setName(name)
- .setDescription(description)
- .setMember(userId != null);
- }
}
+++ /dev/null
-/*
- * 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);
- }
-
-}
+++ /dev/null
-/*
- * 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<User> 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);
- }
-}
+++ /dev/null
-/*
- * 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");
- }
-}
+++ /dev/null
-/*
- * 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());
- }
-
-}
+++ /dev/null
-/*
- * 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();
- }
-
-}
+++ /dev/null
-/*
- * 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);
- }
-}
+++ /dev/null
-<dataset>
-
- <users id="1" login="simon" name="Simon Brandhof" email="simon.brandhof@sonarsource.com"/>
- <users id="2" login="godin" name="Evgeny Mandrikov" email="evgeny.mandrikov@sonarsource.com"/>
-
-</dataset>