]> source.dussan.org Git - sonarqube.git/commitdiff
Fix issue when log message contains variable to replace but there are no arguments.
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 10 Jan 2014 15:23:38 +0000 (16:23 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 10 Jan 2014 15:24:22 +0000 (16:24 +0100)
sonar-core/src/main/java/org/sonar/core/profiling/LoggingWatch.java
sonar-core/src/test/java/org/sonar/core/profiling/LoggingWatchTest.java [new file with mode: 0644]

index 2e29aa7bf04ee697a018aad73b0176028a475be3..286be7731a1c421b72512987c47bd8a82a4a63c2 100644 (file)
  */
 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 (file)
index 0000000..0587319
--- /dev/null
@@ -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");
+  }
+}