aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-10-19 17:18:03 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-10-19 17:18:03 +0200
commit602700de57516592e661e1a3b40fc15a180cd08e (patch)
tree491bcff8fe0c05326b1254a3c944227d6aa8f40a /sonar-core
parent0fb0a76254e9aa12c7d1e3050c668aa81ac75a78 (diff)
downloadsonarqube-602700de57516592e661e1a3b40fc15a180cd08e.tar.gz
sonarqube-602700de57516592e661e1a3b40fc15a180cd08e.zip
SONAR-3889 Add unit tests
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java1
-rw-r--r--sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java35
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml7
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml7
4 files changed, 39 insertions, 11 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java b/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
index 104d8b90905..0b709f8d45d 100644
--- a/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
@@ -66,6 +66,7 @@ public class AuthorDao implements BatchComponent, ServerComponent {
AuthorDto persistedAuthor = mapper.selectByLogin(authorDto.getLogin());
if (persistedAuthor != null) {
authorDto.setId(persistedAuthor.getId());
+ authorDto.setPersonId(persistedAuthor.getPersonId());
} else {
throw e;
}
diff --git a/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
index db93f779987..68e51a2eef0 100644
--- a/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
@@ -23,9 +23,7 @@ import org.junit.Before;
import org.junit.Test;
import org.sonar.core.persistence.AbstractDaoTestCase;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class AuthorDaoTest extends AbstractDaoTestCase {
@@ -41,11 +39,11 @@ public class AuthorDaoTest extends AbstractDaoTestCase {
setupData("shouldSelectByLogin");
AuthorDto authorDto = dao.selectByLogin("godin");
- assertThat(authorDto.getId(), is(1L));
- assertThat(authorDto.getPersonId(), is(13L));
- assertThat(authorDto.getLogin(), is("godin"));
+ assertThat(authorDto.getId()).isEqualTo(1L);
+ assertThat(authorDto.getPersonId()).isEqualTo(13L);
+ assertThat(authorDto.getLogin()).isEqualTo("godin");
- assertThat(dao.selectByLogin("simon"), is(nullValue()));
+ assertThat(dao.selectByLogin("simon")).isNull();
}
@Test
@@ -53,8 +51,8 @@ public class AuthorDaoTest extends AbstractDaoTestCase {
setupData("shouldInsert");
AuthorDto authorDto = new AuthorDto()
- .setLogin("godin")
- .setPersonId(13L);
+ .setLogin("godin")
+ .setPersonId(13L);
dao.insert(authorDto);
@@ -65,7 +63,22 @@ public class AuthorDaoTest extends AbstractDaoTestCase {
public void countDeveloperLogins() {
setupData("countDeveloperLogins");
- assertThat(dao.countDeveloperLogins(1L), is(2));
- assertThat(dao.countDeveloperLogins(98765L), is(0));
+ assertThat(dao.countDeveloperLogins(1L)).isEqualTo(2);
+ assertThat(dao.countDeveloperLogins(98765L)).isEqualTo(0);
+ }
+
+ @Test
+ public void shouldPreventConcurrentInserts() {
+ setupData("shouldPreventConcurrentInserts");
+
+ // already exists in database with id 1
+ AuthorDto authorDto = new AuthorDto()
+ .setLogin("godin")
+ .setPersonId(13L);
+ dao.insert(authorDto);
+
+ checkTables("shouldPreventConcurrentInserts", new String[]{"created_at", "updated_at"}, "authors");
+ assertThat(authorDto.getId()).isEqualTo(1L);
+ assertThat(authorDto.getPersonId()).isEqualTo(100L);
}
}
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml
new file mode 100644
index 00000000000..9138910692d
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml
@@ -0,0 +1,7 @@
+<dataset>
+
+ <authors id="1" person_id="100" login="godin" />
+ <authors id="2" person_id="100" login="evgeny" />
+ <authors id="3" person_id="200" login="simon" />
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml
new file mode 100644
index 00000000000..9138910692d
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml
@@ -0,0 +1,7 @@
+<dataset>
+
+ <authors id="1" person_id="100" login="godin" />
+ <authors id="2" person_id="100" login="evgeny" />
+ <authors id="3" person_id="200" login="simon" />
+
+</dataset>