aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-05-28 14:25:08 +0200
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-05-28 14:25:08 +0200
commitc54140ff2d4f622daffe03dc6edef2435a386ab2 (patch)
treeb6abca5e477d47482f3e6d8ef5fcb8468fc6126e
parent0eddfffce1b680964cc0917cc0bdc5d505a7a167 (diff)
downloadsonarqube-c54140ff2d4f622daffe03dc6edef2435a386ab2.tar.gz
sonarqube-c54140ff2d4f622daffe03dc6edef2435a386ab2.zip
DEVACT-103 Modified author dao to allow author and developer creation in the same transaction scope
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java5
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java40
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java22
-rw-r--r--sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java66
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor-result.xml (renamed from sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml)0
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor.xml (renamed from sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml)0
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication-result.xml5
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication.xml5
-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
10 files changed, 123 insertions, 34 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
index 2b6b04a7122..661527f987d 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
@@ -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);
+ }
}
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 2ae8975b874..e7061bd015e 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
@@ -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);
+ }
}
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
index 1c5eb3bf23a..3ef4e110568 100644
--- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java
@@ -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");
+ }
}
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 d455675d099..ff2c102b20b 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
@@ -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/shouldInsertAuthor-result.xml
index a129400b266..a129400b266 100644
--- 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/shouldInsertAuthor-result.xml
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/shouldInsertAuthor.xml
index fb0854fccbe..fb0854fccbe 100644
--- a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsertAuthor.xml
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
index 00000000000..c9cbb085a40
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication-result.xml
@@ -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
index 00000000000..c9cbb085a40
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventAuthorsDuplication.xml
@@ -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
index 9138910692d..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts-result.xml
+++ /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
index 9138910692d..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldPreventConcurrentInserts.xml
+++ /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>