From 602700de57516592e661e1a3b40fc15a180cd08e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 19 Oct 2012 17:18:03 +0200 Subject: [PATCH] SONAR-3889 Add unit tests --- .../java/org/sonar/core/user/AuthorDao.java | 1 + .../org/sonar/core/user/AuthorDaoTest.java | 35 +++++++++++++------ .../shouldPreventConcurrentInserts-result.xml | 7 ++++ .../shouldPreventConcurrentInserts.xml | 7 ++++ 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml 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 @@ + + + + + + + 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 @@ + + + + + + + -- 2.39.5