]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 31 May 2013 13:12:29 +0000 (15:12 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 31 May 2013 13:12:46 +0000 (15:12 +0200)
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java
sonar-core/src/main/java/org/sonar/core/purge/PurgeProfiler.java
sonar-core/src/test/java/org/sonar/core/purge/PurgeProfilerTest.java

index afafdb93a2a711fa2f4df7ceb7aa37e2b3905d8c..19f607b69a7a42f2258896138c30e594d3888b94 100644 (file)
@@ -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));
   }
index 93669697408624cbe4dfc74594420162209e8c2f..4469ad0ac651e36d929a9aace917859a52b6fb6c 100644 (file)
@@ -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<PurgeConfiguration>() {
       @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<PurgeConfiguration>() {
       @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));
   }
 }
index 0d1135077a142d1c149d262b1c9df25636214eac..f9c27da9bfce491b489a00b07d10c8004f83d720 100644 (file)
@@ -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<Entry<String, Long>> data = new ArrayList<Map.Entry<String, Long>>(durations.entrySet());
     Collections.sort(data, new Comparator<Entry<String, Long>>() {
       @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());
     }
   }
 
index 0454295d97cfa0baf76c93bb385d81e0434298e7..b9b6956c400fc41ee0834cf5f0f8d8ba67137531 100644 (file)
  */
 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 {