]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 6 Sep 2017 09:09:20 +0000 (11:09 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 6 Sep 2017 09:09:20 +0000 (11:09 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactory.java
server/sonar-server/src/main/java/org/sonar/server/es/SearchIdResult.java
server/sonar-server/src/main/java/org/sonar/server/es/textsearch/ComponentTextSearchQueryFactory.java
server/sonar-server/src/main/java/org/sonar/server/issue/Result.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/AssignAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/AddUserAction.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
sonar-scanner-protocol/src/main/java/org/sonar/scanner/protocol/input/ProjectRepositories.java

index 87c54c55c8435d93a1be7c7fda8bb25557817af5..f9074a13b9accb402de5c983dfdef3b4cbe4a0d9 100644 (file)
@@ -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) {
index 5da8761630c2e7784da72696bb655d167144147e..41787359a6879d282431307ecf33591c3a4ad3ea 100644 (file)
@@ -35,7 +35,7 @@ public class SearchIdResult<ID> {
 
   public SearchIdResult(SearchResponse response, Function<String, ID> converter) {
     this.facets = new Facets(response);
-    this.total = response.getHits().totalHits();
+    this.total = response.getHits().getTotalHits();
     this.ids = convertToIds(response.getHits(), converter);
   }
 
index f4bac4cf3ea4a97e8e34fbb18c71afb01717f8cd..101073ce47827e8bfe02973494283e35d8838ef7 100644 (file)
@@ -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);
       }
     }
index 4bb0bad5636b1691987a923787e7144a3adea5ca..25ce10fb94e230246caf26300c7c416cd7e193d0 100644 (file)
  */
 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<T> {
         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
index 85b1fdf581fcc508feb5b41d565178bf2cddc6a4..c66e7fd144484b26bbece97cff332a64d42943da 100644 (file)
@@ -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());
   }
 }
index 623d74ccb9abb3c664e9314d02db45f82e1fb266..556eda33ea592cf4ab1c45f54cde2f210955bf39 100644 (file)
@@ -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());
   }
 }
index a642aa1294ee00f72f9bf1232c6dbbf142515915..bbf309d5058aae6c43b97e06b47e793774d7520a 100644 (file)
@@ -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());
   }
 }
index f09f0d35c8c8309e33823480a7fbdbe2f6c70469..ea1949354e2ea399581e2dfffba80745f9c4e723 100644 (file)
@@ -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);
   }
 
   /**
index ce1a0575995bbaf581a9202761cc6aec9a9e1410..36dbde468ccbf81e5b74566ce566a58d23d070a4 100644 (file)
@@ -48,11 +48,7 @@ public class ProjectRepositories {
   }
 
   public ProjectRepositories addSettings(String moduleKey, Map<String, String> settings) {
-    Map<String, String> existingSettings = settingsByModule.get(moduleKey);
-    if (existingSettings == null) {
-      existingSettings = new HashMap<>();
-      settingsByModule.put(moduleKey, existingSettings);
-    }
+    Map<String, String> existingSettings = settingsByModule.computeIfAbsent(moduleKey, k -> new HashMap<>());
     existingSettings.putAll(settings);
     return this;
   }
@@ -74,11 +70,7 @@ public class ProjectRepositories {
       return this;
     }
 
-    Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey);
-    if (existingFileDataByPath == null) {
-      existingFileDataByPath = new HashMap<>();
-      fileDataByModuleAndPath.put(moduleKey, existingFileDataByPath);
-    }
+    Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.computeIfAbsent(moduleKey, k -> new HashMap<>());
     existingFileDataByPath.put(path, fileData);
     return this;
   }