]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5417 Get active rules using batch protocol
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 18 Jul 2014 10:18:39 +0000 (12:18 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 18 Jul 2014 13:43:39 +0000 (15:43 +0200)
38 files changed:
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRepositoryExporterTest.java
sonar-batch-protocol/pom.xml
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java [new file with mode: 0644]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java [deleted file]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfile.java [new file with mode: 0644]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java [deleted file]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/package-info.java [new file with mode: 0644]
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java
sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/languages/DeprecatedLanguagesReferential.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java
sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java
sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultAnalyzerContext.java
sonar-batch/src/main/java/org/sonar/batch/scan2/ModuleScanContainer.java
sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java [deleted file]
sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java [deleted file]
sonar-batch/src/test/java/org/sonar/batch/scan/LanguageVerifierTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleDto.java
sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java

index 9538f515703515d6f12529cd8536325e82c295fb..3ecb31a6e4fa7965f034f53ff0c3a1628c5add9c 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.server.search.action.IndexAction;
 import org.sonar.server.search.action.KeyIndexAction;
 
 import javax.annotation.CheckForNull;
+
 import java.sql.Timestamp;
 import java.util.Date;
 import java.util.List;
@@ -58,7 +59,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
   private static final String ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED = "ActiveRuleParam is already persisted";
   private static final String PARAMETER_NAME_CANNOT_BE_NULL = "ParameterName cannot be null";
 
-  //TODO remove once key is finalized (used only to get id for SQL statement)
+  // TODO remove once key is finalized (used only to get id for SQL statement)
   private final RuleDao ruleDao;
   private final QualityProfileDao profileDao;
 
@@ -98,7 +99,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
     ActiveRuleDto activeRule = mapper(session).selectById(activeRuleId);
     if (activeRule != null) {
       QualityProfileDto profile = profileDao.getById(activeRule.getProfileId(), session);
-      RuleDto rule = ruleDao.getById(session, activeRule.getRulId());
+      RuleDto rule = ruleDao.getById(session, activeRule.getRuleId());
       if (profile != null && rule != null) {
         activeRule.setKey(ActiveRuleKey.of(profile.getKey(), rule.getKey()));
         return activeRule;
@@ -115,7 +116,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
   @Override
   protected ActiveRuleDto doInsert(DbSession session, ActiveRuleDto item) {
     Preconditions.checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
-    Preconditions.checkArgument(item.getRulId() != null, RULE_IS_NOT_PERSISTED);
+    Preconditions.checkArgument(item.getRuleId() != null, RULE_IS_NOT_PERSISTED);
     Preconditions.checkArgument(item.getId() == null, ACTIVE_RULE_IS_ALREADY_PERSISTED);
     mapper(session).insert(item);
     return item;
@@ -124,7 +125,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
   @Override
   protected ActiveRuleDto doUpdate(DbSession session, ActiveRuleDto item) {
     Preconditions.checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
-    Preconditions.checkArgument(item.getRulId() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
+    Preconditions.checkArgument(item.getRuleId() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
     Preconditions.checkArgument(item.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
     mapper(session).update(item);
     return item;
@@ -172,7 +173,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
   }
 
   public void removeParamByKeyAndName(DbSession session, ActiveRuleKey key, String param) {
-    //TODO SQL rewrite to delete by key
+    // TODO SQL rewrite to delete by key
     ActiveRuleDto activeRule = getNullableByKey(session, key);
     if (activeRule != null) {
       ActiveRuleParamDto activeRuleParam = mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), param);
@@ -222,7 +223,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
     Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
     Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL);
     ActiveRuleDto activeRule = getNullableByKey(session, key);
-    if (activeRule!=null) {
+    if (activeRule != null) {
       return mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), name);
     }
     return null;
index d4bf7a3ecb839e8ee43341f2af0784805ff48182..cdbc9cad7aa7c3fc27ceccadb50fa68aa13e13d8 100644 (file)
@@ -50,7 +50,11 @@ import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.Fail.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public class QProfileRepositoryExporterTest {
@@ -105,7 +109,7 @@ public class QProfileRepositoryExporterTest {
 
     ArgumentCaptor<ActiveRuleDto> activeRuleArgument = ArgumentCaptor.forClass(ActiveRuleDto.class);
     verify(activeRuleDao).insert(activeRuleArgument.capture(), eq(session));
-    assertThat(activeRuleArgument.getValue().getRulId()).isEqualTo(10);
+    assertThat(activeRuleArgument.getValue().getRuleId()).isEqualTo(10);
     assertThat(activeRuleArgument.getValue().getSeverityString()).isEqualTo(Severity.BLOCKER);
 
     ArgumentCaptor<ActiveRuleParamDto> activeRuleParamArgument = ArgumentCaptor.forClass(ActiveRuleParamDto.class);
index b57fa36fb4f2086390e9c359c7ca27e227d2b662..e596ef0ab909a3fde0995955e39f171f48d9983b 100644 (file)
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.google.code.findbugs</groupId>
+      <artifactId>jsr305</artifactId>
+    </dependency>
 
     <!-- unit tests -->
     <dependency>
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java
new file mode 100644 (file)
index 0000000..144a34b
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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.batch.protocol.input;
+
+import javax.annotation.CheckForNull;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ActiveRule {
+  private final String repositoryKey, ruleKey;
+  private final String severity, internalKey, language;
+  private final Map<String, String> params = new HashMap<String, String>();
+
+  public ActiveRule(String repositoryKey, String ruleKey, String severity, String internalKey, String language) {
+    this.repositoryKey = repositoryKey;
+    this.ruleKey = ruleKey;
+    this.severity = severity;
+    this.internalKey = internalKey;
+    this.language = language;
+  }
+
+  public String repositoryKey() {
+    return repositoryKey;
+  }
+
+  public String ruleKey() {
+    return ruleKey;
+  }
+
+  public String severity() {
+    return severity;
+  }
+
+  public String language() {
+    return language;
+  }
+
+  @CheckForNull
+  public String param(String key) {
+    return params.get(key);
+  }
+
+  public ActiveRule addParam(String key, String value) {
+    params.put(key, value);
+    return this;
+  }
+
+  public Map<String, String> params() {
+    return params;
+  }
+
+  public String internalKey() {
+    return internalKey;
+  }
+}
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Language.java
deleted file mode 100644 (file)
index 8e1bd46..0000000
+++ /dev/null
@@ -1,42 +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.batch.protocol.input;
-
-public class Language {
-
-  private String key;
-
-  private String name;
-
-  private String[] suffixes;
-
-  public String key() {
-    return key;
-  }
-
-  public String name() {
-    return name;
-  }
-
-  public String[] suffixes() {
-    return suffixes;
-  }
-
-}
index bff1a434069a449600cf99f0f9aa344d05d4c413..c813b6c3f60e9fb67e29bfaf5cd8bb87fff53e4d 100644 (file)
@@ -24,21 +24,38 @@ import com.google.gson.Gson;
 import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 
 public class ProjectReferentials {
 
   private long timestamp;
-  private Collection<Language> languages = new ArrayList<Language>();
   private Collection<Metric> metrics = new ArrayList<Metric>();
-
-  public Collection<Language> languages() {
-    return languages;
-  }
+  private Map<String, QProfile> qprofilesByLanguage = new HashMap<String, QProfile>();
+  private Collection<ActiveRule> activeRules = new ArrayList<ActiveRule>();
 
   public Collection<Metric> metrics() {
     return metrics;
   }
 
+  public Collection<QProfile> qProfiles() {
+    return qprofilesByLanguage.values();
+  }
+
+  public ProjectReferentials addQProfile(QProfile qProfile) {
+    qprofilesByLanguage.put(qProfile.language(), qProfile);
+    return this;
+  }
+
+  public Collection<ActiveRule> activeRules() {
+    return activeRules;
+  }
+
+  public ProjectReferentials addActiveRule(ActiveRule activeRule) {
+    activeRules.add(activeRule);
+    return this;
+  }
+
   public long timestamp() {
     return timestamp;
   }
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfile.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfile.java
new file mode 100644 (file)
index 0000000..4c6b019
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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.batch.protocol.input;
+
+import java.util.Date;
+
+public class QProfile {
+
+  private final String key, name, language;
+  private final Date rulesUpdatedAt;
+
+  public QProfile(String key, String name, String language, Date rulesUpdatedAt) {
+    this.key = key;
+    this.name = name;
+    this.language = language;
+    this.rulesUpdatedAt = rulesUpdatedAt;
+  }
+
+  public String key() {
+    return key;
+  }
+
+  public String name() {
+    return name;
+  }
+
+  public String language() {
+    return language;
+  }
+
+  public Date rulesUpdatedAt() {
+    return rulesUpdatedAt;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    QProfile qProfile = (QProfile) o;
+    return key.equals(qProfile.key);
+  }
+
+  @Override
+  public int hashCode() {
+    return key.hashCode();
+  }
+}
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/QProfiles.java
deleted file mode 100644 (file)
index 66f4edf..0000000
+++ /dev/null
@@ -1,33 +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.batch.protocol.input;
-
-import java.util.Map;
-
-public class QProfiles {
-
-  public static class QProfile {
-
-    private String key, name, language;
-  }
-
-  private Map<String, QProfile> byLanguage;
-
-}
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/package-info.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/package-info.java
new file mode 100644 (file)
index 0000000..a545fa5
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.batch.protocol.input;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
index e4d5142f10db7be5249f4dd1ccecdd668cd86017..cb5bc33d23e01cf2227db1f599057df059edff9c 100644 (file)
@@ -24,23 +24,32 @@ import org.junit.Test;
 import org.skyscreamer.jsonassert.JSONAssert;
 
 import java.io.StringReader;
+import java.text.SimpleDateFormat;
 
 import static org.fest.assertions.Assertions.assertThat;
 
 public class ProjectReferentialsTest {
 
   @Test
-  public void testToJson() throws JSONException {
+  public void testToJson() throws Exception {
     ProjectReferentials ref = new ProjectReferentials();
     ref.metrics().add(new Metric("ncloc", "INT"));
+    ref.addQProfile(new QProfile("squid-java", "Java", "java", new SimpleDateFormat("dd/MM/yyyy").parse("14/03/1984")));
 
     System.out.println(ref.toJson());
-    JSONAssert.assertEquals("{timestamp:0,languages:[],metrics:[{key:ncloc,valueType:INT}]}", ref.toJson(), true);
+    JSONAssert
+      .assertEquals(
+        "{timestamp:0,metrics:[{key:ncloc,valueType:INT}],"
+          + "qprofilesByLanguage:{java:{key:\"squid-java\","
+          + "name:Java,"
+          + "language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
+          + "activeRules:[]}",
+        ref.toJson(), true);
   }
 
   @Test
   public void testFromJson() throws JSONException {
-    ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{timestamp:1,languages:[],metrics:[{key:ncloc,valueType:INT}]}"));
+    ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{timestamp:1,metrics:[{key:ncloc,valueType:INT}]}"));
 
     assertThat(ref.timestamp()).isEqualTo(1);
     Metric metric = ref.metrics().iterator().next();
index 7f89a3b0eaf7c347eb89244981a2625e139498e1..daa6210cd1f43dbbfd076a6315e80241f525e217 100644 (file)
@@ -25,25 +25,19 @@ import org.sonar.api.Plugin;
 import org.sonar.api.config.EmailSettings;
 import org.sonar.api.platform.ComponentContainer;
 import org.sonar.api.platform.PluginMetadata;
-import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.utils.Durations;
 import org.sonar.api.utils.HttpDownloader;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.UriReader;
 import org.sonar.api.utils.internal.TempFolderCleaner;
-import org.sonar.batch.components.PastMeasuresLoader;
 import org.sonar.batch.components.PastSnapshotFinder;
 import org.sonar.batch.components.PastSnapshotFinderByDate;
 import org.sonar.batch.components.PastSnapshotFinderByDays;
 import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis;
 import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion;
 import org.sonar.batch.components.PastSnapshotFinderByVersion;
-import org.sonar.batch.debt.DebtModelProvider;
 import org.sonar.batch.referential.DefaultProjectReferentialsLoader;
 import org.sonar.batch.referential.ProjectReferentialsLoader;
-import org.sonar.batch.rule.RulesProvider;
-import org.sonar.batch.rules.DefaultQProfileReferential;
-import org.sonar.batch.rules.QProfilesReferential;
 import org.sonar.batch.settings.DefaultSettingsReferential;
 import org.sonar.batch.settings.SettingsReferential;
 import org.sonar.core.cluster.NullQueue;
@@ -110,20 +104,14 @@ public class BootstrapContainer extends ComponentContainer {
       UriReader.class,
       new FileCacheProvider(),
       System2.INSTANCE);
-    if (getComponentByType(ProjectReferentialsLoader.class) == null) {
-      add(DefaultProjectReferentialsLoader.class);
-    }
     if (getComponentByType(SettingsReferential.class) == null) {
       add(DefaultSettingsReferential.class);
     }
     if (getComponentByType(PluginsReferential.class) == null) {
       add(DefaultPluginsReferential.class);
     }
-    if (getComponentByType(RuleFinder.class) == null) {
-      add(CacheRuleFinder.class);
-    }
-    if (getComponentByType(QProfilesReferential.class) == null) {
-      add(DefaultQProfileReferential.class);
+    if (getComponentByType(ProjectReferentialsLoader.class) == null) {
+      add(DefaultProjectReferentialsLoader.class);
     }
   }
 
@@ -141,7 +129,8 @@ public class BootstrapContainer extends ComponentContainer {
       JpaDatabaseSession.class,
       BatchDatabaseSessionFactory.class,
       DaoUtils.getDaoClasses(),
-      PurgeProfiler.class);
+      PurgeProfiler.class,
+      CacheRuleFinder.class);
   }
 
   /**
@@ -161,11 +150,8 @@ public class BootstrapContainer extends ComponentContainer {
       PastSnapshotFinderByPreviousAnalysis.class,
       PastSnapshotFinderByVersion.class,
       PastSnapshotFinderByPreviousVersion.class,
-      PastMeasuresLoader.class,
       PastSnapshotFinder.class,
-      Durations.class,
-      new DebtModelProvider(),
-      new RulesProvider());
+      Durations.class);
   }
 
   @Override
diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/languages/DefaultLanguagesReferential.java
new file mode 100644 (file)
index 0000000..e9099c1
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.batch.languages;
+
+import org.sonar.api.resources.Languages;
+
+import javax.annotation.CheckForNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Languages referential using {@link Languages}
+ * @since 4.4
+ */
+public class DefaultLanguagesReferential implements LanguagesReferential {
+
+  private Languages languages;
+
+  public DefaultLanguagesReferential(Languages languages) {
+    this.languages = languages;
+  }
+
+  /**
+   * Get language.
+   */
+  @CheckForNull
+  public Language get(String languageKey) {
+    org.sonar.api.resources.Language language = languages.get(languageKey);
+    return language != null ? new Language(language.getKey(), language.getName(), language.getFileSuffixes()) : null;
+  }
+
+  /**
+   * Get list of all supported languages.
+   */
+  public Collection<Language> all() {
+    org.sonar.api.resources.Language[] all = languages.all();
+    Collection<Language> result = new ArrayList<Language>(all.length);
+    for (org.sonar.api.resources.Language language : all) {
+      result.add(new Language(language.getKey(), language.getName(), language.getFileSuffixes()));
+    }
+    return result;
+  }
+
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/languages/DeprecatedLanguagesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/languages/DeprecatedLanguagesReferential.java
deleted file mode 100644 (file)
index 23591b1..0000000
+++ /dev/null
@@ -1,62 +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.batch.languages;
-
-import org.sonar.api.resources.Languages;
-
-import javax.annotation.CheckForNull;
-
-import java.util.ArrayList;
-import java.util.Collection;
-
-/**
- * Languages referential using {@link Languages}
- * @since 4.4
- */
-public class DeprecatedLanguagesReferential implements LanguagesReferential {
-
-  private Languages languages;
-
-  public DeprecatedLanguagesReferential(Languages languages) {
-    this.languages = languages;
-  }
-
-  /**
-   * Get language.
-   */
-  @CheckForNull
-  public Language get(String languageKey) {
-    org.sonar.api.resources.Language language = languages.get(languageKey);
-    return language != null ? new Language(language.getKey(), language.getName(), language.getFileSuffixes()) : null;
-  }
-
-  /**
-   * Get list of all supported languages.
-   */
-  public Collection<Language> all() {
-    org.sonar.api.resources.Language[] all = languages.all();
-    Collection<Language> result = new ArrayList<Language>(all.length);
-    for (org.sonar.api.resources.Language language : all) {
-      result.add(new Language(language.getKey(), language.getName(), language.getFileSuffixes()));
-    }
-    return result;
-  }
-
-}
index 13d43e7846b01e9fa405a5353a9654dae5ea4800..8aa68596d0738697f706b7ee6efc8614b73e4ce7 100644 (file)
  */
 package org.sonar.batch.mediumtest;
 
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-import com.google.common.collect.Maps;
 import org.apache.commons.io.IOUtils;
 import org.sonar.api.SonarPlugin;
 import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
 import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.batch.debt.internal.DefaultDebtModel;
 import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
-import org.sonar.api.batch.rule.internal.RulesBuilder;
+import org.sonar.api.config.Settings;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.platform.PluginMetadata;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleQuery;
+import org.sonar.api.resources.Languages;
 import org.sonar.batch.bootstrap.PluginsReferential;
 import org.sonar.batch.bootstrapper.Batch;
 import org.sonar.batch.bootstrapper.EnvironmentInformation;
-import org.sonar.batch.languages.Language;
-import org.sonar.batch.languages.LanguagesReferential;
+import org.sonar.batch.protocol.input.ActiveRule;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.batch.referential.ProjectReferentialsLoader;
-import org.sonar.batch.rule.QProfile;
-import org.sonar.batch.rules.QProfilesReferential;
 import org.sonar.batch.scan.filesystem.InputFileCache;
 import org.sonar.batch.scan2.AnalyzerIssueCache;
 import org.sonar.batch.scan2.AnalyzerMeasureCache;
@@ -58,7 +49,6 @@ import org.sonar.core.plugins.RemotePlugin;
 import java.io.File;
 import java.io.FileReader;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -78,13 +68,7 @@ public class AnalyzerMediumTester {
     private final FakeProjectReferentialsLoader refProvider = new FakeProjectReferentialsLoader();
     private final FakeSettingsReferential settingsReferential = new FakeSettingsReferential();
     private final FackPluginsReferential pluginsReferential = new FackPluginsReferential();
-    private final FakeRuleFinder ruleFinder = new FakeRuleFinder();
-    private final FakeQProfileReferential qProfileReferential = new FakeQProfileReferential();
-    private final FakeLanguageReferential languageReferential = new FakeLanguageReferential();
     private final Map<String, String> bootstrapProperties = new HashMap<String, String>();
-    private final RulesBuilder rulesBuilder = new RulesBuilder();
-    private final ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder();
-    private int metricId = 1;
 
     public AnalyzerMediumTester build() {
       return new AnalyzerMediumTester(this);
@@ -109,12 +93,11 @@ public class AnalyzerMediumTester {
 
     public AnalyzerMediumTesterBuilder registerMetric(Metric<?> metric) {
       refProvider.add(metric);
-      metricId++;
       return this;
     }
 
     public AnalyzerMediumTesterBuilder addQProfile(String language, String name) {
-      qProfileReferential.add(new QProfile().setKey(name).setName(name).setLanguage(language).setRulesUpdatedAt(new Date()));
+      refProvider.addQProfile(language, name);
       return this;
     }
 
@@ -124,24 +107,13 @@ public class AnalyzerMediumTester {
       return this;
     }
 
-    public AnalyzerMediumTesterBuilder registerLanguage(org.sonar.api.resources.Language... languages) {
-      languageReferential.register(languages);
-      return this;
-    }
-
     public AnalyzerMediumTesterBuilder bootstrapProperties(Map<String, String> props) {
       bootstrapProperties.putAll(props);
       return this;
     }
 
-    public AnalyzerMediumTesterBuilder activateRule(RuleKey key) {
-      rulesBuilder.add(key);
-      activeRulesBuilder.create(key).activate();
-      return this;
-    }
-
-    public AnalyzerMediumTesterBuilder registerInactiveRule(RuleKey key) {
-      rulesBuilder.add(key);
+    public AnalyzerMediumTesterBuilder activateRule(ActiveRule activeRule) {
+      refProvider.addActiveRule(activeRule);
       return this;
     }
 
@@ -163,12 +135,7 @@ public class AnalyzerMediumTester {
         builder.settingsReferential,
         builder.pluginsReferential,
         builder.refProvider,
-        builder.ruleFinder,
-        builder.qProfileReferential,
-        builder.rulesBuilder.build(),
-        builder.activeRulesBuilder.build(),
-        new DefaultDebtModel(),
-        builder.languageReferential)
+        new DefaultDebtModel())
       .setBootstrapProperties(builder.bootstrapProperties)
       .build();
   }
@@ -266,14 +233,24 @@ public class AnalyzerMediumTester {
     private ProjectReferentials ref = new ProjectReferentials();
 
     @Override
-    public ProjectReferentials load(String projectKey) {
+    public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) {
       return ref;
     }
 
+    public FakeProjectReferentialsLoader addQProfile(String language, String name) {
+      ref.addQProfile(new org.sonar.batch.protocol.input.QProfile(name, name, language, new Date()));
+      return this;
+    }
+
     public FakeProjectReferentialsLoader add(Metric metric) {
       ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metric.key(), metric.getType().name()));
       return this;
     }
+
+    public FakeProjectReferentialsLoader addActiveRule(ActiveRule activeRule) {
+      ref.addActiveRule(activeRule);
+      return this;
+    }
   }
 
   private static class FakeSettingsReferential implements SettingsReferential {
@@ -328,76 +305,4 @@ public class AnalyzerMediumTester {
 
   }
 
-  private static class FakeRuleFinder implements RuleFinder {
-    private BiMap<Integer, Rule> rulesById = HashBiMap.create();
-    private Map<String, Map<String, Rule>> rulesByRepoKeyAndRuleKey = Maps.newHashMap();
-
-    @Override
-    public Rule findById(int ruleId) {
-      return rulesById.get(ruleId);
-    }
-
-    @Override
-    public Rule findByKey(String repositoryKey, String ruleKey) {
-      Map<String, Rule> repository = rulesByRepoKeyAndRuleKey.get(repositoryKey);
-      return repository != null ? repository.get(ruleKey) : null;
-    }
-
-    @Override
-    public Rule findByKey(RuleKey key) {
-      return findByKey(key.repository(), key.rule());
-    }
-
-    @Override
-    public Rule find(RuleQuery query) {
-      throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public Collection<Rule> findAll(RuleQuery query) {
-      throw new UnsupportedOperationException();
-    }
-  }
-
-  private static class FakeQProfileReferential implements QProfilesReferential {
-
-    private Map<String, Map<String, QProfile>> profiles = new HashMap<String, Map<String, QProfile>>();
-
-    @Override
-    public QProfile get(String language, String name) {
-      return profiles.get(language).get(name);
-    }
-
-    public void add(QProfile qprofile) {
-      if (!profiles.containsKey(qprofile.getLanguage())) {
-        profiles.put(qprofile.getLanguage(), new HashMap<String, QProfile>());
-      }
-      profiles.get(qprofile.getLanguage()).put(qprofile.getName(), qprofile);
-    }
-
-  }
-
-  private static class FakeLanguageReferential implements LanguagesReferential {
-
-    private Map<String, Language> languages = new HashMap<String, Language>();
-
-    public FakeLanguageReferential register(org.sonar.api.resources.Language... languages) {
-      for (org.sonar.api.resources.Language language : languages) {
-        this.languages.put(language.getKey(), new Language(language.getKey(), language.getName(), language.getFileSuffixes()));
-      }
-      return this;
-    }
-
-    @Override
-    public Language get(String languageKey) {
-      return languages.get(languageKey);
-    }
-
-    @Override
-    public Collection<Language> all() {
-      return languages.values();
-    }
-
-  }
-
 }
index 9f6bb3275947a5029af9ff22bdf0541932aed764..d4f15f1fa3f4540eddf9b0eab9b34c3e04cca254 100644 (file)
  */
 package org.sonar.batch.referential;
 
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.config.Settings;
 import org.sonar.api.measures.Metric;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.RuleParam;
+import org.sonar.api.utils.MessageException;
+import org.sonar.batch.protocol.input.ActiveRule;
 import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.batch.protocol.input.QProfile;
+import org.sonar.batch.rule.ModuleQProfiles;
+import org.sonar.core.UtcDateUtils;
+import org.sonar.core.qualityprofile.db.ActiveRuleDao;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDao;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
 import org.sonar.jpa.session.DatabaseSessionFactory;
 
+import javax.annotation.CheckForNull;
+
 import java.util.Collection;
 
+/**
+ * TODO This is currently implemented by accessing DB but should be replaced by WS call
+ */
 public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoader {
 
   private static final String ENABLED = "enabled";
-  private DatabaseSessionFactory sessionFactory;
 
-  protected Collection<Metric> doFindAll() {
-    return sessionFactory.getSession().getResults(Metric.class, ENABLED, true);
-  }
+  private final DatabaseSessionFactory sessionFactory;
+  private final QualityProfileDao qualityProfileDao;
+  private final ActiveRuleDao activeRuleDao;
+  private final RuleFinder ruleFinder;
 
-  public DefaultProjectReferentialsLoader(DatabaseSessionFactory sessionFactory) {
+  public DefaultProjectReferentialsLoader(DatabaseSessionFactory sessionFactory, QualityProfileDao qualityProfileDao,
+    ActiveRuleDao activeRuleDao, RuleFinder ruleFinder) {
     this.sessionFactory = sessionFactory;
+    this.qualityProfileDao = qualityProfileDao;
+    this.activeRuleDao = activeRuleDao;
+    this.ruleFinder = ruleFinder;
   }
 
-  public ProjectReferentials load(String projectKey) {
+  @Override
+  public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) {
     ProjectReferentials ref = new ProjectReferentials();
     for (Metric m : sessionFactory.getSession().getResults(Metric.class, ENABLED, true)) {
       ref.metrics().add(new org.sonar.batch.protocol.input.Metric(m.getKey(), m.getType().name()));
     }
+
+    String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP);
+
+    for (Language language : languages.all()) {
+      org.sonar.batch.protocol.input.QProfile profile = null;
+      if (StringUtils.isNotBlank(defaultName)) {
+        profile = loadDefaultQProfile(defaultName, language.getKey());
+      }
+      if (profile == null) {
+        profile = loadQProfile(settings, language.getKey());
+      }
+      if (profile != null) {
+        ref.addQProfile(profile);
+      }
+    }
+
+    for (QProfile qProfile : ref.qProfiles()) {
+      ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId = ArrayListMultimap.create();
+      for (ActiveRuleParamDto dto : activeRuleDao.selectParamsByProfileKey(qProfile.key())) {
+        paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto);
+      }
+
+      for (ActiveRuleDto activeDto : activeRuleDao.selectByProfileKey(qProfile.key())) {
+        Rule rule = ruleFinder.findById(activeDto.getRuleId());
+        if (rule != null) {
+          String internalKey;
+          Rule template = rule.getTemplate();
+          if (template != null) {
+            internalKey = template.getConfigKey();
+          } else {
+            internalKey = rule.getConfigKey();
+          }
+          ActiveRule activeRule = new ActiveRule(rule.ruleKey().repository(), rule.ruleKey().rule(), activeDto.getSeverityString(), internalKey, rule.getLanguage());
+
+          // load parameter values
+          for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
+            activeRule.params().put(paramDto.getKey(), paramDto.getValue());
+          }
+
+          // load default values
+          for (RuleParam param : rule.getParams()) {
+            if (!activeRule.params().containsKey(param.getKey())) {
+              activeRule.params().put(param.getKey(), param.getDefaultValue());
+            }
+          }
+
+          ref.addActiveRule(activeRule);
+        }
+      }
+    }
+
     return ref;
   }
+
+  @CheckForNull
+  private QProfile loadQProfile(Settings settings, String language) {
+    String profileName = settings.getString("sonar.profile." + language);
+    if (profileName != null) {
+      QProfile dto = get(language, profileName);
+      if (dto == null) {
+        throw MessageException.of(String.format("Quality profile not found : '%s' on language '%s'", profileName, language));
+      }
+      return dto;
+    }
+    return null;
+  }
+
+  @CheckForNull
+  private QProfile loadDefaultQProfile(String profileName, String language) {
+    return get(language, profileName);
+  }
+
+  protected Collection<Metric> doFindAll() {
+    return sessionFactory.getSession().getResults(Metric.class, ENABLED, true);
+  }
+
+  public QProfile get(String language, String name) {
+    QualityProfileDto dto = qualityProfileDao.getByNameAndLanguage(name, language);
+    if (dto == null) {
+      return null;
+    }
+    return new org.sonar.batch.protocol.input.QProfile(dto.getKey(), dto.getName(), dto.getLanguage(), UtcDateUtils.parseDateTime(dto.getRulesUpdatedAt()));
+  }
 }
index 3dff3784414848e13eea1f07392d804e4206b8e9..f34e358ceecdde639e86630811c54a1dc927b744 100644 (file)
  */
 package org.sonar.batch.referential;
 
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Languages;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 
 public interface ProjectReferentialsLoader {
 
-  ProjectReferentials load(String projectKey);
+  ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages);
+
 }
diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsProvider.java
new file mode 100644 (file)
index 0000000..75a063e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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.batch.referential;
+
+import org.picocontainer.injectors.ProviderAdapter;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Languages;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+
+public class ProjectReferentialsProvider extends ProviderAdapter {
+
+  public ProjectReferentials provide(ProjectReferentialsLoader loader, ProjectReactor reactor, Settings settings, Languages languages) {
+    return loader.load(reactor, settings, languages);
+  }
+}
index a4fec80b1f57a170c43a5293a070f7064afac417..9b6b009d3df86b2be852bbd81c93a6fe21c7a515 100644 (file)
  */
 package org.sonar.batch.rule;
 
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ListMultimap;
 import org.picocontainer.injectors.ProviderAdapter;
 import org.sonar.api.batch.rule.ActiveRules;
 import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
 import org.sonar.api.batch.rule.internal.NewActiveRule;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleParam;
-import org.sonar.core.qualityprofile.db.ActiveRuleDao;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.batch.protocol.input.ActiveRule;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+
+import java.util.Map.Entry;
 
 /**
  * Loads the rules that are activated on the Quality profiles
@@ -40,48 +37,27 @@ public class ActiveRulesProvider extends ProviderAdapter {
 
   private ActiveRules singleton = null;
 
-  public ActiveRules provide(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) {
+  public ActiveRules provide(ProjectReferentials ref) {
     if (singleton == null) {
-      singleton = load(qProfiles, dao, ruleFinder);
+      singleton = load(ref);
     }
     return singleton;
   }
 
-  private ActiveRules load(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) {
+  private ActiveRules load(ProjectReferentials ref) {
     ActiveRulesBuilder builder = new ActiveRulesBuilder();
-    for (QProfile qProfile : qProfiles.findAll()) {
-      ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId = ArrayListMultimap.create();
-      for (ActiveRuleParamDto dto : dao.selectParamsByProfileKey(qProfile.getKey())) {
-        paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto);
-      }
-
-      for (ActiveRuleDto activeDto : dao.selectByProfileKey(qProfile.getKey())) {
-        Rule rule = ruleFinder.findById(activeDto.getRulId());
-        if (rule != null) {
-          NewActiveRule newActiveRule = builder.create(rule.ruleKey());
-          newActiveRule.setSeverity(activeDto.getSeverityString());
-          newActiveRule.setLanguage(rule.getLanguage());
-          Rule template = rule.getTemplate();
-          if (template != null) {
-            newActiveRule.setInternalKey(template.getConfigKey());
-          } else {
-            newActiveRule.setInternalKey(rule.getConfigKey());
-          }
+    for (ActiveRule activeRule : ref.activeRules()) {
+      NewActiveRule newActiveRule = builder.create(RuleKey.of(activeRule.repositoryKey(), activeRule.ruleKey()));
+      newActiveRule.setSeverity(activeRule.severity());
+      newActiveRule.setLanguage(activeRule.language());
+      newActiveRule.setInternalKey(activeRule.internalKey());
 
-          // load parameter values
-          for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
-            newActiveRule.setParam(paramDto.getKey(), paramDto.getValue());
-          }
-
-          // load default values
-          for (RuleParam param : rule.getParams()) {
-            if (!newActiveRule.params().containsKey(param.getKey())) {
-              newActiveRule.setParam(param.getKey(), param.getDefaultValue());
-            }
-          }
-          newActiveRule.activate();
-        }
+      // load parameters
+      for (Entry<String, String> param : activeRule.params().entrySet()) {
+        newActiveRule.setParam(param.getKey(), param.getValue());
       }
+
+      newActiveRule.activate();
     }
     return builder.build();
   }
index 9bc04bd9c8a525cb36247af1ce424b55a0d85af1..dba6cd74a9b97939cbfce5202673abb85be63793 100644 (file)
  */
 package org.sonar.batch.rule;
 
-import org.sonar.batch.languages.Language;
-
 import com.google.common.collect.ImmutableMap;
-import org.apache.commons.lang.StringUtils;
 import org.sonar.api.BatchComponent;
-import org.sonar.api.config.Settings;
-import org.sonar.api.utils.MessageException;
-import org.sonar.batch.languages.LanguagesReferential;
-import org.sonar.batch.rules.QProfilesReferential;
+import org.sonar.batch.protocol.input.ProjectReferentials;
 
 import javax.annotation.CheckForNull;
 
@@ -40,46 +34,18 @@ import java.util.Map;
 public class ModuleQProfiles implements BatchComponent {
 
   public static final String SONAR_PROFILE_PROP = "sonar.profile";
-
   private final Map<String, QProfile> byLanguage;
 
-  public ModuleQProfiles(Settings settings, LanguagesReferential languages, QProfilesReferential qProfileRef) {
+  public ModuleQProfiles(ProjectReferentials ref) {
     ImmutableMap.Builder<String, QProfile> builder = ImmutableMap.builder();
-    String defaultName = settings.getString(SONAR_PROFILE_PROP);
 
-    for (Language language : languages.all()) {
-      QProfile profile = null;
-      if (StringUtils.isNotBlank(defaultName)) {
-        profile = loadDefaultQProfile(qProfileRef, defaultName, language.key());
-      }
-      if (profile == null) {
-        profile = loadQProfile(qProfileRef, settings, language.key());
-      }
-      if (profile != null) {
-        builder.put(profile.getLanguage(), profile);
-      }
+    for (org.sonar.batch.protocol.input.QProfile qProfile : ref.qProfiles()) {
+      builder.put(qProfile.language(),
+        new QProfile().setKey(qProfile.key()).setName(qProfile.name()).setLanguage(qProfile.language()).setRulesUpdatedAt(qProfile.rulesUpdatedAt()));
     }
     byLanguage = builder.build();
   }
 
-  @CheckForNull
-  private QProfile loadQProfile(QProfilesReferential qProfileRef, Settings settings, String language) {
-    String profileName = settings.getString("sonar.profile." + language);
-    if (profileName != null) {
-      QProfile dto = qProfileRef.get(language, profileName);
-      if (dto == null) {
-        throw MessageException.of(String.format("Quality profile not found : '%s' on language '%s'", profileName, language));
-      }
-      return dto;
-    }
-    return null;
-  }
-
-  @CheckForNull
-  private QProfile loadDefaultQProfile(QProfilesReferential qProfileRef, String profileName, String language) {
-    return qProfileRef.get(language, profileName);
-  }
-
   public Collection<QProfile> findAll() {
     return byLanguage.values();
   }
diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java b/sonar-batch/src/main/java/org/sonar/batch/rules/DefaultQProfileReferential.java
deleted file mode 100644 (file)
index 8aee397..0000000
+++ /dev/null
@@ -1,52 +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.batch.rules;
-
-import org.sonar.batch.rule.QProfile;
-import org.sonar.core.UtcDateUtils;
-import org.sonar.core.qualityprofile.db.QualityProfileDao;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-
-/**
- * @since 4.4
- */
-public class DefaultQProfileReferential implements QProfilesReferential {
-
-  private QualityProfileDao qualityProfileDao;
-
-  public DefaultQProfileReferential(QualityProfileDao qualityProfileDao) {
-    this.qualityProfileDao = qualityProfileDao;
-  }
-
-  @Override
-  public QProfile get(String language, String name) {
-    QualityProfileDto dto = qualityProfileDao.getByNameAndLanguage(name, language);
-    if (dto == null) {
-      return null;
-    }
-    QProfile profile = new QProfile();
-    profile.setKey(dto.getKey());
-    profile.setName(dto.getName());
-    profile.setLanguage(dto.getLanguage());
-    profile.setRulesUpdatedAt(UtcDateUtils.parseDateTime(dto.getRulesUpdatedAt()));
-    return profile;
-  }
-
-}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java b/sonar-batch/src/main/java/org/sonar/batch/rules/QProfilesReferential.java
deleted file mode 100644 (file)
index 5d50cf3..0000000
+++ /dev/null
@@ -1,40 +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.batch.rules;
-
-import org.sonar.batch.rule.QProfile;
-
-import org.sonar.api.BatchComponent;
-
-import javax.annotation.CheckForNull;
-
-/**
- * Quality profiles referential
- * @since 4.4
- */
-public interface QProfilesReferential extends BatchComponent {
-
-  /**
-   * Get quality profile
-   */
-  @CheckForNull
-  QProfile get(String language, String name);
-
-}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/rules/package-info.java
deleted file mode 100644 (file)
index 1522091..0000000
+++ /dev/null
@@ -1,21 +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.
- */
-@javax.annotation.ParametersAreNonnullByDefault
-package org.sonar.batch.rules;
index e7b22e32a1b6469e31221a5dc04a5e12fe152817..911aede44cc44285a3d4f75806e573a00949944c 100644 (file)
@@ -39,7 +39,9 @@ import org.sonar.batch.bootstrap.ExtensionInstaller;
 import org.sonar.batch.bootstrap.ExtensionMatcher;
 import org.sonar.batch.bootstrap.ExtensionUtils;
 import org.sonar.batch.bootstrap.MetricProvider;
+import org.sonar.batch.components.PastMeasuresLoader;
 import org.sonar.batch.components.PeriodsDefinition;
+import org.sonar.batch.debt.DebtModelProvider;
 import org.sonar.batch.debt.IssueChangelogDebtCalculator;
 import org.sonar.batch.index.Caches;
 import org.sonar.batch.index.ComponentDataCache;
@@ -60,10 +62,11 @@ import org.sonar.batch.issue.DeprecatedViolations;
 import org.sonar.batch.issue.IssueCache;
 import org.sonar.batch.issue.IssuePersister;
 import org.sonar.batch.issue.ScanIssueStorage;
-import org.sonar.batch.languages.DeprecatedLanguagesReferential;
+import org.sonar.batch.languages.DefaultLanguagesReferential;
 import org.sonar.batch.phases.GraphPersister;
 import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
-import org.sonar.batch.referential.ProjectReferentialsLoader;
+import org.sonar.batch.referential.ProjectReferentialsProvider;
+import org.sonar.batch.rule.RulesProvider;
 import org.sonar.batch.scan.filesystem.InputFileCache;
 import org.sonar.batch.scan.maven.FakeMavenPluginExecutor;
 import org.sonar.batch.scan.maven.MavenPluginExecutor;
@@ -123,13 +126,11 @@ public class ProjectScanContainer extends ComponentContainer {
       }
       add(reactor);
     }
-    ProjectReferentialsLoader projectReferentialsLoader = getComponentByType(ProjectReferentialsLoader.class);
-    add(projectReferentialsLoader.load(reactor.getRoot().getKeyWithBranch()));
-
   }
 
   private void addBatchComponents() {
     add(
+      new ProjectReferentialsProvider(),
       DefaultResourceCreationLock.class,
       DefaultPersistenceManager.class,
       DependencyPersister.class,
@@ -179,7 +180,7 @@ public class ProjectScanContainer extends ComponentContainer {
 
       // lang
       Languages.class,
-      DeprecatedLanguagesReferential.class,
+      DefaultLanguagesReferential.class,
       HighlightableBuilder.class,
       SymbolizableBuilder.class,
 
@@ -193,6 +194,11 @@ public class ProjectScanContainer extends ComponentContainer {
       MeasureCache.class,
       DeprecatedMetricFinder.class,
       DefaultMetricFinder.class,
+      PastMeasuresLoader.class,
+
+      // Rules
+      new RulesProvider(),
+      new DebtModelProvider(),
 
       ProjectSettings.class);
   }
index fe9c898755d8bcd099f1094ec2fc8f2c4b66e28a..7c495df931c7d8fe401a667e09c8740994d6bf95 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.measure.Metric;
 import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.Rules;
 import org.sonar.api.config.Settings;
 import org.sonar.batch.issue.IssueFilters;
 import org.sonar.batch.scan.AnalyzerContextAdaptor;
@@ -50,10 +49,9 @@ public class DefaultAnalyzerContext implements AnalyzerContext {
   private final FileSystem fs;
   private final ActiveRules activeRules;
   private final IssueFilters issueFilters;
-  private final Rules rules;
 
   public DefaultAnalyzerContext(ProjectDefinition def, AnalyzerMeasureCache measureCache, AnalyzerIssueCache issueCache,
-    Settings settings, FileSystem fs, ActiveRules activeRules, IssueFilters issueFilters, Rules rules) {
+    Settings settings, FileSystem fs, ActiveRules activeRules, IssueFilters issueFilters) {
     this.def = def;
     this.measureCache = measureCache;
     this.issueCache = issueCache;
@@ -61,7 +59,6 @@ public class DefaultAnalyzerContext implements AnalyzerContext {
     this.fs = fs;
     this.activeRules = activeRules;
     this.issueFilters = issueFilters;
-    this.rules = rules;
   }
 
   @Override
index d4fc0d708fd4c3298392149e60773377409020df..0dcfcc18a88caa1fd8994a4cc21684ce03ec7a46 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer;
 import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer;
 import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
 import org.sonar.batch.issue.ignore.scanner.IssueExclusionsRegexpScanner;
+import org.sonar.batch.rule.ActiveRulesProvider;
 import org.sonar.batch.rule.ModuleQProfiles;
 import org.sonar.batch.rule.QProfileVerifier;
 import org.sonar.batch.scan.LanguageVerifier;
@@ -106,6 +107,7 @@ public class ModuleScanContainer extends ComponentContainer {
 
       // rules
       ModuleQProfiles.class,
+      new ActiveRulesProvider(),
       CheckFactory.class,
 
       // issues
index 097f835afd8f6566ae4c7171d2b46eff3f10d447..a2b602f166a71a18160e73301ac0b433a29aa41a 100644 (file)
@@ -28,13 +28,17 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.config.Settings;
 import org.sonar.api.platform.ComponentContainer;
+import org.sonar.api.resources.Languages;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.batch.bootstrap.ExtensionInstaller;
 import org.sonar.batch.bootstrap.ExtensionMatcher;
 import org.sonar.batch.bootstrap.ExtensionUtils;
 import org.sonar.batch.index.Caches;
+import org.sonar.batch.languages.DefaultLanguagesReferential;
 import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
+import org.sonar.batch.referential.DefaultProjectReferentialsLoader;
 import org.sonar.batch.referential.ProjectReferentialsLoader;
+import org.sonar.batch.referential.ProjectReferentialsProvider;
 import org.sonar.batch.scan.ProjectReactorBuilder;
 import org.sonar.batch.scan.ProjectSettings;
 import org.sonar.batch.scan.filesystem.InputFileCache;
@@ -76,15 +80,21 @@ public class ProjectScanContainer extends ComponentContainer {
       throw new IllegalStateException(bootstrapper + " has returned null as ProjectReactor");
     }
     add(reactor);
-    ProjectReferentialsLoader projectReferentialsLoader = getComponentByType(ProjectReferentialsLoader.class);
-    add(projectReferentialsLoader.load(reactor.getRoot().getKeyWithBranch()));
+    if (getComponentByType(ProjectReferentialsLoader.class) == null) {
+      add(DefaultProjectReferentialsLoader.class);
+    }
   }
 
   private void addBatchComponents() {
     add(
+      new ProjectReferentialsProvider(),
       ProjectSettings.class,
       Caches.class,
 
+      // lang
+      Languages.class,
+      DefaultLanguagesReferential.class,
+
       // Measures
       DefaultMetricFinder.class,
       AnalyzerMeasureCache.class,
index 343bd741713f7c051cd98bab6cd1ec8f39da88c5..d572ef1b3832b740229a6e9fac8b5ec800ffe3e8 100644 (file)
@@ -28,12 +28,11 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.rule.RuleKey;
 import org.sonar.api.utils.MessageException;
 import org.sonar.batch.mediumtest.AnalyzerMediumTester;
 import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult;
 import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin;
-import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
+import org.sonar.batch.protocol.input.ActiveRule;
 
 import java.io.File;
 import java.io.IOException;
@@ -50,9 +49,8 @@ public class FileSystemMediumTest {
 
   public AnalyzerMediumTester tester = AnalyzerMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
-    .registerLanguage(new Xoo())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(RuleKey.of("xoo", "OneIssuePerLine"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "MAJOR", "xoo", "xoo"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index 75663d743822ecde95d2c6145eab7c9fd8d61ab9..26cc47ff793b9d823277d881fcb5b302aac8ad41 100644 (file)
@@ -27,11 +27,10 @@ import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
 import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.rule.RuleKey;
 import org.sonar.batch.mediumtest.AnalyzerMediumTester;
 import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult;
 import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin;
-import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
+import org.sonar.batch.protocol.input.ActiveRule;
 
 import java.io.File;
 import java.io.IOException;
@@ -45,9 +44,8 @@ public class IssuesMediumTest {
 
   public AnalyzerMediumTester tester = AnalyzerMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
-    .registerLanguage(new Xoo())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(RuleKey.of("xoo", "OneIssuePerLine"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "MAJOR", "xoo", "xoo"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index 017f530d521e45c37090056778d0dafbad912bed..31064275b700098a6c797a7b23fa57f8c7190299 100644 (file)
@@ -31,7 +31,6 @@ import org.sonar.api.measures.CoreMetrics;
 import org.sonar.batch.mediumtest.AnalyzerMediumTester;
 import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult;
 import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin;
-import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
 
 import java.io.File;
 import java.io.IOException;
@@ -45,7 +44,6 @@ public class MeasuresMediumTest {
 
   public AnalyzerMediumTester tester = AnalyzerMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
-    .registerLanguage(new Xoo())
     .addDefaultQProfile("xoo", "Sonar Way")
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java
deleted file mode 100644 (file)
index 25576c8..0000000
+++ /dev/null
@@ -1,83 +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.batch.rule;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.batch.rule.ActiveRule;
-import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-import org.sonar.core.qualityprofile.db.ActiveRuleDao;
-
-import java.util.Arrays;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class ActiveRulesProviderTest extends AbstractDaoTestCase {
-
-  ModuleQProfiles qProfiles = mock(ModuleQProfiles.class);
-  RuleFinder ruleFinder = mock(RuleFinder.class);
-
-  @Before
-  public void init_rules() {
-    Rule squidRule = new Rule().setRepositoryKey("squid").setKey("S0001");
-    squidRule.createParameter("min").setDefaultValue("12");
-    when(ruleFinder.findById(10)).thenReturn(squidRule);
-    when(ruleFinder.findById(100)).thenReturn(new Rule().setRepositoryKey("phpunit").setKey("P1"));
-  }
-
-  @Test
-  public void build_active_rules() throws Exception {
-    setupData("shared");
-    when(qProfiles.findAll()).thenReturn(Arrays.asList(
-      // 1 rule is enabled on java with severity INFO
-      new QProfile().setKey("java-two").setName("Java Two").setLanguage("java"),
-      // 1 rule is enabled on php with severity BLOCKER
-      new QProfile().setKey("php-one").setName("Php One").setLanguage("php")
-      ));
-
-    ActiveRulesProvider provider = new ActiveRulesProvider();
-    ActiveRuleDao activeRuleDao = new ActiveRuleDao(getMyBatis());
-    ActiveRules activeRules = provider.provide(qProfiles, activeRuleDao, ruleFinder);
-
-    assertThat(activeRules.findAll()).hasSize(2);
-    assertThat(activeRules.findByRepository("squid")).hasSize(1);
-    assertThat(activeRules.findByRepository("phpunit")).hasSize(1);
-    assertThat(activeRules.findByRepository("unknown")).isEmpty();
-    ActiveRule squidRule = activeRules.find(RuleKey.of("squid", "S0001"));
-    assertThat(squidRule.severity()).isEqualTo(Severity.INFO);
-    assertThat(squidRule.internalKey()).isNull();
-    // "max" and "format" parameters are set in db, "min" is not set but has a default value
-    assertThat(squidRule.params()).hasSize(3);
-    assertThat(squidRule.param("min")).isEqualTo("12");
-    assertThat(squidRule.param("max")).isEqualTo("20");
-    assertThat(squidRule.param("format")).isEqualTo("html");
-
-    ActiveRule phpRule = activeRules.find(RuleKey.of("phpunit", "P1"));
-    assertThat(phpRule.severity()).isEqualTo(Severity.BLOCKER);
-    assertThat(phpRule.params()).isEmpty();
-  }
-}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/ModuleQProfilesTest.java
deleted file mode 100644 (file)
index 8c7d2d2..0000000
+++ /dev/null
@@ -1,141 +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.batch.rule;
-
-import com.google.common.collect.Lists;
-import org.junit.Test;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.utils.MessageException;
-import org.sonar.api.utils.System2;
-import org.sonar.batch.languages.DeprecatedLanguagesReferential;
-import org.sonar.batch.languages.LanguagesReferential;
-import org.sonar.batch.rules.DefaultQProfileReferential;
-import org.sonar.batch.rules.QProfilesReferential;
-import org.sonar.core.persistence.AbstractDaoTestCase;
-import org.sonar.core.qualityprofile.db.QualityProfileDao;
-
-import java.util.List;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.Fail.fail;
-
-public class ModuleQProfilesTest extends AbstractDaoTestCase {
-
-  LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new SimpleLanguage("java"), new SimpleLanguage("php")));
-  Settings settings = new Settings();
-
-  @Test
-  public void find_profiles() throws Exception {
-    // 4 profiles in db
-    setupData("shared");
-    QualityProfileDao dao = new QualityProfileDao(getMyBatis(), System2.INSTANCE);
-    QProfilesReferential ref = new DefaultQProfileReferential(dao);
-
-    settings.setProperty("sonar.profile.java", "Java One");
-    settings.setProperty("sonar.profile.abap", "Abap One");
-    settings.setProperty("sonar.profile.php", "Php One");
-
-    ModuleQProfiles moduleQProfiles = new ModuleQProfiles(settings, languages, ref);
-    List<QProfile> qProfiles = Lists.newArrayList(moduleQProfiles.findAll());
-
-    // load only the profiles of languages detected in project
-    assertThat(qProfiles).hasSize(2);
-    assertThat(moduleQProfiles.findByLanguage("java")).isNotNull();
-    assertThat(moduleQProfiles.findByLanguage("php")).isNotNull();
-    assertThat(moduleQProfiles.findByLanguage("abap")).isNull();
-    QProfile javaProfile = qProfiles.get(0);
-    assertThat(javaProfile.getKey()).isEqualTo("java-one");
-    assertThat(javaProfile.getName()).isEqualTo("Java One");
-    assertThat(javaProfile.getLanguage()).isEqualTo("java");
-    QProfile phpProfile = qProfiles.get(1);
-    assertThat(phpProfile.getKey()).isEqualTo("php-one");
-    assertThat(phpProfile.getName()).isEqualTo("Php One");
-    assertThat(phpProfile.getLanguage()).isEqualTo("php");
-  }
-
-  @Test
-  public void supported_deprecated_property() throws Exception {
-    setupData("shared");
-    QualityProfileDao dao = new QualityProfileDao(getMyBatis(), System2.INSTANCE);
-    QProfilesReferential ref = new DefaultQProfileReferential(dao);
-
-    // deprecated property
-    settings.setProperty("sonar.profile", "Java Two");
-    settings.setProperty("sonar.profile.php", "Php One");
-
-    ModuleQProfiles moduleQProfiles = new ModuleQProfiles(settings, languages, ref);
-    List<QProfile> qProfiles = Lists.newArrayList(moduleQProfiles.findAll());
-
-    assertThat(qProfiles).hasSize(2);
-    QProfile javaProfile = qProfiles.get(0);
-    assertThat(javaProfile.getKey()).isEqualTo("java-two");
-    assertThat(javaProfile.getName()).isEqualTo("Java Two");
-    assertThat(javaProfile.getLanguage()).isEqualTo("java");
-
-    // "Java Two" does not exist for PHP -> fallback to sonar.profile.php
-    QProfile phpProfile = qProfiles.get(1);
-    assertThat(phpProfile.getKey()).isEqualTo("php-one");
-    assertThat(phpProfile.getName()).isEqualTo("Php One");
-    assertThat(phpProfile.getLanguage()).isEqualTo("php");
-  }
-
-  @Test
-  public void fail_if_unknown_profile() throws Exception {
-    setupData("shared");
-    QualityProfileDao dao = new QualityProfileDao(getMyBatis(), System2.INSTANCE);
-    QProfilesReferential ref = new DefaultQProfileReferential(dao);
-
-    settings.setProperty("sonar.profile.java", "Unknown");
-    settings.setProperty("sonar.profile.php", "Php One");
-
-    try {
-      new ModuleQProfiles(settings, languages, ref);
-      fail();
-    } catch (MessageException e) {
-      assertThat(e).hasMessage("Quality profile not found : 'Unknown' on language 'java'");
-    }
-  }
-
-  private static class SimpleLanguage implements Language {
-
-    private final String key;
-
-    private SimpleLanguage(String key) {
-      this.key = key;
-    }
-
-    @Override
-    public String getKey() {
-      return key;
-    }
-
-    @Override
-    public String getName() {
-      return key;
-    }
-
-    @Override
-    public String[] getFileSuffixes() {
-      return new String[0];
-    }
-  }
-}
index 30b6b571883ca33e97dcc41e553471e87e328b63..eb19d5428ea30a8309634e38291f9f365c910dfd 100644 (file)
@@ -27,7 +27,7 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Java;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.utils.MessageException;
-import org.sonar.batch.languages.DeprecatedLanguagesReferential;
+import org.sonar.batch.languages.DefaultLanguagesReferential;
 import org.sonar.batch.languages.LanguagesReferential;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -35,7 +35,7 @@ import static org.fest.assertions.Assertions.assertThat;
 public class LanguageVerifierTest {
 
   Settings settings = new Settings();
-  LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(Java.INSTANCE));
+  LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(Java.INSTANCE));
   DefaultFileSystem fs = new DefaultFileSystem();
 
   @Rule
index 0cf0be9b57e3013d91c1e03872cff8f8cea8e586..e3824aa70f11327401243bb03e8d8c3aa1dcc9fc 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
 import org.sonar.api.platform.ComponentContainer;
+import org.sonar.api.resources.Languages;
 import org.sonar.api.task.TaskExtension;
 import org.sonar.api.utils.System2;
 import org.sonar.batch.bootstrap.AnalysisMode;
@@ -39,6 +40,8 @@ import org.sonar.batch.bootstrap.BootstrapProperties;
 import org.sonar.batch.bootstrap.ExtensionInstaller;
 import org.sonar.batch.bootstrap.GlobalSettings;
 import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.batch.referential.ProjectReferentialsLoader;
 import org.sonar.batch.scan.maven.MavenPluginExecutor;
 import org.sonar.batch.settings.SettingsReferential;
 
@@ -62,14 +65,22 @@ public class ProjectScanContainerTest {
     bootstrapProperties = new BootstrapProperties(Collections.<String, String>emptyMap());
     AnalysisMode analysisMode = new AnalysisMode(bootstrapProperties);
     when(projectBootstrapper.bootstrap()).thenReturn(new ProjectReactor(ProjectDefinition.create()));
-    settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), mock(SettingsReferential.class), new PropertiesConfiguration(), analysisMode);
     parentContainer = new ComponentContainer();
-    parentContainer.add(settings);
     parentContainer.add(System2.INSTANCE);
     parentContainer.add(bootstrapProperties);
     parentContainer.add(analysisMode);
     parentContainer.add(new PropertiesConfiguration());
-    parentContainer.add(mock(SettingsReferential.class));
+    SettingsReferential settingsReferential = mock(SettingsReferential.class);
+    settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), settingsReferential, new PropertiesConfiguration(), analysisMode);
+    parentContainer.add(settings);
+    parentContainer.add(settingsReferential);
+    ProjectReferentialsLoader projectReferentialsLoader = new ProjectReferentialsLoader() {
+      @Override
+      public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) {
+        return new ProjectReferentials();
+      }
+    };
+    parentContainer.add(projectReferentialsLoader);
     container = new ProjectScanContainer(parentContainer);
   }
 
index cda01e168810100e5e2aac561ec7885bef94de4c..2cb4b862f1a6b4ac13cfb05ca6411ce90208929e 100644 (file)
@@ -23,7 +23,7 @@ import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Java;
 import org.sonar.api.resources.Languages;
-import org.sonar.batch.languages.DeprecatedLanguagesReferential;
+import org.sonar.batch.languages.DefaultLanguagesReferential;
 import org.sonar.batch.languages.LanguagesReferential;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -31,7 +31,7 @@ import static org.fest.assertions.Assertions.assertThat;
 public class LanguageDetectionFactoryTest {
   @Test
   public void testCreate() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(Java.INSTANCE));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(Java.INSTANCE));
     LanguageDetectionFactory factory = new LanguageDetectionFactory(new Settings(), languages);
     LanguageDetection languageDetection = factory.create();
     assertThat(languageDetection).isNotNull();
index 7c6eb7ff4f2a5b269244a00b5c31a02d6a33b26f..b8281a3cdcf6203466db3cdc16a310131aaaea92 100644 (file)
@@ -30,7 +30,7 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.utils.MessageException;
-import org.sonar.batch.languages.DeprecatedLanguagesReferential;
+import org.sonar.batch.languages.DefaultLanguagesReferential;
 import org.sonar.batch.languages.LanguagesReferential;
 
 import java.io.File;
@@ -58,7 +58,7 @@ public class LanguageDetectionTest {
 
   @Test
   public void search_by_file_extension() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob")));
     LanguageDetection detection = new LanguageDetection(new Settings(), languages);
 
     assertThat(detection.language(newInputFile("Foo.java"))).isEqualTo("java");
@@ -76,13 +76,13 @@ public class LanguageDetectionTest {
 
   @Test
   public void should_not_fail_if_no_language() throws Exception {
-    LanguageDetection detection = spy(new LanguageDetection(new Settings(), new DeprecatedLanguagesReferential(new Languages())));
+    LanguageDetection detection = spy(new LanguageDetection(new Settings(), new DefaultLanguagesReferential(new Languages())));
     assertThat(detection.language(newInputFile("Foo.java"))).isNull();
   }
 
   @Test
   public void plugin_can_declare_a_file_extension_twice_for_case_sensitivity() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("abap", "abap", "ABAP")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("abap", "abap", "ABAP")));
 
     LanguageDetection detection = new LanguageDetection(new Settings(), languages);
     assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap");
@@ -92,7 +92,7 @@ public class LanguageDetectionTest {
   public void language_with_no_extension() throws Exception {
     // abap does not declare any file extensions.
     // When analyzing an ABAP project, then all source files must be parsed.
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("abap")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("abap")));
 
     // No side-effect on non-ABAP projects
     LanguageDetection detection = new LanguageDetection(new Settings(), languages);
@@ -110,7 +110,7 @@ public class LanguageDetectionTest {
 
   @Test
   public void force_language_using_deprecated_property() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php")));
 
     Settings settings = new Settings();
     settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "java");
@@ -126,7 +126,7 @@ public class LanguageDetectionTest {
     thrown.expect(MessageException.class);
     thrown.expectMessage("No language is installed with key 'unknown'. Please update property 'sonar.language'");
 
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php")));
     Settings settings = new Settings();
     settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "unknown");
     new LanguageDetection(settings, languages);
@@ -134,7 +134,7 @@ public class LanguageDetectionTest {
 
   @Test
   public void fail_if_conflicting_language_suffix() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml")));
     LanguageDetection detection = new LanguageDetection(new Settings(), languages);
     try {
       detection.language(newInputFile("abc.xhtml"));
@@ -149,7 +149,7 @@ public class LanguageDetectionTest {
 
   @Test
   public void solve_conflict_using_filepattern() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml")));
 
     Settings settings = new Settings();
     settings.setProperty("sonar.lang.patterns.xml", "xml/**");
@@ -161,7 +161,7 @@ public class LanguageDetectionTest {
 
   @Test
   public void fail_if_conflicting_filepattern() throws Exception {
-    LanguagesReferential languages = new DeprecatedLanguagesReferential(new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol")));
+    LanguagesReferential languages = new DefaultLanguagesReferential(new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol")));
     Settings settings = new Settings();
     settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt");
     settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt");
index fed0a1078da03f061462a3fe23c522eb074209a9..b908bb4e6bb17cef06d43ed3beda0d6806016df8 100644 (file)
@@ -87,7 +87,7 @@ public class ActiveRuleDto extends Dto<ActiveRuleKey> {
     return this;
   }
 
-  public Integer getRulId() {
+  public Integer getRuleId() {
     return ruleId;
   }
 
index 4c93ba8f97799ab2e11739b96036bf005fe8ca9d..96aab429ea5ad894d3b81fefc667bcc5adbddef2 100644 (file)
@@ -38,6 +38,10 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+/**
+ * @deprecated since 4.5
+ */
+@Deprecated
 public final class CacheRuleFinder implements RuleFinder {
 
   private BiMap<Integer, Rule> rulesById = HashBiMap.create();
index 14fb6cb7ac341939ab9a7d983dd49080c34c14a5..3e96bd664bb16f2682104c89ba34df46249bd770 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.api.batch.rule;
 
-import com.google.common.annotations.Beta;
 import org.sonar.api.rule.RuleKey;
 
 import javax.annotation.CheckForNull;
@@ -28,8 +27,9 @@ import java.util.Collection;
 
 /**
  * @since 4.2
+ * @deprecated since 4.5 use {@link ActiveRules}
  */
-@Beta
+@Deprecated
 public interface Rules {
 
   @CheckForNull