]> source.dussan.org Git - sonarqube.git/commitdiff
DEVACT-103 Modified author dao to allow author and developer creation in the same...
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Tue, 28 May 2013 12:25:08 +0000 (14:25 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Tue, 28 May 2013 12:25:08 +0000 (14:25 +0200)
12 files changed:
sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml [deleted file]

index 2b6b04a712292b33a7b2b54b63e90e7240e13ea1..661527f987d25cf7ca30bb231a4e271fe0d91dfc 100644 (file)
@@ -181,4 +181,9 @@ public class ResourceDao {
       .setName(resourceDto.getName())
       .setQualifier(resourceDto.getQualifier());
   }
+
+  public void insertUsingExistingSession(ResourceDto resourceDto, SqlSession session) {
+    ResourceMapper resourceMapper = session.getMapper(ResourceMapper.class);
+    resourceMapper.insert(resourceDto);
+  }
 }
index 2ae8975b874f2088e41558216238c9d61da9efd0..e7061bd015ea4a4683e0760f84ad78e4a217527a 100644 (file)
@@ -23,6 +23,10 @@ import org.apache.ibatis.session.SqlSession;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.ServerComponent;
 import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.resource.ResourceDao;
+import org.sonar.core.resource.ResourceDto;
+
+import java.util.Date;
 
 /**
  * @since 3.0
@@ -30,9 +34,11 @@ import org.sonar.core.persistence.MyBatis;
 public class AuthorDao implements BatchComponent, ServerComponent {
 
   private final MyBatis mybatis;
+  private final ResourceDao resourceDao;
 
-  public AuthorDao(MyBatis mybatis) {
+  public AuthorDao(MyBatis mybatis, ResourceDao resourceDao) {
     this.mybatis = mybatis;
+    this.resourceDao = resourceDao;
   }
 
   public AuthorDto selectByLogin(String login) {
@@ -77,4 +83,36 @@ public class AuthorDao implements BatchComponent, ServerComponent {
     }
   }
 
+  public void insertAuthor(String login, long personId) {
+    SqlSession session = mybatis.openSession();
+    try {
+      insertAuthor(login, personId, session);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public void insertAuthorAndDeveloper(String login, ResourceDto resourceDto) {
+    SqlSession session = mybatis.openSession();
+    try {
+      resourceDao.insertUsingExistingSession(resourceDto, session);
+      insertAuthor(login, resourceDto.getId(), session);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  private void insertAuthor(String login, long personId, SqlSession session) {
+    AuthorMapper authorMapper = session.getMapper(AuthorMapper.class);
+    Date now = new Date();
+    AuthorDto authorDto = new AuthorDto()
+      .setLogin(login)
+      .setPersonId(personId)
+      .setCreatedAt(now)
+      .setUpdatedAt(now);
+
+    authorMapper.insert(authorDto);
+  }
 }
index 1c5eb3bf23a9d960b2fa83a966a4c9c6c843d890..3ef4e1105684a7eb35713f56fdfd8985a4fcdf44 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.core.resource;
 
+import org.apache.ibatis.session.SqlSession;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.component.Component;
@@ -223,4 +224,25 @@ public class ResourceDaoTest extends AbstractDaoTestCase {
     ResourceDto fileLoadedFromDB = dao.getResource(file1.getId());
     assertThat(fileLoadedFromDB.getCreatedAt()).isNotNull();
   }
+
+  @Test
+  public void should_insert_using_existing_session() throws Exception {
+    setupData("insert");
+
+    ResourceDto file1 = new ResourceDto()
+      .setKey("org.struts:struts:org.struts.Action").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
+      .setLanguage("java").setName("Action").setLongName("org.struts.Action");
+    ResourceDto file2 = new ResourceDto()
+      .setKey("org.struts:struts:org.struts.Filter").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
+      .setLanguage("java").setName("Filter").setLongName("org.struts.Filter");
+
+    SqlSession session = getMyBatis().openSession();
+
+    dao.insertUsingExistingSession(file1, session);
+    dao.insertUsingExistingSession(file2, session);
+
+    session.rollback();
+
+    assertEmptyTables("projects");
+  }
 }
index d455675d0992e02f896aef1786a6f06bd0767494..ff2c102b20b72d867cfb51c6dd3ffdd991649105 100644 (file)
@@ -22,8 +22,11 @@ package org.sonar.core.user;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.resource.ResourceDao;
+import org.sonar.core.resource.ResourceDto;
 
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
 
 public class AuthorDaoTest extends AbstractDaoTestCase {
 
@@ -31,7 +34,7 @@ public class AuthorDaoTest extends AbstractDaoTestCase {
 
   @Before
   public void setUp() {
-    dao = new AuthorDao(getMyBatis());
+    dao = new AuthorDao(getMyBatis(), new ResourceDao(getMyBatis()));
   }
 
   @Test
@@ -47,16 +50,12 @@ public class AuthorDaoTest extends AbstractDaoTestCase {
   }
 
   @Test
-  public void shouldInsert() {
-    setupData("shouldInsert");
+  public void shouldInsertAuthor() {
+    setupData("shouldInsertAuthor");
 
-    AuthorDto authorDto = new AuthorDto()
-      .setLogin("godin")
-      .setPersonId(13L);
+    dao.insertAuthor("godin", 13L);
 
-    dao.insert(authorDto);
-
-    checkTables("shouldInsert", new String[]{"created_at", "updated_at"}, "authors");
+    checkTables("shouldInsertAuthor", new String[]{"created_at", "updated_at"}, "authors");
   }
 
   @Test
@@ -68,17 +67,46 @@ public class AuthorDaoTest extends AbstractDaoTestCase {
   }
 
   @Test
-  public void shouldPreventConcurrentInserts() {
-    setupData("shouldPreventConcurrentInserts");
+  public void shouldInsertAuthorAndDeveloper() throws Exception {
+    setupData("shouldInsertAuthorAndDeveloper");
 
-    // already exists in database with id 1
-    AuthorDto authorDto = new AuthorDto()
-      .setLogin("godin")
-      .setPersonId(13L);
-    dao.insert(authorDto);
+    String login = "developer@company.net";
+    ResourceDto resourceDto = new ResourceDto().setName(login).setQualifier("DEV");
+    dao.insertAuthorAndDeveloper(login, resourceDto);
 
-    checkTables("shouldPreventConcurrentInserts", new String[]{"created_at", "updated_at"}, "authors");
-    assertThat(authorDto.getId()).isEqualTo(1L);
-    assertThat(authorDto.getPersonId()).isEqualTo(100L);
+    checkTables("shouldInsertAuthorAndDeveloper",
+      new String[]{"created_at", "updated_at", "copy_resource_id", "description", "enabled", "kee", "language", "long_name", "person_id", "root_id", "scope"},
+      "authors", "projects");
+  }
+
+  @Test
+  public void shouldPreventAuthorsDuplication() {
+    setupData("shouldPreventAuthorsDuplication");
+
+    try {
+      dao.insertAuthor("godin", 20L);
+      fail();
+    } catch (RuntimeException ex) {
+    }
+
+    checkTables("shouldPreventAuthorsDuplication", new String[]{"created_at", "updated_at"}, "authors");
+  }
+
+  @Test
+  public void shouldPreventAuthorsAndDevelopersDuplication() throws Exception {
+    setupData("shouldPreventAuthorsAndDevelopersDuplication");
+
+    String login = "developer@company.net";
+    ResourceDto resourceDto = new ResourceDto().setName(login).setQualifier("DEV");
+
+    try {
+      dao.insertAuthorAndDeveloper("developer@company.net", resourceDto);
+      fail();
+    } catch (RuntimeException ex) {
+    }
+
+    checkTables("shouldPreventAuthorsAndDevelopersDuplication",
+      new String[]{"created_at", "updated_at", "copy_resource_id", "description", "enabled", "kee", "language", "long_name", "person_id", "root_id", "scope"},
+      "authors", "projects");
   }
 }
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml
deleted file mode 100644 (file)
index a129400..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<dataset>
-
-  <authors
-    id="1"
-    person_id="13"
-    login="godin" />
-
-</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml
deleted file mode 100644 (file)
index fb0854f..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-<dataset>
-</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor-result.xml
new file mode 100644 (file)
index 0000000..a129400
--- /dev/null
@@ -0,0 +1,8 @@
+<dataset>
+
+  <authors
+    id="1"
+    person_id="13"
+    login="godin" />
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor.xml
new file mode 100644 (file)
index 0000000..fb0854f
--- /dev/null
@@ -0,0 +1,2 @@
+<dataset>
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication-result.xml
new file mode 100644 (file)
index 0000000..c9cbb08
--- /dev/null
@@ -0,0 +1,5 @@
+<dataset>
+
+  <authors id="1" person_id="10" login="godin" />
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication.xml
new file mode 100644 (file)
index 0000000..c9cbb08
--- /dev/null
@@ -0,0 +1,5 @@
+<dataset>
+
+  <authors id="1" person_id="10" login="godin" />
+
+</dataset>
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
deleted file mode 100644 (file)
index 9138910..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<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
deleted file mode 100644 (file)
index 9138910..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<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>