]> source.dussan.org Git - sonarqube.git/commitdiff
Add unit tests and fix some quality flaws
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 20 Feb 2012 21:05:03 +0000 (22:05 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 20 Feb 2012 21:05:03 +0000 (22:05 +0100)
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleaner.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/period/package-info.java [new file with mode: 0644]
plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/period/DefaultPeriodCleanerTest.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/purge/PurgeSnapshotQuery.java
sonar-core/src/main/java/org/sonar/core/purge/PurgeableSnapshotDto.java
sonar-core/src/test/java/org/sonar/core/persistence/BatchSessionTest.java [new file with mode: 0644]
sonar-testing-harness/src/main/java/org/sonar/test/i18n/BundleSynchronizedMatcher.java

index 5c092c61c041834f91efe7b603646518919e8263..efc9ff49d642882e3a412f5c9b7cea59d124d4ca 100644 (file)
@@ -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<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));
     }
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 (file)
index 0000000..1e098ea
--- /dev/null
@@ -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 (file)
index 0000000..ca806d6
--- /dev/null
@@ -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<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;
+  }
+}
index 5e9d085372f0d764cb8c14cba183fbb06323dbdd..7fca0f6773a3ff22dbd8298a521244c90bc93cc2 100644 (file)
@@ -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;
   }
 
index f51b1218c6109e21e9b860bf3aca3e4e7a0b4196..826b158b336def704ba9b25cf6f9616ec50e0930 100644 (file)
@@ -46,8 +46,9 @@ public class PurgeableSnapshotDto implements Comparable<PurgeableSnapshotDto> {
     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 (file)
index 0000000..dc21a98
--- /dev/null
@@ -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();
+    }
+}
index 7a7bbdca8b6fa29450cdb3ec142ebfa549b64632..853365c3bcf4d8069a1e90783540d87354ca0709 100644 (file)
@@ -129,7 +129,7 @@ public class BundleSynchronizedMatcher extends BaseMatcher<String> {
       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);
     }