From 467f27509670c29a08f5555bd356b4eeda10aee2 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 10 Jan 2014 16:23:38 +0100 Subject: [PATCH] Fix issue when log message contains variable to replace but there are no arguments. --- .../sonar/core/profiling/LoggingWatch.java | 16 +++- .../core/profiling/LoggingWatchTest.java | 74 +++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 sonar-core/src/test/java/org/sonar/core/profiling/LoggingWatchTest.java diff --git a/sonar-core/src/main/java/org/sonar/core/profiling/LoggingWatch.java b/sonar-core/src/main/java/org/sonar/core/profiling/LoggingWatch.java index 2e29aa7bf04..286be7731a1 100644 --- a/sonar-core/src/main/java/org/sonar/core/profiling/LoggingWatch.java +++ b/sonar-core/src/main/java/org/sonar/core/profiling/LoggingWatch.java @@ -19,22 +19,32 @@ */ package org.sonar.core.profiling; +import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; +import org.sonar.api.utils.System2; class LoggingWatch extends StopWatch { private Logger logger; + private System2 system; private long startTimeInMillis; LoggingWatch(Logger logger) { + this(logger, System2.INSTANCE); + } + + @VisibleForTesting + LoggingWatch(Logger logger, System2 system) { + this.system = system; this.logger = logger; - this.startTimeInMillis = System.currentTimeMillis(); + this.startTimeInMillis = system.now(); } @Override public void stop(String message, Object... args) { - long endTimeInMillis = System.currentTimeMillis(); - logger.info("{}ms {}", Long.valueOf(endTimeInMillis - startTimeInMillis), String.format(message, args)); + long endTimeInMillis = system.now(); + String messageToDisplay = (args.length == 0) ? message : String.format(message, args); + logger.info("{}ms {}", Long.valueOf(endTimeInMillis - startTimeInMillis), messageToDisplay); } } diff --git a/sonar-core/src/test/java/org/sonar/core/profiling/LoggingWatchTest.java b/sonar-core/src/test/java/org/sonar/core/profiling/LoggingWatchTest.java new file mode 100644 index 00000000000..0587319bae4 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/profiling/LoggingWatchTest.java @@ -0,0 +1,74 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.core.profiling; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.slf4j.Logger; +import org.sonar.api.utils.System2; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class LoggingWatchTest { + + @Mock + Logger logger; + + @Mock + System2 system; + + LoggingWatch loggingWatch; + + @Before + public void setUp() throws Exception { + doReturn(1000l).when(system).now(); + loggingWatch = new LoggingWatch(logger, system); + } + + @Test + public void stop_with_params() throws Exception { + doReturn(1500l).when(system).now(); + + loggingWatch.stop("Create '%s' elements of type '%s'", 10, "test"); + verify(logger).info("{}ms {}", 500l, "Create '10' elements of type 'test'"); + } + + @Test + public void stop_without_params() throws Exception { + doReturn(1500l).when(system).now(); + + loggingWatch.stop("End of process"); + verify(logger).info("{}ms {}", 500l, "End of process"); + } + + @Test + public void stop_with_variable_but_without_params() throws Exception { + doReturn(1500l).when(system).now(); + + loggingWatch.stop("End of process at %s"); + verify(logger).info("{}ms {}", 500l, "End of process at %s"); + } +} -- 2.39.5