From: Julien Lancelot Date: Wed, 6 Sep 2017 09:09:20 +0000 (+0200) Subject: Fix quality flaws X-Git-Tag: 6.6-RC1~441 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=24afdb04fb172ef2ff3fe23760513fc16e42e42a;p=sonarqube.git Fix quality flaws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactory.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactory.java index 87c54c55c84..f9074a13b9a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactory.java @@ -125,9 +125,9 @@ class ProjectMeasuresQueryFactory { Operator operator = criterion.getOperator(); String value = criterion.getValue(); checkArgument(EQ.equals(operator), "Only equals operator is available for quality gate criteria"); - Arrays.stream(Level.values()).filter(level -> level.name().equalsIgnoreCase(value)).findFirst() + Level qualityGate = Arrays.stream(Level.values()).filter(level -> level.name().equalsIgnoreCase(value)).findFirst() .orElseThrow(() -> new IllegalArgumentException(format("Unknown quality gate status : '%s'", value))); - query.setQualityGateStatus(Level.valueOf(value)); + query.setQualityGateStatus(qualityGate); } private static void processMetricCriterion(Criterion criterion, ProjectMeasuresQuery query) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/SearchIdResult.java b/server/sonar-server/src/main/java/org/sonar/server/es/SearchIdResult.java index 5da8761630c..41787359a68 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/SearchIdResult.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/SearchIdResult.java @@ -35,7 +35,7 @@ public class SearchIdResult { public SearchIdResult(SearchResponse response, Function converter) { this.facets = new Facets(response); - this.total = response.getHits().totalHits(); + this.total = response.getHits().getTotalHits(); this.ids = convertToIds(response.getHits(), converter); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/textsearch/ComponentTextSearchQueryFactory.java b/server/sonar-server/src/main/java/org/sonar/server/es/textsearch/ComponentTextSearchQueryFactory.java index f4bac4cf3ea..101073ce478 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/textsearch/ComponentTextSearchQueryFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/textsearch/ComponentTextSearchQueryFactory.java @@ -163,11 +163,11 @@ public class ComponentTextSearchQueryFactory { } public ComponentTextSearchQuery build() { - this.queryText = requireNonNull(queryText, "query text cannot be null"); - this.fieldKey = requireNonNull(fieldKey, "field key cannot be null"); - this.fieldName = requireNonNull(fieldName, "field name cannot be null"); - this.recentlyBrowsedKeys = requireNonNull(recentlyBrowsedKeys, "field recentlyBrowsedKeys cannot be null"); - this.favoriteKeys = requireNonNull(favoriteKeys, "field favoriteKeys cannot be null"); + requireNonNull(queryText, "query text cannot be null"); + requireNonNull(fieldKey, "field key cannot be null"); + requireNonNull(fieldName, "field name cannot be null"); + requireNonNull(recentlyBrowsedKeys, "field recentlyBrowsedKeys cannot be null"); + requireNonNull(favoriteKeys, "field favoriteKeys cannot be null"); return new ComponentTextSearchQuery(this); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java b/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java index 4bb0bad5636..25ce10fb94e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java @@ -19,13 +19,11 @@ */ package org.sonar.server.issue; -import org.apache.commons.lang.builder.ReflectionToStringBuilder; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - import java.util.Arrays; import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.lang.builder.ReflectionToStringBuilder; import static com.google.common.collect.Lists.newArrayList; @@ -133,13 +131,7 @@ public class Result { return false; } // Probably incorrect - comparing Object[] arrays with Arrays.equals - if (!Arrays.equals(l10nParams, message.l10nParams)) { - return false; - } - if ((text != null) ? !text.equals(message.text) : (message.text != null)) { - return false; - } - return true; + return Arrays.equals(l10nParams, message.l10nParams) && ((text != null) ? text.equals(message.text) : (message.text == null)); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AssignAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AssignAction.java index 85b1fdf581f..c66e7fd1444 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AssignAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/AssignAction.java @@ -45,6 +45,7 @@ import org.sonar.server.issue.IssueFinder; import org.sonar.server.issue.IssueUpdater; import org.sonar.server.user.UserSession; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.emptyToNull; import static java.lang.String.format; import static java.util.Objects.requireNonNull; @@ -66,7 +67,7 @@ public class AssignAction implements IssuesWsAction { private final OperationResponseWriter responseWriter; public AssignAction(System2 system2, UserSession userSession, DbClient dbClient, IssueFinder issueFinder, IssueFieldsSetter issueFieldsSetter, IssueUpdater issueUpdater, - OperationResponseWriter responseWriter) { + OperationResponseWriter responseWriter) { this.system2 = system2; this.userSession = userSession; this.dbClient = dbClient; @@ -148,8 +149,8 @@ public class AssignAction implements IssuesWsAction { ComponentDto project = Optional.ofNullable(dbClient.componentDao().selectByUuid(dbSession, projectUuid).orNull()) .orElseThrow(() -> new IllegalStateException(format("Unknown project %s", projectUuid))); OrganizationDto organizationDto = dbClient.organizationDao().selectByUuid(dbSession, project.getOrganizationUuid()) - .orElseThrow(() -> new IllegalStateException(format("Unknown organization %s", project.getOrganizationUuid()))); - dbClient.organizationMemberDao().select(dbSession, organizationDto.getUuid(), user.getId()) - .orElseThrow(() -> new IllegalArgumentException(format("User '%s' is not member of organization '%s'", user.getLogin(), organizationDto.getKey()))); + .orElseThrow(() -> new IllegalStateException(format("Unknown organizationMember %s", project.getOrganizationUuid()))); + checkArgument(dbClient.organizationMemberDao().select(dbSession, organizationDto.getUuid(), user.getId()).isPresent(), + "User '%s' is not member of organization '%s'", user.getLogin(), organizationDto.getKey()); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java index 623d74ccb9a..556eda33ea5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java @@ -37,7 +37,7 @@ import org.sonar.server.usergroups.ws.GroupWsRef; import org.sonar.server.usergroups.ws.GroupWsSupport; import org.sonarqube.ws.client.permission.PermissionsWsParameters; -import static java.lang.String.format; +import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.server.ws.WsUtils.checkFound; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME; @@ -106,7 +106,7 @@ public class PermissionWsSupport { } public void checkMembership(DbSession dbSession, OrganizationDto organization, UserId user) { - dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId()) - .orElseThrow(() -> new IllegalArgumentException(format("User '%s' is not member of organization '%s'", user.getLogin(), organization.getKey()))); + checkArgument(dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId()).isPresent(), + "User '%s' is not member of organization '%s'", user.getLogin(), organization.getKey()); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/AddUserAction.java b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/AddUserAction.java index a642aa1294e..bbf309d5058 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/AddUserAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/AddUserAction.java @@ -32,6 +32,7 @@ import org.sonar.db.user.UserDto; import org.sonar.db.user.UserGroupDto; import org.sonar.server.user.UserSession; +import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_ID; @@ -98,7 +99,7 @@ public class AddUserAction implements UserGroupsWsAction { } private void checkMembership(DbSession dbSession, OrganizationDto organization, UserDto user) { - dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId()) - .orElseThrow(() -> new IllegalArgumentException(format("User '%s' is not member of organization '%s'", user.getLogin(), organization.getKey()))); + checkArgument(dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId()).isPresent(), + "User '%s' is not member of organization '%s'", user.getLogin(), organization.getKey()); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index f09f0d35c8c..ea1949354e2 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -287,7 +287,7 @@ public class Rule { */ @Deprecated public Rule getParent() { - return template; + return getTemplate(); } /** @@ -295,8 +295,7 @@ public class Rule { */ @Deprecated public Rule setParent(Rule parent) { - this.template = parent; - return this; + return setTemplate(parent); } /** diff --git a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/input/ProjectRepositories.java b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/input/ProjectRepositories.java index ce1a0575995..36dbde468cc 100644 --- a/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/input/ProjectRepositories.java +++ b/sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/input/ProjectRepositories.java @@ -48,11 +48,7 @@ public class ProjectRepositories { } public ProjectRepositories addSettings(String moduleKey, Map settings) { - Map existingSettings = settingsByModule.get(moduleKey); - if (existingSettings == null) { - existingSettings = new HashMap<>(); - settingsByModule.put(moduleKey, existingSettings); - } + Map existingSettings = settingsByModule.computeIfAbsent(moduleKey, k -> new HashMap<>()); existingSettings.putAll(settings); return this; } @@ -74,11 +70,7 @@ public class ProjectRepositories { return this; } - Map existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey); - if (existingFileDataByPath == null) { - existingFileDataByPath = new HashMap<>(); - fileDataByModuleAndPath.put(moduleKey, existingFileDataByPath); - } + Map existingFileDataByPath = fileDataByModuleAndPath.computeIfAbsent(moduleKey, k -> new HashMap<>()); existingFileDataByPath.put(path, fileData); return this; }