From 726c17ed044d5e23a4ee1465a8a8c487ac1a9646 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 20 Feb 2012 22:05:03 +0100 Subject: Add unit tests and fix some quality flaws --- .../org/sonar/core/purge/PurgeSnapshotQuery.java | 2 +- .../org/sonar/core/purge/PurgeableSnapshotDto.java | 3 +- .../sonar/core/persistence/BatchSessionTest.java | 54 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 sonar-core/src/test/java/org/sonar/core/persistence/BatchSessionTest.java (limited to 'sonar-core/src') 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(); + } +} -- cgit v1.2.3