]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3889 Add unit tests
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 19 Oct 2012 15:18:03 +0000 (17:18 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 19 Oct 2012 15:18:03 +0000 (17:18 +0200)
sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml [new file with mode: 0644]

index 104d8b90905e726458062111eff08459ac17ddb3..0b709f8d45d1c6ba4f90500618354f50894cb7a5 100644 (file)
@@ -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;
       }
index db93f7799873f60d8b18f2c648e6fdb9f351a7ac..68e51a2eef0af9067cfd611ad107da10edf6ff09 100644 (file)
@@ -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 (file)
index 0000000..9138910
--- /dev/null
@@ -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 (file)
index 0000000..9138910
--- /dev/null
@@ -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>