From 4e6ce48288c10f14af1969cef727bb32d31c5942 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 30 Apr 2014 15:59:38 +0200 Subject: [PATCH] Fix quality flaws --- .../batch/bootstrap/JdbcDriverHolder.java | 7 +- .../sonar/batch/bootstrap/TaskContainer.java | 1 - .../sonar/batch/scan/ModuleScanContainer.java | 6 +- .../org/sonar/core/issue/IssueUpdater.java | 6 +- .../api/issue/internal/DefaultIssue.java | 2 +- .../org/sonar/server/issue/IssueService.java | 42 +++++------ .../org/sonar/server/platform/SonarHome.java | 75 ------------------- .../server/qualityprofile/ws/QProfilesWs.java | 8 +- .../qualityprofile/ws/QProfilesWsTest.java | 2 +- 9 files changed, 35 insertions(+), 114 deletions(-) delete mode 100644 sonar-server/src/main/java/org/sonar/server/platform/SonarHome.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java index 437fd62ff55..99f5f028978 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java @@ -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) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java index d823a0650cf..b328680a990 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TaskContainer.java @@ -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; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index 5180a498752..f4769d38316 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -19,12 +19,9 @@ */ 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.*; diff --git a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java index 9ac381eeffd..0edb74d1b0a 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java @@ -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; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java index e6a380a0080..d5a4f38d177 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java @@ -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; } diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java index 5e026586f1a..c15c3e10c1e 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java @@ -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 index 2f1b7bcfafd..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/platform/SonarHome.java +++ /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 : - *
    - *
  1. system property SONAR_HOME
  2. - *
  3. environment variable SONAR_HOME
  4. - *
- * - * @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 homeSupplier = Suppliers.memoize(new Supplier() { - 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; - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java index cb2ad27e558..4b8d81f55b4 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java @@ -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(); } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index 9b93e8e1cdb..fbd83aebf4c 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -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(); -- 2.39.5