*/
package org.sonar.plugins.dbcleaner.period;
+import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Settings;
}
public void purge(long projectId) {
- List<PurgeableSnapshotDto> history = selectProjectSnapshots(projectId);
+ doPurge(projectId, new Filters(settings).getFilters());
+ }
- Filters filters = new Filters(settings);
- for (Filter filter : filters.getFilters()) {
+ @VisibleForTesting
+ void doPurge(long projectId, List<Filter> filters) {
+ List<PurgeableSnapshotDto> history = selectProjectSnapshots(projectId);
+ for (Filter filter : filters) {
filter.log();
delete(filter.filter(history));
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.plugins.dbcleaner.period;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.dbcleaner.period;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.sonar.api.config.Settings;
+import org.sonar.core.purge.PurgeDao;
+import org.sonar.core.purge.PurgeSnapshotQuery;
+import org.sonar.core.purge.PurgeableSnapshotDto;
+
+import java.util.Arrays;
+import java.util.Date;
+
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Mockito.*;
+
+public class DefaultPeriodCleanerTest {
+
+
+ @Test
+ public void doPurge() {
+ PurgeDao dao = mock(PurgeDao.class);
+ when(dao.selectPurgeableSnapshots(123L)).thenReturn(Arrays.asList(
+ new PurgeableSnapshotDto().setSnapshotId(999L).setDate(new Date())));
+ Filter filter1 = newLazyFilter();
+ Filter filter2 = newLazyFilter();
+
+ DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, mock(Settings.class));
+ cleaner.doPurge(123L, Arrays.asList(filter1, filter2));
+
+ verify(filter1).log();
+ verify(filter2).log();
+ verify(dao, times(2)).deleteSnapshots(argThat(newRootSnapshotQuery()));
+ }
+
+ private BaseMatcher<PurgeSnapshotQuery> newRootSnapshotQuery() {
+ return new BaseMatcher<PurgeSnapshotQuery>() {
+ public boolean matches(Object o) {
+ return ((PurgeSnapshotQuery) o).getRootSnapshotId() == 999L;
+ }
+
+ public void describeTo(Description description) {
+ }
+ };
+ }
+
+ private Filter newLazyFilter() {
+ Filter filter1 = mock(Filter.class);
+ when(filter1.filter(anyListOf(PurgeableSnapshotDto.class))).thenAnswer(new Answer<Object>() {
+ public Object answer(InvocationOnMock invocation) throws Throwable {
+ return invocation.getArguments()[0];
+ }
+ });
+ return filter1;
+ }
+}
}
public PurgeSnapshotQuery setStatus(String[] status) {
- this.status = status;
+ this.status = status; //NOSONAR org.sonar.core.purge.PurgeSnapshotQuery.setStatus(String[]) may expose internal representation
return this;
}
return isLast;
}
- public void setDate(Date date) {
+ public PurgeableSnapshotDto setDate(Date date) {
this.date = date;//NOSONAR May expose internal representation by incorporating reference to mutable object
+ return this;
}
public PurgeableSnapshotDto setSnapshotId(long snapshotId) {
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.core.persistence;
+
+import org.apache.ibatis.session.SqlSession;
+import org.junit.Test;
+
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Mockito.*;
+
+public class BatchSessionTest {
+ @Test
+ public void shouldCommitWhenReachingBatchSize() {
+ SqlSession mybatisSession = mock(SqlSession.class);
+ BatchSession session = new BatchSession(mybatisSession, 10);
+
+ for (int i = 0; i < 9; i++) {
+ session.insert("id" + i);
+ verify(mybatisSession).insert("id" + i);
+ verify(mybatisSession, never()).commit();
+ verify(mybatisSession, never()).commit(anyBoolean());
+ }
+ session.insert("id9");
+ verify(mybatisSession).commit();
+ }
+
+ @Test
+ public void shouldResetCounterAfterCommit() {
+ SqlSession mybatisSession = mock(SqlSession.class);
+ BatchSession session = new BatchSession(mybatisSession, 10);
+
+ for (int i = 0; i < 35; i++) {
+ session.insert("id" + i);
+ }
+ verify(mybatisSession, times(3)).commit();
+ }
+}
writer = new FileWriter(dumpFile);
writer.write(details);
} catch (IOException e) {
- System.out.println("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'.");
+ throw new RuntimeException("Unable to write the report to 'target/l10n/" + bundleName + ".report.txt'");
} finally {
IOUtils.closeQuietly(writer);
}