From 2bc4e191083b996abf80bcbc535064bce6881bed Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 23 Feb 2015 22:32:31 +0100 Subject: [PATCH] Fix quality flaws --- .../java/org/sonar/process/LogbackHelper.java | 2 +- .../computation/ComputationService.java | 16 +++++----- .../computation/ComputationServiceTest.java | 2 ++ .../batch/phases/InitializersExecutor.java | 12 +++---- .../batch/phases/PhasesTimeProfiler.java | 14 ++++---- .../GlobalRepositoriesProvider.java | 17 ++++------ .../ProjectRepositoriesProvider.java | 17 ++++------ .../profiling/ProfiledDataSource.java | 2 +- .../sonar/core/util/NonNullInputFunction.java | 2 +- .../core/util/NonNullInputFunctionTest.java | 2 +- .../org/sonar/api/utils/TimeProfiler.java | 1 + .../org/sonar/api/utils/log/LogTester.java | 2 +- .../sonar/api/utils/log/BaseLoggerTest.java | 32 +++++++++++++++++++ 13 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/utils/log/BaseLoggerTest.java diff --git a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java index dc33e3c0b6c..7fce2ffa786 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java +++ b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java @@ -102,7 +102,7 @@ public class LogbackHelper { } else if (rollingPolicy.startsWith("size:")) { return new SizeRollingPolicy(ctx, filenamePrefix, logsDir, maxFiles, StringUtils.substringAfter(rollingPolicy, "size:")); - } else if (rollingPolicy.equals("none")) { + } else if ("none".equals(rollingPolicy)) { return new NoRollingPolicy(ctx, filenamePrefix, logsDir, maxFiles); } else { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java index 2f8f2cce6ca..77ee77fb272 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java @@ -22,11 +22,11 @@ package org.sonar.server.computation; import com.google.common.base.Throwables; import org.apache.commons.lang.ArrayUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.api.utils.System2; -import org.sonar.api.utils.TimeProfiler; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; import org.sonar.core.activity.Activity; import org.sonar.core.component.ComponentDto; import org.sonar.core.computation.db.AnalysisReportDto; @@ -39,7 +39,7 @@ import org.sonar.server.db.DbClient; public class ComputationService implements ServerComponent { - private static final Logger LOG = LoggerFactory.getLogger(ComputationService.class); + private static final Logger LOG = Loggers.get(ComputationService.class); private final DbClient dbClient; private final ComputationSteps steps; @@ -52,7 +52,7 @@ public class ComputationService implements ServerComponent { } public void process(AnalysisReportDto report) { - TimeProfiler profiler = new TimeProfiler(LOG).start(String.format( + Profiler profiler = Profiler.create(LOG).startInfo(String.format( "#%s - %s - processing analysis report", report.getId(), report.getProjectKey())); ComponentDto project = loadProject(report); @@ -60,9 +60,9 @@ public class ComputationService implements ServerComponent { ComputationContext context = new ComputationContext(report, project); for (ComputationStep step : steps.orderedSteps()) { if (ArrayUtils.contains(step.supportedProjectQualifiers(), context.getProject().qualifier())) { - TimeProfiler stepProfiler = new TimeProfiler(LOG).start(step.getDescription()); + Profiler stepProfiler = Profiler.create(LOG).startInfo(step.getDescription()); step.execute(context); - stepProfiler.stop(); + stepProfiler.stopInfo(); } } report.succeed(); @@ -73,7 +73,7 @@ public class ComputationService implements ServerComponent { } finally { logActivity(report, project); - profiler.stop(); + profiler.stopInfo(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java index 1e3eebf1e43..e983b901cef 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.computation; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; @@ -107,6 +108,7 @@ public class ComputationServiceTest { private ComputationStep mockStep(String... qualifiers) { ComputationStep step = mock(ComputationStep.class); when(step.supportedProjectQualifiers()).thenReturn(qualifiers); + when(step.getDescription()).thenReturn(RandomStringUtils.randomAscii(5)); return step; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java index b6d1de32299..2297dba07f4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java @@ -21,11 +21,11 @@ package org.sonar.batch.phases; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.batch.Initializer; import org.sonar.api.resources.Project; -import org.sonar.api.utils.TimeProfiler; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; import org.sonar.batch.bootstrap.BatchExtensionDictionnary; import org.sonar.batch.events.EventBus; @@ -33,7 +33,7 @@ import java.util.Collection; public class InitializersExecutor { - private static final Logger LOG = LoggerFactory.getLogger(SensorsExecutor.class); + private static final Logger LOG = Loggers.get(SensorsExecutor.class); private Project project; private BatchExtensionDictionnary selector; @@ -55,9 +55,9 @@ public class InitializersExecutor { for (Initializer initializer : initializers) { eventBus.fireEvent(new InitializerExecutionEvent(initializer, true)); - TimeProfiler profiler = new TimeProfiler(LOG).start("Initializer " + initializer); + Profiler profiler = Profiler.create(LOG).startInfo("Initializer " + initializer); initializer.execute(project); - profiler.stop(); + profiler.stopInfo(); eventBus.fireEvent(new InitializerExecutionEvent(initializer, false)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java index 05eda79a065..612e4d49d5a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java @@ -22,14 +22,14 @@ package org.sonar.batch.phases; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.events.DecoratorExecutionHandler; import org.sonar.api.batch.events.DecoratorsPhaseHandler; import org.sonar.api.batch.events.SensorExecutionHandler; import org.sonar.api.batch.events.SensorsPhaseHandler; -import org.sonar.api.utils.TimeProfiler; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; import java.util.IdentityHashMap; import java.util.List; @@ -37,9 +37,9 @@ import java.util.Map; public class PhasesTimeProfiler implements SensorExecutionHandler, DecoratorExecutionHandler, DecoratorsPhaseHandler, SensorsPhaseHandler { - private static final Logger LOG = LoggerFactory.getLogger(PhasesTimeProfiler.class); + private static final Logger LOG = Loggers.get(PhasesTimeProfiler.class); - private TimeProfiler profiler = new TimeProfiler(LOG); + private Profiler profiler = Profiler.create(LOG); private DecoratorsProfiler decoratorsProfiler = new DecoratorsProfiler(); @Override @@ -52,9 +52,9 @@ public class PhasesTimeProfiler implements SensorExecutionHandler, DecoratorExec @Override public void onSensorExecution(SensorExecutionEvent event) { if (event.isStart()) { - profiler.start("Sensor " + event.getSensor()); + profiler.startInfo("Sensor " + event.getSensor()); } else { - profiler.stop(); + profiler.stopInfo(); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java index 6f13f3878de..ba8dc3c7542 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java @@ -20,25 +20,22 @@ package org.sonar.batch.repository; import org.picocontainer.injectors.ProviderAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.utils.TimeProfiler; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; import org.sonar.batch.protocol.input.GlobalRepositories; public class GlobalRepositoriesProvider extends ProviderAdapter { - private static final Logger LOG = LoggerFactory.getLogger(GlobalRepositoriesProvider.class); + private static final Logger LOG = Loggers.get(GlobalRepositoriesProvider.class); private GlobalRepositories globalReferentials; public GlobalRepositories provide(GlobalRepositoriesLoader loader) { if (globalReferentials == null) { - TimeProfiler profiler = new TimeProfiler(LOG).start("Load global repositories"); - try { - globalReferentials = loader.load(); - } finally { - profiler.stop(); - } + Profiler profiler = Profiler.create(LOG).startInfo("Load global repositories"); + globalReferentials = loader.load(); + profiler.stopInfo(); } return globalReferentials; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java index e69386ec498..283beec595f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java @@ -20,28 +20,25 @@ package org.sonar.batch.repository; import org.picocontainer.injectors.ProviderAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.utils.TimeProfiler; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; import org.sonar.batch.bootstrap.TaskProperties; import org.sonar.batch.protocol.input.ProjectRepositories; public class ProjectRepositoriesProvider extends ProviderAdapter { - private static final Logger LOG = LoggerFactory.getLogger(ProjectRepositoriesProvider.class); + private static final Logger LOG = Loggers.get(ProjectRepositoriesProvider.class); private ProjectRepositories projectReferentials; public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProjectReactor reactor, TaskProperties taskProps, AnalysisMode analysisMode) { if (projectReferentials == null) { - TimeProfiler profiler = new TimeProfiler(LOG).start("Load project repositories"); - try { - projectReferentials = loader.load(reactor, taskProps); - } finally { - profiler.stop(); - } + Profiler profiler = Profiler.create(LOG).startInfo("Load project repositories"); + projectReferentials = loader.load(reactor, taskProps); + profiler.stopInfo(); if (analysisMode.isPreview() && projectReferentials.lastAnalysisDate() == null) { LOG.warn("No analysis has been found on the server for this project. All issues will be marked as 'new'."); } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/profiling/ProfiledDataSource.java b/sonar-core/src/main/java/org/sonar/core/persistence/profiling/ProfiledDataSource.java index 5fbc4d49dab..0a48c71c5b2 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/profiling/ProfiledDataSource.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/profiling/ProfiledDataSource.java @@ -31,7 +31,7 @@ import java.util.Collection; public class ProfiledDataSource extends BasicDataSource { - public static final Logger SQL_LOGGER = Loggers.get("sql"); + static final Logger SQL_LOGGER = Loggers.get("sql"); private final BasicDataSource delegate; diff --git a/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java b/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java index bf312d68d25..a99652beb58 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java +++ b/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java @@ -32,7 +32,7 @@ public abstract class NonNullInputFunction implements Function { @Override public final T apply(@Nullable F input) { - Preconditions.checkNotNull(input, "Null inputs are not allowed in this function"); + Preconditions.checkArgument(input != null, "Null inputs are not allowed in this function"); return doApply(input); } diff --git a/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java b/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java index f7417d0ce48..45f4ae03548 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java @@ -33,7 +33,7 @@ public class NonNullInputFunctionTest { try { sut.apply(null); fail(); - } catch (NullPointerException e) { + } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Null inputs are not allowed in this function"); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeProfiler.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeProfiler.java index 29ffa1c55cf..8aea48239d5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeProfiler.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeProfiler.java @@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory; * @deprecated since 5.1. Replaced by {@link org.sonar.api.utils.log.Profiler} * @since 2.0 */ +@Deprecated public class TimeProfiler { private org.slf4j.Logger logger; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java index 630492277ce..cdc112fbbf7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java @@ -75,7 +75,7 @@ public class LogTester extends ExternalResource { setLevel(initialLevel); } - protected LoggerLevel getLevel() { + LoggerLevel getLevel() { return Loggers.getFactory().getLevel(); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/BaseLoggerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/BaseLoggerTest.java new file mode 100644 index 00000000000..4aa1bd0bd9b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/BaseLoggerTest.java @@ -0,0 +1,32 @@ +/* + * 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.utils.log; + +import org.junit.Test; + +public class BaseLoggerTest { + + ConsoleLogger sut = new ConsoleLogger(); + + @Test + public void log() throws Exception { + sut.log(LoggerLevel.WARN, "message"); + } +} -- 2.39.5