]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 30 Apr 2014 13:59:38 +0000 (15:59 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 30 Apr 2014 13:59:38 +0000 (15:59 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java
sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java
sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java
sonar-server/src/main/java/org/sonar/server/issue/IssueService.java
sonar-server/src/main/java/org/sonar/server/platform/SonarHome.java [deleted file]
sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java

index 437fd62ff55ad17ff9feb8e4a37c69097d1a73e5..99f5f02897880760b0953bc2c905a06e972a4b86 100644 (file)
@@ -26,8 +26,6 @@ import org.slf4j.LoggerFactory;
 import org.sonar.api.utils.SonarException;
 import org.sonar.home.cache.FileCache;
 
-import javax.annotation.CheckForNull;
-
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -61,7 +59,7 @@ public class JdbcDriverHolder {
       try {
         LOG.info("Install JDBC driver");
         String[] nameAndHash = downloadJdbcDriverIndex();
-        if (nameAndHash != null) {
+        if (nameAndHash.length > 0) {
           String filename = nameAndHash[0];
           String hash = nameAndHash[1];
 
@@ -138,7 +136,6 @@ public class JdbcDriverHolder {
     }
   }
 
-  @CheckForNull
   private String[] downloadJdbcDriverIndex() {
     String url = "/deploy/jdbc-driver.txt";
     try {
@@ -146,7 +143,7 @@ public class JdbcDriverHolder {
       String indexContent = serverClient.request(url);
       // File is empty when H2 is used
       if (Strings.isNullOrEmpty(indexContent)) {
-        return null;
+        return new String[]{};
       }
       return indexContent.split("\\|");
     } catch (Exception e) {
index d823a0650cfedaba53f794951dbbc0bd7182b7a7..b328680a990d2a0ffcbdae0144772e0e607d3c0c 100644 (file)
@@ -26,7 +26,6 @@ import org.sonar.api.resources.ResourceTypes;
 import org.sonar.api.task.Task;
 import org.sonar.api.task.TaskComponent;
 import org.sonar.api.task.TaskDefinition;
-import org.sonar.api.task.TaskExtension;
 import org.sonar.api.utils.SonarException;
 import org.sonar.batch.scan.ScanTask;
 import org.sonar.batch.tasks.ListTask;
index 5180a49875251ee07e08ae7c67ad511c06d39e9b..f4769d38316341d6a2d247ce4898ff37c02f726e 100644 (file)
  */
 package org.sonar.batch.scan;
 
-import org.sonar.api.BatchComponent;
-import org.sonar.batch.qualitygate.GenerateQualityGateEvents;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.sonar.api.BatchExtension;
+import org.sonar.api.BatchComponent;
 import org.sonar.api.batch.InstantiationStrategy;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.rule.CheckFactory;
@@ -51,6 +48,7 @@ import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
 import org.sonar.batch.issue.ignore.scanner.IssueExclusionsRegexpScanner;
 import org.sonar.batch.phases.PhaseExecutor;
 import org.sonar.batch.phases.PhasesTimeProfiler;
+import org.sonar.batch.qualitygate.GenerateQualityGateEvents;
 import org.sonar.batch.qualitygate.QualityGateProvider;
 import org.sonar.batch.qualitygate.QualityGateVerifier;
 import org.sonar.batch.rule.*;
index 9ac381eeffddf25601252f106aa0e7082f7609a2..0edb74d1b0a39f08711eb29abbb2a630ae99bb2f 100644 (file)
@@ -247,9 +247,9 @@ public class IssueUpdater implements BatchComponent, ServerComponent {
     return false;
   }
 
-  public boolean setProject(DefaultIssue issue, String key, IssueChangeContext context) {
-    if (!Objects.equal(key, issue.projectKey())) {
-      issue.setProjectKey(key);
+  public boolean setProject(DefaultIssue issue, @Nullable String projectKey, IssueChangeContext context) {
+    if (!Objects.equal(projectKey, issue.projectKey())) {
+      issue.setProjectKey(projectKey);
       issue.setUpdateDate(context.date());
       issue.setChanged(true);
       return true;
index e6a380a00801c96a95220a3bdad6639785f09cfa..d5a4f38d177a68a7832d1559d8a906f4399a93c4 100644 (file)
@@ -141,7 +141,7 @@ public class DefaultIssue implements Issue {
     return projectKey;
   }
 
-  public DefaultIssue setProjectKey(String projectKey) {
+  public DefaultIssue setProjectKey(@Nullable String projectKey) {
     this.projectKey = projectKey;
     return this;
   }
index 5e026586f1adec0e44913e46582c4882f00130ec..c15c3e10c1e244a580c40dac63e770868a7f3810 100644 (file)
@@ -210,47 +210,47 @@ public class IssueService implements ServerComponent {
   public DefaultIssue createManualIssue(String componentKey, RuleKey ruleKey, @Nullable Integer line, @Nullable String message, @Nullable String severity,
                                         @Nullable Double effortToFix, UserSession userSession) {
     verifyLoggedIn(userSession);
-    ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(componentKey));
+    ResourceDto component = resourceDao.getResource(ResourceQuery.create().setKey(componentKey));
     ResourceDto project = resourceDao.getRootProjectByComponentKey(componentKey);
-    if (resourceDto == null || project == null) {
+    if (component == null || project == null) {
       throw new IllegalArgumentException("Unknown component: " + componentKey);
     }
+    if (!authorizationDao.isAuthorizedComponentKey(component.getKey(), userSession.userId(), UserRole.USER)) {
+      // TODO throw unauthorized
+      throw new IllegalStateException("User does not have the required role");
+    }
+    if (!org.sonar.server.rule.Rule.MANUAL_REPOSITORY_KEY.equals(ruleKey.repository())) {
+      throw new IllegalArgumentException("Issues can be created only on rules marked as 'manual': " + ruleKey);
+    }
+    Rule rule = findRule(ruleKey);
 
     DefaultIssue issue = (DefaultIssue) new DefaultIssueBuilder()
-      .componentKey(resourceDto.getKey())
+      .componentKey(component.getKey())
       .projectKey(project.getKey())
       .line(line)
-      .message(message)
+      .message(!Strings.isNullOrEmpty(message) ? message : rule.getName())
       .severity(Objects.firstNonNull(severity, Severity.MAJOR))
       .effortToFix(effortToFix)
       .ruleKey(ruleKey)
       .reporter(UserSession.get().login())
       .build();
 
-    if (!authorizationDao.isAuthorizedComponentKey(resourceDto.getKey(), userSession.userId(), UserRole.USER)) {
-      // TODO throw unauthorized
-      throw new IllegalStateException("User does not have the required role");
-    }
-    if (!org.sonar.server.rule.Rule.MANUAL_REPOSITORY_KEY.equals(issue.ruleKey().repository())) {
-      throw new IllegalArgumentException("Issues can be created only on rules marked as 'manual': " + issue.ruleKey());
-    }
-
-    Rule rule = ruleFinder.findByKey(issue.ruleKey());
-    if (rule == null) {
-      throw new IllegalArgumentException("Unknown rule: " + issue.ruleKey());
-    }
-    if (Strings.isNullOrEmpty(issue.message())) {
-      issue.setMessage(rule.getName());
-    }
-
     Date now = new Date();
     issue.setCreationDate(now);
     issue.setUpdateDate(now);
     issueStorage.save(issue);
-    dryRunCache.reportResourceModification(resourceDto.getKey());
+    dryRunCache.reportResourceModification(component.getKey());
     return issue;
   }
 
+  private Rule findRule (RuleKey ruleKey) {
+    Rule rule = ruleFinder.findByKey(ruleKey);
+    if (rule == null) {
+      throw new IllegalArgumentException("Unknown rule: " + ruleKey);
+    }
+    return rule;
+  }
+
   public IssueQueryResult loadIssue(String issueKey) {
     IssueQueryResult result = finder.find(IssueQuery.builder().issueKeys(Arrays.asList(issueKey)).requiredRole(UserRole.USER).build());
     if (result.issues().size() != 1) {
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/SonarHome.java b/sonar-server/src/main/java/org/sonar/server/platform/SonarHome.java
deleted file mode 100644 (file)
index 2f1b7bc..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.platform;
-
-import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.CoreProperties;
-
-import java.io.File;
-
-/**
- * Search fo the Sonar installation directory in the following ordered steps :
- * <ol>
- * <li>system property SONAR_HOME</li>
- * <li>environment variable SONAR_HOME</li>
- * </ol>
- *
- * @since 2.12
- *
- * TODO Delete it as it's now useless (SONAR_HOME is set by Tomcat)
- */
-final class SonarHome {
-
-  private SonarHome() {
-    // only static methods
-  }
-
-  static Supplier<File> homeSupplier = Suppliers.memoize(new Supplier<File>() {
-    public File get() {
-      File home = locate();
-      System.setProperty(CoreProperties.SONAR_HOME, home.getAbsolutePath());
-      return home;
-    }
-  });
-
-  static File getHome() {
-    return homeSupplier.get();
-  }
-
-  static File locate() {
-    String value = System.getProperty(CoreProperties.SONAR_HOME);
-    if (StringUtils.isBlank(value)) {
-      value = System.getenv(CoreProperties.SONAR_HOME);
-    }
-
-    if (StringUtils.isBlank(value)) {
-      throw new IllegalStateException("The system property or env variable " + CoreProperties.SONAR_HOME + " is not set");
-    }
-
-    File dir = new File(value);
-    if (!dir.isDirectory() || !dir.exists()) {
-      throw new IllegalStateException(CoreProperties.SONAR_HOME + " is not valid: " + value + ". Please fix the env variable/system " +
-        "property " + CoreProperties.SONAR_HOME);
-    }
-    return dir;
-  }
-}
index cb2ad27e558babf90dc0b79fd8c11790504ef252..4b8d81f55b4738bb2a77bade60c72c096553cbdf 100644 (file)
@@ -35,11 +35,13 @@ public class QProfilesWs implements WebService {
     NewController controller = context.createController("api/qprofiles")
       .setDescription("Quality profiles management");
 
-    controller.createAction("restore_default")
+    NewAction restoreDefault = controller.createAction("restore_default")
       .setDescription("Restore default profiles")
       .setSince("4.4")
-      .setHandler(qProfileBackupWsHandler)
-      .createParam("language", "Restore default profiles for this language");
+      .setHandler(qProfileBackupWsHandler);
+    restoreDefault.createParam("language")
+      .setDescription("Restore default profiles for this language");
+
     controller.done();
   }
 }
index 9b93e8e1cdb183544e3cee9b8c694ac98cd21e33..fbd83aebf4c442b31b5675b51b42455b47f84466 100644 (file)
@@ -53,7 +53,7 @@ public class QProfilesWsTest {
 
     WebService.Action restoreProfiles = controller.action("restore_default");
     assertThat(restoreProfiles).isNotNull();
-    assertThat(restoreProfiles.handler()).isNotNull();
+    assertThat(restoreProfiles.handler()).isSameAs(qProfileBackupWsHandler);
     assertThat(restoreProfiles.since()).isEqualTo("4.4");
     assertThat(restoreProfiles.isPost()).isFalse();
     assertThat(restoreProfiles.isInternal()).isFalse();