]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 1 Jul 2014 14:25:01 +0000 (16:25 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 1 Jul 2014 14:25:08 +0000 (16:25 +0200)
18 files changed:
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DeleteAllFilter.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filter.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/Filters.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/KeepOneFilter.java
sonar-batch/src/main/java/org/sonar/batch/scan/AnalyzerContextAdaptor.java
sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java
sonar-plugin-api/src/main/java/org/sonar/api/profiles/XMLProfileParser.java
sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/charts/deprecated/CustomBarChart.java
sonar-server/src/main/java/org/sonar/server/db/migrations/v44/ChangeLogMigration.java
sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RuleActivationActions.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java
sonar-server/src/main/java/org/sonar/server/startup/RegisterNewMeasureFilters.java

index 9d5993cfac167e68505f5b8272d17afc5d9662f8..ade98c3dbaf140c9bb9ef1b32964ccac34875fdb 100644 (file)
@@ -48,7 +48,7 @@ public class DefaultPeriodCleaner implements TaskExtension {
   }
 
   public void clean(long projectId) {
-    doClean(projectId, new Filters(settings).getFilters());
+    doClean(projectId, new Filters(settings).all());
   }
 
   @VisibleForTesting
index 5a1bcbd381f30f4f3f3653acd9e3631e8a865c17..18a795e1ee74aa26c9100ef41aa45c798ca14ebc 100644 (file)
@@ -27,7 +27,7 @@ import org.sonar.core.purge.PurgeableSnapshotDto;
 import java.util.Date;
 import java.util.List;
 
-class DeleteAllFilter extends Filter {
+class DeleteAllFilter implements Filter {
   private final Date before;
 
   public DeleteAllFilter(Date before) {
@@ -35,7 +35,7 @@ class DeleteAllFilter extends Filter {
   }
 
   @Override
-  List<PurgeableSnapshotDto> filter(List<PurgeableSnapshotDto> history) {
+  public List<PurgeableSnapshotDto> filter(List<PurgeableSnapshotDto> history) {
     List<PurgeableSnapshotDto> result = Lists.newArrayList();
     for (PurgeableSnapshotDto snapshot : history) {
       if (snapshot.getDate().before(before)) {
@@ -46,7 +46,7 @@ class DeleteAllFilter extends Filter {
   }
 
   @Override
-  void log() {
+  public void log() {
     LoggerFactory.getLogger(getClass()).info("-> Delete data prior to: " + DateUtils.formatDate(before));
   }
 }
index 7bc0b6d62908af5b2b2edf6e94e653ddc8efbd98..4d4419be8c490cc73b7b61dde599e47a96951c3c 100644 (file)
@@ -23,8 +23,8 @@ import org.sonar.core.purge.PurgeableSnapshotDto;
 
 import java.util.List;
 
-abstract class Filter {
-  abstract List<PurgeableSnapshotDto> filter(List<PurgeableSnapshotDto> snapshots);
+interface Filter {
+  List<PurgeableSnapshotDto> filter(List<PurgeableSnapshotDto> snapshots);
 
-  abstract void log();
+  void log();
 }
index 62f7680cc653b2e3c67de27981e86720f80307d9..c291cd8fec4f6612d39a9241f96dc1adea72d54f 100644 (file)
@@ -29,7 +29,7 @@ import java.util.Date;
 import java.util.List;
 
 class Filters {
-  private final List<Filter> filters = Lists.newArrayList();
+  private final List<Filter> all = Lists.newArrayList();
 
   Filters(Settings settings) {
     Date dateToStartKeepingOneSnapshotByDay = getDateFromHours(settings, DbCleanerConstants.HOURS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_DAY);
@@ -37,14 +37,14 @@ class Filters {
     Date dateToStartKeepingOneSnapshotByMonth = getDateFromWeeks(settings, DbCleanerConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH);
     Date dateToStartDeletingAllSnapshots = getDateFromWeeks(settings, DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS);
 
-    filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, dateToStartKeepingOneSnapshotByDay, Calendar.DAY_OF_YEAR, "day"));
-    filters.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByMonth, dateToStartKeepingOneSnapshotByWeek, Calendar.WEEK_OF_YEAR, "week"));
-    filters.add(new KeepOneFilter(dateToStartDeletingAllSnapshots, dateToStartKeepingOneSnapshotByMonth, Calendar.MONTH, "month"));
-    filters.add(new DeleteAllFilter(dateToStartDeletingAllSnapshots));
+    all.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, dateToStartKeepingOneSnapshotByDay, Calendar.DAY_OF_YEAR, "day"));
+    all.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByMonth, dateToStartKeepingOneSnapshotByWeek, Calendar.WEEK_OF_YEAR, "week"));
+    all.add(new KeepOneFilter(dateToStartDeletingAllSnapshots, dateToStartKeepingOneSnapshotByMonth, Calendar.MONTH, "month"));
+    all.add(new DeleteAllFilter(dateToStartDeletingAllSnapshots));
   }
 
-  List<Filter> getFilters() {
-    return filters;
+  List<Filter> all() {
+    return all;
   }
 
   static Date getDateFromWeeks(Settings settings, String propertyKey) {
index 45ad24cf5ffb5954e4e7c50bb4b5d459a21e873d..81249fdd929d9ceef221a2b5426c0205c28fb0db 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.core.purge.PurgeableSnapshotDto;
 import java.util.Date;
 import java.util.List;
 
-class KeepOneFilter extends Filter {
+class KeepOneFilter implements Filter {
 
   private final Date start;
   private final Date end;
@@ -43,7 +43,7 @@ class KeepOneFilter extends Filter {
   }
 
   @Override
-  List<PurgeableSnapshotDto> filter(List<PurgeableSnapshotDto> history) {
+  public List<PurgeableSnapshotDto> filter(List<PurgeableSnapshotDto> history) {
     List<Interval> intervals = Interval.group(history, start, end, dateField);
     List<PurgeableSnapshotDto> result = Lists.newArrayList();
     for (Interval interval : intervals) {
@@ -54,7 +54,7 @@ class KeepOneFilter extends Filter {
   }
 
   @Override
-  void log() {
+  public void log() {
     LoggerFactory.getLogger(getClass()).info("-> Keep one snapshot per " + label + " between " + DateUtils.formatDate(start) + " and " + DateUtils.formatDate(end));
   }
 
index e1bf875059f102087c7306ab8314f0fc2a9bd5fa..d92769f308cc5489d920d506130c3975bb8536e0 100644 (file)
@@ -171,13 +171,13 @@ public class AnalyzerContextAdaptor implements AnalyzerContext {
         measureToSave.setData(((String) measure.value()));
         break;
       case WORK_DUR:
-        measureToSave.setValue(Double.valueOf(((Long) measure.value())));
+        measureToSave.setValue(Double.valueOf((Long) measure.value()));
         break;
       default:
         if (m.isNumericType()) {
-          measureToSave.setValue(((Double) measure.value()));
+          measureToSave.setValue((Double) measure.value());
         } else if (m.isDataType()) {
-          measureToSave.setData(((String) measure.value()));
+          measureToSave.setData((String) measure.value());
         } else {
           throw new UnsupportedOperationException("Unsupported type :" + m.getType());
         }
index 1847682f73675c786caca21ef6c2188216f1918e..c3813ba404e06d4d1fef96081d4cc582eb65116a 100644 (file)
@@ -135,6 +135,7 @@ public class RulesProfile implements Cloneable {
   /**
    * @deprecated profile versioning is dropped in 4.4. Always returns -1.
    */
+  @CheckForNull
   @Deprecated
   public Boolean getUsed() {
     return null;
index 5ce3cb8a9457e4c524961b989ec223fcb0239216..8519072307c912e65b219d5999d350056221c22c 100644 (file)
@@ -40,8 +40,6 @@ import java.util.HashMap;
 import java.util.Map;
 
 /**
- * TODO should be an interface
- *
  * @since 2.3
  */
 public class XMLProfileParser implements ServerComponent {
index 58994058c11a459ff00c8048a77cf1f88292f828..407a4f8f0ffd07081807cd79a582ce6fed2173ed 100644 (file)
@@ -162,6 +162,8 @@ public class RequestTest {
   @Test
   public void param_as_long() throws Exception {
     assertThat(request.setParam("a_number", "123").paramAsLong("a_number")).isEqualTo(123L);
+    assertThat(request.setParam("a_number", "123").paramAsLong("a_number", 42L)).isEqualTo(123L);
+    assertThat(request.setParam("a_number", null).paramAsLong("a_number", 42L)).isEqualTo(123L);
   }
 
   @Test
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java
new file mode 100644 (file)
index 0000000..26be294
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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.api.server.ws.internal;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class SimpleGetRequestTest {
+
+  @Test
+  public void method() throws Exception {
+    SimpleGetRequest request = new SimpleGetRequest();
+    assertThat(request.method()).isEqualTo("GET");
+
+    request.setParam("foo", "bar");
+    assertThat(request.param("foo")).isEqualTo("bar");
+    assertThat(request.param("unknown")).isNull();
+  }
+}
index 260c49302773048db4efddb6481305b695a54c10..4541ce07895f7b480b8a37b111387a011d0d1dfe 100644 (file)
@@ -115,7 +115,7 @@ public class CustomBarChart extends BarChart {
       } else {
         categoriesSplit = new String[nbValues];
         for (int i = 0; i < nbValues; i++) {
-          categoriesSplit[i] = DEFAULT_NAME_CATEGORY + i;
+          categoriesSplit[i] = new StringBuilder().append(DEFAULT_NAME_CATEGORY).append(i).toString();
         }
       }
 
index 2a3f67432d824ebc3768d4b0d0776e94a116c3eb..a92f0983af6c508acfc1a234b5714dc581287116 100644 (file)
@@ -109,10 +109,10 @@ public class ChangeLogMigration implements DatabaseMigration {
 
   private void processRuleChange(ActiveRuleChange ruleChange, ChangeLog change) {
     ruleChange.setSeverity(SeverityUtil.getSeverityFromOrdinal(change.getSeverity()));
-    String param_name = change.getParamKey();
-    String param_value = change.getParamValue();
-    if (StringUtils.isNotEmpty(param_name)) {
-      ruleChange.setParameter(param_name, param_value);
+    String paramName = change.getParamKey();
+    String paramValue = change.getParamValue();
+    if (StringUtils.isNotEmpty(paramName)) {
+      ruleChange.setParameter(paramName, paramValue);
     }
   }
 
index 3e2062960000bbce06d1f8b1c32983174efb6f64..fd448b98f91253f1367f3d4645cfec1e53f912cc 100644 (file)
@@ -145,7 +145,7 @@ public class DuplicationsParser implements ServerComponent {
     }
   }
 
-  private static class BlockComparator implements Comparator<Block> {
+  private static class BlockComparator implements Comparator<Block>, Serializable {
     @Override
     public int compare(@Nullable Block b1,
       @Nullable Block b2) {
index b89ebbc5a3aa62ee5447260dbfbf478c279acc21..efa0e1bca12a17367d1c5aa2c8e85e62b2785318 100644 (file)
@@ -117,7 +117,7 @@ public class RuleActivationActions implements ServerComponent {
     if (params != null) {
       activation.setParameters(KeyValueFormat.parse(params));
     }
-    activation.setReset(request.paramAsBoolean(RESET) == Boolean.TRUE);
+    activation.setReset(Boolean.TRUE.equals(request.paramAsBoolean(RESET)));
     service.activate(request.mandatoryParam(PROFILE_KEY), activation);
   }
 
index 0ff56b58ae1deb505d83f44db4c9f0afb64af5a3..b90b835440a1103b9e76ddb8b569db3bbc58b56b 100644 (file)
@@ -136,16 +136,10 @@ public class RuleDoc extends BaseDoc implements Rule {
   @Override
   public List<RuleParam> params() {
     List<RuleParam> params = new ArrayList<RuleParam>();
-    List<Map<String, Object>> esParams = getNullableField(RuleNormalizer.RuleField.PARAMS.field());
+    final List<Map<String, Object>> esParams = getNullableField(RuleNormalizer.RuleField.PARAMS.field());
     if (esParams != null) {
       for (final Map<String, Object> esParam : esParams) {
         params.add(new RuleParam() {
-          {
-            this.fields = esParam;
-          }
-
-          Map<String, Object> fields;
-
           @Override
           public String key() {
             return (String) esParam.get(RuleNormalizer.RuleParamField.NAME.field());
@@ -163,8 +157,7 @@ public class RuleDoc extends BaseDoc implements Rule {
 
           @Override
           public RuleParamType type() {
-            return RuleParamType
-              .parse((String) esParam.get(RuleNormalizer.RuleParamField.TYPE.field()));
+            return RuleParamType.parse((String) esParam.get(RuleNormalizer.RuleParamField.TYPE.field()));
           }
         });
       }
index 22b272b72cd4c2317ed66ca82f27faf686e6f7cd..a6bc91a32130eff74c1f92f9050c01e1c188e556 100644 (file)
@@ -228,7 +228,8 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     this.addTermFilter(fb, RuleNormalizer.RuleField._TAGS.field(), query.getTags());
 
     // Construct the debt filter on effective char and subChar
-    if (query.getDebtCharacteristics() != null && !query.getDebtCharacteristics().isEmpty()) {
+    Collection<String> characteristics = query.getDebtCharacteristics();
+    if (characteristics != null && !characteristics.isEmpty()) {
       fb.must(
         FilterBuilders.orFilter(
           // Match only when NOT NONE overriden
@@ -236,8 +237,8 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
             FilterBuilders.notFilter(
               FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE)),
             FilterBuilders.orFilter(
-              FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), query.getDebtCharacteristics()),
-              FilterBuilders.termsFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field(), query.getDebtCharacteristics()))
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), characteristics),
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field(), characteristics))
             ),
 
           // Match only when NOT NONE overriden
@@ -246,14 +247,14 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
               FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), ""),
               FilterBuilders.notFilter(FilterBuilders.existsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()))),
             FilterBuilders.orFilter(
-              FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), query.getDebtCharacteristics()),
-              FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field(), query.getDebtCharacteristics())))
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), characteristics),
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field(), characteristics)))
           )
         );
     }
 
     // Debt char exist filter
-    if (query.getHasDebtCharacteristic() != null && query.getHasDebtCharacteristic()) {
+    if (Boolean.TRUE.equals(query.getHasDebtCharacteristic())) {
       fb.must(FilterBuilders.existsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()));
     }
 
@@ -262,9 +263,10 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
         .gte(query.getAvailableSince()));
     }
 
-    if (query.getStatuses() != null && !query.getStatuses().isEmpty()) {
+    Collection<RuleStatus> statuses = query.getStatuses();
+    if (statuses != null && !statuses.isEmpty()) {
       Collection<String> stringStatus = new ArrayList<String>();
-      for (RuleStatus status : query.getStatuses()) {
+      for (RuleStatus status : statuses) {
         stringStatus.add(status.name());
       }
       this.addTermFilter(fb, RuleNormalizer.RuleField.STATUS.field(), stringStatus);
@@ -391,6 +393,9 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     return tags;
   }
 
+  /**
+   * @deprecated do not use ids but keys
+   */
   @Deprecated
   @CheckForNull
   public Rule getById(int id) {
@@ -407,6 +412,9 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     }
   }
 
+  /**
+   * @deprecated do not use ids but keys
+   */
   @Deprecated
   public List<Rule> getByIds(Collection<Integer> ids) {
     SearchResponse response = getClient().prepareSearch(this.getIndexName())
index 887b92abba345235b3bbd57e7d1ea04b7d7b8287..0bb354aa314bfd15df498faa45db52c84da991a1 100644 (file)
@@ -60,6 +60,6 @@ public class IndexSynchronizer {
     long start = System.currentTimeMillis();
     dao.synchronizeAfter(session,
       index.getLastSynchronization());
-    LOG.info("-- Synchronized {} in {}ms", index.getIndexType(), (System.currentTimeMillis() - start));
+    LOG.info("-- Synchronized {} in {}ms", index.getIndexType(), System.currentTimeMillis() - start);
   }
 }
index 755a1eed6a6ea807720b68aa5bd9d95168744a87..84c53745e9b986cec9ee2c7f37a20185a4f33660 100644 (file)
@@ -149,11 +149,11 @@ public final class RegisterNewMeasureFilters {
   private static void appendColumns(Filter filter, List<String> fields) {
     List<String> columnFields = Lists.newArrayList();
     for (FilterColumn column : filter.getColumns()) {
-      String columnKey = column.getFamily();
+      StringBuilder columnKey = new StringBuilder().append(column.getFamily());
       if (StringUtils.isNotBlank(column.getKey()) && !column.isVariation()) {
-        columnKey += ":" + column.getKey();
+        columnKey.append(":").append(column.getKey());
       }
-      columnFields.add(columnKey);
+      columnFields.add(columnKey.toString());
     }
     if (!columnFields.isEmpty()) {
       fields.add("cols=" + Joiner.on(",").join(columnFields));