From be0fdd37d9b4cd911b88009114d2264f37035160 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 31 May 2013 15:12:29 +0200 Subject: [PATCH] Fix some quality flaws --- .../plugins/dbcleaner/DefaultPurgeTask.java | 10 +++--- .../dbcleaner/DefaultPurgeTaskTest.java | 19 +++++++---- .../org/sonar/core/purge/PurgeProfiler.java | 11 ++---- .../sonar/core/purge/PurgeProfilerTest.java | 34 +++++++------------ 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java index afafdb93a2a..19f607b69a7 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java @@ -62,9 +62,9 @@ public class DefaultPurgeTask implements PurgeTask { doPurge(resourceId); if (settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)) { long duration = System.currentTimeMillis() - start; - System.out.println("\n -------- Profiling for purge: " + TimeUtils.formatDuration(duration) + " --------\n"); - profiler.dump(duration); - System.out.println("\n -------- End of profiling for purge --------\n"); + LOG.info("\n -------- Profiling for purge: " + TimeUtils.formatDuration(duration) + " --------\n"); + profiler.dump(duration, LOG); + LOG.info("\n -------- End of profiling for purge --------\n"); } return this; } @@ -88,9 +88,9 @@ public class DefaultPurgeTask implements PurgeTask { } private PurgeConfiguration newConf(long resourceId) { - String[] scopes = new String[]{Scopes.FILE}; + String[] scopes = new String[] {Scopes.FILE}; if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) { - scopes = new String[]{Scopes.DIRECTORY, Scopes.FILE}; + scopes = new String[] {Scopes.DIRECTORY, Scopes.FILE}; } return new PurgeConfiguration(resourceId, scopes, settings.getInt(DbCleanerConstants.DAYS_BEFORE_DELETING_CLOSED_ISSUES)); } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java index 93669697408..4469ad0ac65 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java @@ -19,6 +19,7 @@ */ package org.sonar.plugins.dbcleaner; +import ch.qos.logback.classic.Logger; import org.junit.Test; import org.mockito.ArgumentMatcher; import org.sonar.api.CoreProperties; @@ -33,7 +34,11 @@ import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class DefaultPurgeTaskTest { @Test @@ -48,8 +53,8 @@ public class DefaultPurgeTaskTest { verify(purgeDao).purge(argThat(new ArgumentMatcher() { @Override public boolean matches(Object o) { - PurgeConfiguration conf = (PurgeConfiguration)o; - return conf.rootProjectId()==1L && conf.scopesWithoutHistoricalData().length==1 && conf.scopesWithoutHistoricalData()[0].equals(Scopes.FILE); + PurgeConfiguration conf = (PurgeConfiguration) o; + return conf.rootProjectId() == 1L && conf.scopesWithoutHistoricalData().length == 1 && conf.scopesWithoutHistoricalData()[0].equals(Scopes.FILE); } })); } @@ -65,9 +70,9 @@ public class DefaultPurgeTaskTest { verify(purgeDao).purge(argThat(new ArgumentMatcher() { @Override public boolean matches(Object o) { - PurgeConfiguration conf = (PurgeConfiguration)o; - return conf.rootProjectId()==1L && - conf.scopesWithoutHistoricalData().length==2 && + PurgeConfiguration conf = (PurgeConfiguration) o; + return conf.rootProjectId() == 1L && + conf.scopesWithoutHistoricalData().length == 2 && conf.scopesWithoutHistoricalData()[0].equals(Scopes.DIRECTORY) && conf.scopesWithoutHistoricalData()[1].equals(Scopes.FILE); } @@ -97,6 +102,6 @@ public class DefaultPurgeTaskTest { DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class), profiler); task.purge(1L); - verify(profiler).dump(anyLong()); + verify(profiler).dump(anyLong(), any(Logger.class)); } } diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeProfiler.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeProfiler.java index 0d1135077a1..f9c27da9bfc 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeProfiler.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeProfiler.java @@ -20,9 +20,9 @@ package org.sonar.core.purge; import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; import org.sonar.api.utils.TimeUtils; -import java.io.PrintStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -66,12 +66,7 @@ public class PurgeProfiler { durations.put(currentTable, cumulatedDuration + (clock.now() - startTime)); } - public void dump(long totalTime) { - dump(totalTime, System.out); - } - - @VisibleForTesting - void dump(long totalTime, PrintStream stream) { + public void dump(long totalTime, Logger logger) { List> data = new ArrayList>(durations.entrySet()); Collections.sort(data, new Comparator>() { @Override @@ -84,7 +79,7 @@ public class PurgeProfiler { StringBuilder sb = new StringBuilder(); sb.append(" o ").append(entry.getKey()).append(": ").append(TimeUtils.formatDuration(entry.getValue())) .append(" (").append((int) (entry.getValue() / percent)).append("%)"); - stream.println(sb.toString()); + logger.info(sb.toString()); } } diff --git a/sonar-core/src/test/java/org/sonar/core/purge/PurgeProfilerTest.java b/sonar-core/src/test/java/org/sonar/core/purge/PurgeProfilerTest.java index 0454295d97c..b9b6956c400 100644 --- a/sonar-core/src/test/java/org/sonar/core/purge/PurgeProfilerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/purge/PurgeProfilerTest.java @@ -19,33 +19,25 @@ */ package org.sonar.core.purge; -import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.slf4j.Logger; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.contains; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class PurgeProfilerTest { private MockedClock clock; private PurgeProfiler profiler; - private ByteArrayOutputStream baos; - private PrintStream ps; + private Logger logger; @Before public void prepare() { clock = new MockedClock(); profiler = new PurgeProfiler(clock); - baos = new ByteArrayOutputStream(); - ps = new PrintStream(baos); - } - - @After - public void cleanup() { - ps.close(); + logger = mock(Logger.class); } @Test @@ -62,10 +54,9 @@ public class PurgeProfilerTest { clock.sleep(8); profiler.stop(); - profiler.dump(50, ps); - String content = baos.toString(); - assertThat(content).contains("foo: 18ms"); - assertThat(content).contains("bar: 5ms"); + profiler.dump(50, logger); + verify(logger).info(contains("foo: 18ms")); + verify(logger).info(contains("bar: 5ms")); } @Test @@ -84,10 +75,9 @@ public class PurgeProfilerTest { clock.sleep(8); profiler.stop(); - profiler.dump(50, ps); - String content = baos.toString(); - assertThat(content).contains("foo: 8ms"); - assertThat(content).contains("bar: 5ms"); + profiler.dump(50, logger); + verify(logger).info(contains("foo: 8ms")); + verify(logger).info(contains("bar: 5ms")); } private class MockedClock extends org.sonar.core.purge.PurgeProfiler.Clock { -- 2.39.5