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;
}
}
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));
}
*/
package org.sonar.plugins.dbcleaner;
+import ch.qos.logback.classic.Logger;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.sonar.api.CoreProperties;
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
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);
}
}));
}
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);
}
DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class), profiler);
task.purge(1L);
- verify(profiler).dump(anyLong());
+ verify(profiler).dump(anyLong(), any(Logger.class));
}
}
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;
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
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());
}
}
*/
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
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
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 {