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
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) {
}
}
+ 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);
+ }
}
*/
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;
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");
+ }
}
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 {
@Before
public void setUp() {
- dao = new AuthorDao(getMyBatis());
+ dao = new AuthorDao(getMyBatis(), new ResourceDao(getMyBatis()));
}
@Test
}
@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
}
@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");
}
}