]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 14 Jan 2016 10:49:13 +0000 (11:49 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 14 Jan 2016 10:55:34 +0000 (11:55 +0100)
22 files changed:
server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java
server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/DeleteAction.java
server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java
server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/DeleteAction.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/MetricsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/MeasureValueFormatterTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/metric/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/UpdateActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java

index c746743768b8eef5803ea5b029caa9e88f3a3b56..e6e967dee4106de9c65fdd432bc3ab00abe4194e 100644 (file)
  */
 package org.sonar.server.component.ws;
 
+import com.google.common.base.Function;
 import com.google.common.collect.Collections2;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.core.util.NonNullInputFunction;
-
 import java.util.Arrays;
 import java.util.Collection;
+import javax.annotation.Nonnull;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
 
 public class LanguageParamUtils {
 
@@ -43,11 +43,15 @@ public class LanguageParamUtils {
   }
 
   public static Collection<String> getLanguageKeys(Languages languages) {
-    return Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction<Language, String>() {
-      @Override
-      public String doApply(Language input) {
-        return input.getKey();
-      }
-    });
+    return Collections2.transform(Arrays.asList(languages.all()), LanguageToKeyFunction.INSTANCE);
+  }
+
+  private enum LanguageToKeyFunction implements Function<Language, java.lang.String> {
+    INSTANCE;
+
+    @Override
+    public String apply(@Nonnull Language input) {
+      return input.getKey();
+    }
   }
 }
index 8ce4308fc69e985fb62f30740ab8a78fc0f8d5cf..980d9552cb8a4541277eb54f88c0dfb03ea4be3d 100644 (file)
@@ -24,11 +24,11 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.measure.custom.CustomMeasureDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.user.UserSession;
 
 public class DeleteAction implements CustomMeasuresWsAction {
index a6218866fbae1271012da978e4a0b93a2d532e8d..37af94449ab7b19561c36e22ffe2b0d9a9daa13d 100644 (file)
@@ -25,12 +25,12 @@ import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.metric.ws.MetricJsonWriter;
 import org.sonar.server.user.UserSession;
 
@@ -77,7 +77,8 @@ public class MetricsAction implements CustomMeasuresWsAction {
     DbSession dbSession = dbClient.openSession(false);
 
     try {
-      ComponentDto project = componentFinder.getByUuidOrKey(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY), PROJECT_ID_AND_KEY);
+      ComponentDto project = componentFinder.getByUuidOrKey(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY),
+        PROJECT_ID_AND_KEY);
       checkPermissions(userSession, project);
       List<MetricDto> metrics = searchMetrics(dbSession, project);
 
index b2ccbf997b69c7d71b74b24ce0e1ff0b6ed49796..33490bb4845e00d6c4330b352cf83c444310f8d9 100644 (file)
@@ -26,12 +26,12 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.user.User;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.user.index.UserIndex;
 
index 051f84a1df1afddb6fe5a6c7861fcb4e156a4b1f..176db2a75b9eb3dbe4c0de6ee7770983b952b3e9 100644 (file)
@@ -88,7 +88,6 @@ public class ComponentTreeDataLoader {
     this.resourceTypes = resourceTypes;
   }
 
-  @CheckForNull
   ComponentTreeData load(ComponentTreeWsRequest wsRequest) {
     DbSession dbSession = dbClient.openSession(false);
     try {
@@ -164,12 +163,7 @@ public class ComponentTreeDataLoader {
   private List<MetricDto> searchMetrics(DbSession dbSession, ComponentTreeWsRequest request) {
     List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetricKeys());
     if (metrics.size() < request.getMetricKeys().size()) {
-      List<String> foundMetricKeys = Lists.transform(metrics, new Function<MetricDto, String>() {
-        @Override
-        public String apply(@Nonnull MetricDto input) {
-          return input.getKey();
-        }
-      });
+      List<String> foundMetricKeys = Lists.transform(metrics, MetricDtoFunctions.toKey());
       Set<String> missingMetricKeys = Sets.difference(
         new LinkedHashSet<>(request.getMetricKeys()),
         new LinkedHashSet<>(foundMetricKeys));
index c5ee3d246a9cef3b4b81879eedbbe496ea86ff28..2cd5f81b1f9b040eaecf18fa4a0761adb7328075 100644 (file)
@@ -47,6 +47,10 @@ class ComponentTreeSort {
 
   static List<ComponentDtoWithSnapshotId> sortComponents(List<ComponentDtoWithSnapshotId> components, ComponentTreeWsRequest wsRequest, List<MetricDto> metrics,
     Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric) {
+    List<String> sortParameters = wsRequest.getSort();
+    if (sortParameters == null || sortParameters.isEmpty()) {
+      return components;
+    }
     boolean isAscending = wsRequest.getAsc();
     Map<String, Ordering<ComponentDtoWithSnapshotId>> orderingsBySortField = ImmutableMap.<String, Ordering<ComponentDtoWithSnapshotId>>builder()
       .put(NAME_SORT, componentNameOrdering(isAscending))
@@ -55,7 +59,6 @@ class ComponentTreeSort {
       .put(METRIC_SORT, metricOrdering(wsRequest, metrics, measuresByComponentUuidAndMetric))
       .build();
 
-    List<String> sortParameters = wsRequest.getSort();
     String firstSortParameter = sortParameters.get(0);
     Ordering<ComponentDtoWithSnapshotId> primaryOrdering = orderingsBySortField.get(firstSortParameter);
     if (sortParameters.size() > 1) {
index 37c85a57297d0f6c5df3f395a1fa7970c1ac70eb..bd40552062441ed67750e78805aee43d311483e5 100644 (file)
@@ -23,7 +23,7 @@ import org.sonar.db.measure.MeasureDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonarqube.ws.WsMeasures;
 
-import static org.sonar.server.measure.ws.MeasureValueFormatter.formatDoubleValue;
+import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue;
 import static org.sonar.server.measure.ws.MeasureValueFormatter.formatMeasureValue;
 
 class MeasureDtoToWsMeasure {
@@ -41,19 +41,19 @@ class MeasureDtoToWsMeasure {
       measure.setValue(formatMeasureValue(measureDto, metricDto));
     }
     if (measureDto.getVariation(1) != null) {
-      measure.setVariationValueP1(formatDoubleValue(measureDto.getVariation(1), metricDto));
+      measure.setVariationValueP1(formatNumericalValue(measureDto.getVariation(1), metricDto));
     }
     if (measureDto.getVariation(2) != null) {
-      measure.setVariationValueP2(formatDoubleValue(measureDto.getVariation(2), metricDto));
+      measure.setVariationValueP2(formatNumericalValue(measureDto.getVariation(2), metricDto));
     }
     if (measureDto.getVariation(3) != null) {
-      measure.setVariationValueP3(formatDoubleValue(measureDto.getVariation(3), metricDto));
+      measure.setVariationValueP3(formatNumericalValue(measureDto.getVariation(3), metricDto));
     }
     if (measureDto.getVariation(4) != null) {
-      measure.setVariationValueP4(formatDoubleValue(measureDto.getVariation(4), metricDto));
+      measure.setVariationValueP4(formatNumericalValue(measureDto.getVariation(4), metricDto));
     }
     if (measureDto.getVariation(5) != null) {
-      measure.setVariationValueP5(formatDoubleValue(measureDto.getVariation(5), metricDto));
+      measure.setVariationValueP5(formatNumericalValue(measureDto.getVariation(5), metricDto));
     }
 
     return measure.build();
index c3a8881b46ba47c2434b2fb0df93a307034524a2..fcf5e28bd2262669aefc140c8bb4b3459209d76d 100644 (file)
@@ -57,7 +57,7 @@ class MeasureValueFormatter {
     }
   }
 
-  static String formatDoubleValue(Double value, MetricDto metric) {
+  static String formatNumericalValue(Double value, MetricDto metric) {
     Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType());
 
     switch (metricType) {
index c4bc881c8a4792ad558131888ac88486dc949669..c8b9c0a5253fc646a444f8bbe37429b5b7cc1b5c 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.server.measure.ws;
 import org.sonar.db.metric.MetricDto;
 import org.sonarqube.ws.Common.Metric;
 
-import static org.sonar.server.measure.ws.MeasureValueFormatter.formatDoubleValue;
+import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue;
 
 class MetricDtoToWsMetric {
   private MetricDtoToWsMetric() {
@@ -48,10 +48,10 @@ class MetricDtoToWsMetric {
       metric.setDecimalScale(metricDto.getDecimalScale());
     }
     if (metricDto.getBestValue() != null) {
-      metric.setBestValue(formatDoubleValue(metricDto.getBestValue(), metricDto));
+      metric.setBestValue(formatNumericalValue(metricDto.getBestValue(), metricDto));
     }
     if (metricDto.getWorstValue() != null) {
-      metric.setWorstValue(formatDoubleValue(metricDto.getWorstValue(), metricDto));
+      metric.setWorstValue(formatNumericalValue(metricDto.getWorstValue(), metricDto));
     }
 
     return metric.build();
index bc8627321aed64bcae25e2a56099347e26fd0ab2..023ed3b06b0179affcdc4978b47c7769edeac9e1 100644 (file)
@@ -27,11 +27,11 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.ruby.RubyBridge;
 import org.sonar.server.user.UserSession;
index 91457bdf42e3da4749cbb59337c6ce8d2583084d..d4447f6563cbcd51609eff4bbd74a5a2cb2cca84 100644 (file)
@@ -27,10 +27,10 @@ import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.ruby.RubyBridge;
 import org.sonar.server.user.UserSession;
 
index 914e57823f53a02204ad1657a9da80f2cb2cadeb..281b12ec80c112961eb84f15c3a58151f280dc63 100644 (file)
@@ -27,10 +27,10 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.es.SearchOptions;
 
 import static com.google.common.collect.Sets.newHashSet;
index 8eb3e8ce7ad7f3a617ab62643697773682a06aac..d9b5f493833f6f56773ad639d86a3e43607b9329 100644 (file)
@@ -27,11 +27,11 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
 import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.ruby.RubyBridge;
 import org.sonar.server.user.UserSession;
index b22388b696e6b1d4bf0eef5120136f5632d4e576..bf1cf7b0486d9feb9f64f8d0d853641600c1b920 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.measure.custom.ws;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -28,15 +27,13 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.RowNotFoundException;
-import org.sonar.db.component.ComponentDao;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.measure.custom.CustomMeasureDao;
-import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.db.DbClient;
+import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
@@ -51,28 +48,21 @@ public class DeleteActionTest {
 
   public static final String ACTION = "delete";
 
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
+
   WsTester ws;
-  DbClient dbClient;
-  DbSession dbSession;
 
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao());
-    dbSession = dbClient.openSession(false);
     ws = new WsTester(new CustomMeasuresWs(new DeleteAction(dbClient, userSessionRule)));
     userSessionRule.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
-    db.truncateTables();
-  }
-
-  @After
-  public void tearDown() {
-    dbSession.close();
   }
 
   @Test
index 76ccfaafc3fbc8adf520db3813f096f517e1ad35..bd4ff5cb2efdaefe4fd165f0b652e2be6d14eb8b 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.measure.custom.ws;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.ClassRule;
@@ -32,19 +31,16 @@ import org.sonar.api.measures.Metric;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDao;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.server.component.ComponentFinder;
-import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.db.measure.custom.CustomMeasureDao;
-import org.sonar.db.metric.MetricDao;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.user.index.UserDoc;
 import org.sonar.server.user.index.UserIndexDefinition;
@@ -53,29 +49,26 @@ import org.sonar.test.DbTests;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto;
+import static org.sonar.db.metric.MetricTesting.newMetricDto;
 import static org.sonar.server.measure.custom.ws.CustomMeasuresWs.ENDPOINT;
 import static org.sonar.server.measure.custom.ws.MetricsAction.ACTION;
-import static org.sonar.db.metric.MetricTesting.newMetricDto;
 
 @Category(DbTests.class)
 public class MetricsActionTest {
   private static final String DEFAULT_PROJECT_UUID = "project-uuid";
   private static final String DEFAULT_PROJECT_KEY = "project-key";
 
+  @ClassRule
+  public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings()));
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
-
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
-
-  @ClassRule
-  public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings()));
-
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
 
-  DbClient dbClient;
-  DbSession dbSession;
   WsTester ws;
   ComponentDto defaultProject;
 
@@ -90,19 +83,11 @@ public class MetricsActionTest {
 
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new ComponentDao(), new CustomMeasureDao());
-    dbSession = dbClient.openSession(false);
-    db.truncateTables();
     ws = new WsTester(new CustomMeasuresWs(new MetricsAction(dbClient, userSession, new ComponentFinder(dbClient))));
     userSession.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
     defaultProject = insertDefaultProject();
   }
 
-  @After
-  public void tearDown() {
-    dbSession.close();
-  }
-
   @Test
   public void list_metrics() throws Exception {
     insertCustomMetric("metric-key-1");
index ccc917edb7882ef9bb3a2f7aa67c6dba9edb0209..79cf52fbf444632faf87d1925fb259a11836b181 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.measure.custom.ws;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.ClassRule;
@@ -31,18 +30,15 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.measures.Metric.ValueType;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.RowNotFoundException;
-import org.sonar.db.component.ComponentDao;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.measure.custom.CustomMeasureDao;
+import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.measure.custom.CustomMeasureDto;
-import org.sonar.db.metric.MetricDao;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.metric.MetricTesting;
-import org.sonar.db.component.ComponentTesting;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.ServerException;
@@ -75,9 +71,9 @@ public class UpdateActionTest {
   public DbTester db = DbTester.create(System2.INSTANCE);
   @ClassRule
   public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings()));
-  DbClient dbClient;
-  DbSession dbSession;
-  System2 system;
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
+  System2 system = mock(System2.class);
   WsTester ws;
 
   @BeforeClass
@@ -91,21 +87,13 @@ public class UpdateActionTest {
 
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao(), new MetricDao());
-    dbSession = dbClient.openSession(false);
-    db.truncateTables();
-    system = mock(System2.class);
     CustomMeasureValidator validator = new CustomMeasureValidator(newFullTypeValidations());
+
     ws = new WsTester(new CustomMeasuresWs(new UpdateAction(dbClient, userSessionRule, system, validator, new CustomMeasureJsonWriter(new UserJsonWriter(userSessionRule)),
       new UserIndex(es.client()))));
     userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
   }
 
-  @After
-  public void tearDown() {
-    dbSession.close();
-  }
-
   @Test
   public void update_text_value_and_description_in_db() throws Exception {
     MetricDto metric = insertNewMetric(ValueType.STRING);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/MeasureValueFormatterTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/MeasureValueFormatterTest.java
new file mode 100644 (file)
index 0000000..9ad6ea6
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.measure.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.measures.Metric;
+import org.sonar.db.measure.MeasureDto;
+import org.sonar.db.metric.MetricDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.measures.Metric.ValueType.BOOL;
+import static org.sonar.api.measures.Metric.ValueType.DATA;
+import static org.sonar.api.measures.Metric.ValueType.FLOAT;
+import static org.sonar.api.measures.Metric.ValueType.INT;
+import static org.sonar.api.measures.Metric.ValueType.MILLISEC;
+import static org.sonar.api.measures.Metric.ValueType.PERCENT;
+import static org.sonar.api.measures.Metric.ValueType.WORK_DUR;
+import static org.sonar.db.metric.MetricTesting.newMetricDto;
+import static org.sonar.server.measure.ws.MeasureValueFormatter.formatMeasureValue;
+import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue;
+
+public class MeasureValueFormatterTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void test_formatNumericalValue() {
+    assertThat(formatNumericalValue(-1.0d, newMetric(BOOL))).isEqualTo("false");
+    assertThat(formatNumericalValue(1.0d, newMetric(BOOL))).isEqualTo("true");
+    assertThat(formatNumericalValue(1_000.123d, newMetric(FLOAT))).isEqualTo("1000.123");
+    assertThat(formatNumericalValue(1_000.0d, newMetric(INT))).isEqualTo("1000");
+    assertThat(formatNumericalValue(1_000.0d, newMetric(WORK_DUR))).isEqualTo("1000");
+  }
+
+  @Test
+  public void test_formatMeasureValue() {
+    assertThat(formatMeasureValue(newNumericMeasure(-1.0d), newMetric(BOOL))).isEqualTo("false");
+    assertThat(formatMeasureValue(newNumericMeasure(1.0d), newMetric(BOOL))).isEqualTo("true");
+    assertThat(formatMeasureValue(newNumericMeasure(1000.123d), newMetric(PERCENT))).isEqualTo("1000.123");
+    assertThat(formatMeasureValue(newNumericMeasure(1000d), newMetric(MILLISEC))).isEqualTo("1000");
+    assertThat(formatMeasureValue(newNumericMeasure(1000.0d), newMetric(WORK_DUR))).isEqualTo("1000");
+    assertThat(formatMeasureValue(newTextMeasure("text-value"), newMetric(DATA))).isEqualTo("text-value");
+  }
+
+  @Test
+  public void fail_if_text_value_type_for_numeric_formatter() {
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Unsupported metric type 'DATA' for numerical value");
+
+    formatNumericalValue(42.0d, newMetric(DATA));
+  }
+
+  private static MetricDto newMetric(Metric.ValueType valueType) {
+    return newMetricDto().setValueType(valueType.name());
+  }
+
+  private static MeasureDto newNumericMeasure(Double value) {
+    return new MeasureDto().setValue(value);
+  }
+
+  private static MeasureDto newTextMeasure(String data) {
+    return new MeasureDto().setData(data);
+  }
+}
index e9ed5c014cf197f7b9961c854b0f405c70a9695b..98af5a290094c5a023a0216360521b4a3daf9e6d 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.metric.ws;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -28,16 +27,14 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.measures.Metric.ValueType;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.measure.custom.CustomMeasureTesting;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.metric.MetricTesting;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.ServerException;
-import org.sonar.db.measure.custom.CustomMeasureDao;
-import org.sonar.db.measure.custom.CustomMeasureTesting;
-import org.sonar.db.metric.MetricDao;
 import org.sonar.server.ruby.RubyBridge;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
@@ -61,31 +58,23 @@ public class CreateActionTest {
   private static final String DEFAULT_DESCRIPTION = "custom-metric-description";
   private static final String DEFAULT_TYPE = ValueType.INT.name();
 
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
+
   WsTester ws;
-  DbClient dbClient;
-  DbSession dbSession;
 
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new CustomMeasureDao());
-    dbSession = dbClient.openSession(false);
-    db.truncateTables();
-
     ws = new WsTester(new MetricsWs(new CreateAction(dbClient, userSessionRule, mock(RubyBridge.class, RETURNS_DEEP_STUBS))));
     userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
   }
 
-  @After
-  public void tearDown() {
-    dbSession.close();
-  }
-
   @Test
   public void insert_new_minimalist_metric() throws Exception {
     newRequest()
index cffbcd71b9b89ac3406b281314a7a371547e4d93..7fe950ebc366029d32048daffe4056261083b37d 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.metric.ws;
 
 import java.util.Arrays;
 import java.util.List;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -29,15 +28,14 @@ import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.measure.custom.CustomMeasureDto;
-import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.db.measure.custom.CustomMeasureDao;
 import org.sonar.db.measure.custom.CustomMeasureTesting;
 import org.sonar.db.metric.MetricDao;
+import org.sonar.db.metric.MetricDto;
+import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.ruby.RubyBridge;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
@@ -51,32 +49,25 @@ import static org.sonar.db.metric.MetricTesting.newMetricDto;
 @Category(DbTests.class)
 public class DeleteActionTest {
 
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
-  DbClient dbClient;
-  DbSession dbSession;
-  WsTester ws;
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
   MetricDao metricDao;
 
+  WsTester ws;
+
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new CustomMeasureDao());
-    dbSession = dbClient.openSession(false);
-    db.truncateTables();
     userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
     ws = new WsTester(new MetricsWs(new DeleteAction(dbClient, userSessionRule, mock(RubyBridge.class, RETURNS_DEEP_STUBS))));
     metricDao = dbClient.metricDao();
   }
 
-  @After
-  public void tearDown() {
-    dbSession.close();
-  }
-
   @Test
   public void delete_by_keys() throws Exception {
     insertCustomEnabledMetrics(1, 2, 3);
index 993d722d40575436f1d4895f2c4d0466cac8ac68..22dcb3e62d13dacf37d0ae0d3c61ed210ec061b9 100644 (file)
 package org.sonar.server.metric.ws;
 
 import org.apache.commons.lang.StringUtils;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.db.metric.MetricDao;
 import org.sonar.server.ws.WsTester;
 import org.sonar.test.DbTests;
 
@@ -44,21 +42,13 @@ public class SearchActionTest {
 
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
-  DbClient dbClient;
-  DbSession dbSession;
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
   WsTester ws;
 
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao());
-    dbSession = dbClient.openSession(false);
     ws = new WsTester(new MetricsWs(new SearchAction(dbClient)));
-    db.truncateTables();
-  }
-
-  @After
-  public void tearDown() {
-    dbSession.close();
   }
 
   @Test
index 8d078663f254de8e092c574e777ce058b2c2e686..f278e8d58766d2993f4a674937e0e9137b645f98 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.metric.ws;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -28,14 +27,12 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.measures.Metric.ValueType;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.metric.MetricDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.ServerException;
-import org.sonar.db.measure.custom.CustomMeasureDao;
-import org.sonar.db.metric.MetricDao;
 import org.sonar.server.ruby.RubyBridge;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
@@ -61,31 +58,23 @@ public class UpdateActionTest {
   private static final String DEFAULT_DESCRIPTION = "custom-metric-description";
   private static final String DEFAULT_TYPE = ValueType.INT.name();
 
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+  DbClient dbClient = db.getDbClient();
+  DbSession dbSession = db.getSession();
+
   WsTester ws;
-  DbClient dbClient;
-  DbSession dbSession;
 
   @Before
   public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new CustomMeasureDao());
-    dbSession = dbClient.openSession(false);
-    db.truncateTables();
-
     ws = new WsTester(new MetricsWs(new UpdateAction(dbClient, userSessionRule, mock(RubyBridge.class, RETURNS_DEEP_STUBS))));
     userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
   }
 
-  @After
-  public void tearDown() {
-    dbSession.close();
-  }
-
   @Test
   public void update_all_fields() throws Exception {
     int id = insertMetric(newDefaultMetric());
index 1ff7010885a66d93d15c2feadf90dd0a8122f9c9..e8c99d569e9387bb4326b142a61635dc6787ed73 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonarqube.ws.client.issue;
 
-import com.google.common.base.Joiner;
 import org.sonarqube.ws.Issues.SearchWsResponse;
 import org.sonarqube.ws.client.BaseService;
 import org.sonarqube.ws.client.GetRequest;
@@ -60,7 +59,6 @@ import static org.sonarqube.ws.client.issue.IssueFilterParameters.STATUSES;
 import static org.sonarqube.ws.client.issue.IssueFilterParameters.TAGS;
 
 public class IssuesService extends BaseService {
-  private static final Joiner LIST_TO_PARAMS_STRING = Joiner.on(",").skipNulls();
 
   public IssuesService(WsConnector wsConnector) {
     super(wsConnector, "api/issues");