]> source.dussan.org Git - sonarqube.git/commitdiff
fix some quality flaws
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 10 Nov 2015 15:32:56 +0000 (16:32 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 10 Nov 2015 15:33:15 +0000 (16:33 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/duplication/Duplication.java
server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepExecutorTest.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/hash/SourceHashComputer.java

index d57e9e27853d221973b9a0061ae1e84892e04190..f40c9eb67f80ae45dd95c7b59b3c4528500b038a 100644 (file)
@@ -35,8 +35,8 @@ import static java.util.Objects.requireNonNull;
 
 @Immutable
 public final class Duplication {
-  private static Ordering<Duplicate> DUPLICATE_ORDERING = Ordering.from(DuplicateComparatorByType.INSTANCE)
-      .compound(Ordering.natural().onResultOf(DuplicateToFileKey.INSTANCE))
+  private static final Ordering<Duplicate> DUPLICATE_ORDERING = Ordering.from(DuplicateComparatorByType.INSTANCE)
+    .compound(Ordering.natural().onResultOf(DuplicateToFileKey.INSTANCE))
     .compound(Ordering.natural().onResultOf(DuplicateToTextBlock.INSTANCE));
 
   private final TextBlock original;
index 70fe607e91318c3984eadb1739c7a5e88836d33f..eb03462876eb66773b2e124dbecf1d8f8448aeb7 100644 (file)
@@ -31,7 +31,7 @@ import javax.annotation.Nullable;
  */
 public class ThreadLocalUserSession implements UserSession {
 
-  private static final ThreadLocal<UserSession> THREAD_LOCAL = new ThreadLocal<UserSession>();
+  private static final ThreadLocal<UserSession> THREAD_LOCAL = new ThreadLocal<>();
 
   public UserSession get() {
     return Objects.firstNonNull(THREAD_LOCAL.get(), AnonymousUserSession.INSTANCE);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepExecutorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepExecutorTest.java
new file mode 100644 (file)
index 0000000..e7d39c4
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.computation.step;
+
+import java.util.Arrays;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.InOrder;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ComputationStepExecutorTest {
+  @Rule
+  public LogTester logTester = new LogTester();
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void execute_call_execute_on_each_ComputationStep_in_order_returned_by_instances_method() {
+    ComputationStep computationStep1 = mockComputationStep("step1");
+    ComputationStep computationStep2 = mockComputationStep("step2");
+    ComputationStep computationStep3 = mockComputationStep("step3");
+
+    new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2, computationStep3))
+      .execute();
+
+    InOrder inOrder = inOrder(computationStep1, computationStep2, computationStep3);
+    inOrder.verify(computationStep1).execute();
+    inOrder.verify(computationStep1).getDescription();
+    inOrder.verify(computationStep2).execute();
+    inOrder.verify(computationStep2).getDescription();
+    inOrder.verify(computationStep3).execute();
+    inOrder.verify(computationStep3).getDescription();
+    inOrder.verifyNoMoreInteractions();
+  }
+
+  @Test
+  public void execute_let_exception_thrown_by_ComputationStep_go_up_as_is() {
+    String message = "Exception should go up";
+
+    ComputationStep computationStep = mockComputationStep("step1");
+    doThrow(new RuntimeException(message))
+      .when(computationStep)
+      .execute();
+
+    ComputationStepExecutor computationStepExecutor = new ComputationStepExecutor(mockComputationSteps(computationStep));
+
+    expectedException.expect(RuntimeException.class);
+    expectedException.expectMessage(message);
+
+    computationStepExecutor.execute();
+  }
+
+  @Test
+  public void execute_logs_end_timing_for_each_ComputationStep_called() {
+    ComputationStep computationStep1 = mockComputationStep("step1");
+    ComputationStep computationStep2 = mockComputationStep("step2");
+
+    new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2))
+        .execute();
+
+    List<String> infoLogs = logTester.logs(LoggerLevel.INFO);
+    assertThat(infoLogs).hasSize(2);
+    assertThat(infoLogs.get(0)).contains("step1 | time=");
+    assertThat(infoLogs.get(1)).contains("step2 | time=");
+  }
+
+  private static ComputationSteps mockComputationSteps(ComputationStep... computationSteps) {
+    ComputationSteps steps = mock(ComputationSteps.class);
+    when(steps.instances()).thenReturn(Arrays.asList(computationSteps));
+    return steps;
+  }
+
+  private static ComputationStep mockComputationStep(String desc) {
+    ComputationStep mock = mock(ComputationStep.class);
+    when(mock.getDescription()).thenReturn(desc);
+    return mock;
+  }
+}
index 03a608d477eac4c11c8b6161908a3e69e2b02b35..bf2d4dca8c11d938feaa69de21a86ae2b9b6db9b 100644 (file)
@@ -33,7 +33,7 @@ public class SourceHashComputer {
   private final MessageDigest md5Digest = DigestUtils.getMd5Digest();
 
   public void addLine(String line, boolean hasNextLine) {
-    String lineToHash = hasNextLine ? line + '\n' : line;
+    String lineToHash = hasNextLine ? (line + '\n') : line;
     this.md5Digest.update(lineToHash.getBytes(UTF_8));
   }