aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-11-10 16:32:56 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-11-10 16:33:15 +0100
commit69095c5d5bba7d613205536af1529c750fe4547d (patch)
tree94d8979f68432e40a54aceec248dee9b92f59507
parent51269532c36b9314d698f026ca3e27ed52f15bf5 (diff)
downloadsonarqube-69095c5d5bba7d613205536af1529c750fe4547d.tar.gz
sonarqube-69095c5d5bba7d613205536af1529c750fe4547d.zip
fix some quality flaws
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/duplication/Duplication.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepExecutorTest.java104
-rw-r--r--sonar-core/src/main/java/org/sonar/core/hash/SourceHashComputer.java2
4 files changed, 108 insertions, 4 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/Duplication.java b/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/Duplication.java
index d57e9e27853..f40c9eb67f8 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/Duplication.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/Duplication.java
@@ -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;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java b/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java
index 70fe607e913..eb03462876e 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java
@@ -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
index 00000000000..e7d39c440e8
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ComputationStepExecutorTest.java
@@ -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;
+ }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/hash/SourceHashComputer.java b/sonar-core/src/main/java/org/sonar/core/hash/SourceHashComputer.java
index 03a608d477e..bf2d4dca8c1 100644
--- a/sonar-core/src/main/java/org/sonar/core/hash/SourceHashComputer.java
+++ b/sonar-core/src/main/java/org/sonar/core/hash/SourceHashComputer.java
@@ -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));
}