From 726c17ed044d5e23a4ee1465a8a8c487ac1a9646 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 20 Feb 2012 22:05:03 +0100 Subject: [PATCH] Add unit tests and fix some quality flaws --- .../period/DefaultPeriodCleaner.java | 10 ++- .../dbcleaner/period/package-info.java | 25 ++++++ .../period/DefaultPeriodCleanerTest.java | 77 +++++++++++++++++++ .../sonar/core/purge/PurgeSnapshotQuery.java | 2 +- .../core/purge/PurgeableSnapshotDto.java | 3 +- .../core/persistence/BatchSessionTest.java | 54 +++++++++++++ .../test/i18n/BundleSynchronizedMatcher.java | 2 +- 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java create mode 100644 plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java create mode 100644 sonar-core/src/test/java/org/sonar/core/persistence/BatchSessionTest.java diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java index 5c092c61c04..efc9ff49d64 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java @@ -19,6 +19,7 @@ */ 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; @@ -47,10 +48,13 @@ public class DefaultPeriodCleaner implements PeriodCleaner { } public void purge(long projectId) { - List 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 filters) { + List history = selectProjectSnapshots(projectId); + for (Filter filter : filters) { filter.log(); delete(filter.filter(history)); } diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java new file mode 100644 index 00000000000..1e098ea24f8 --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java @@ -0,0 +1,25 @@ +/* + * 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; + diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java new file mode 100644 index 00000000000..ca806d648fc --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java @@ -0,0 +1,77 @@ +/* + * 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 newRootSnapshotQuery() { + return new BaseMatcher() { + 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() { + public Object answer(InvocationOnMock invocation) throws Throwable { + return invocation.getArguments()[0]; + } + }); + return filter1; + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java index 5e9d085372f..7fca0f6773a 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java @@ -69,7 +69,7 @@ public final class PurgeSnapshotQuery { } 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; } diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java index f51b1218c61..826b158b336 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java @@ -46,8 +46,9 @@ public class PurgeableSnapshotDto implements Comparable { 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) { diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/BatchSessionTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/BatchSessionTest.java new file mode 100644 index 00000000000..dc21a9868e3 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/persistence/BatchSessionTest.java @@ -0,0 +1,54 @@ +/* + * 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(); + } +} diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java index 7a7bbdca8b6..853365c3bcf 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java @@ -129,7 +129,7 @@ public class BundleSynchronizedMatcher extends BaseMatcher { 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); } -- 2.39.5