diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-10-26 18:23:11 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-10-26 18:23:20 +0200 |
commit | a589e2539c1d163337daf23b682c95d492b3fbb9 (patch) | |
tree | 45435dcee69dd72a3f11a7fc44612b5dc6b60c81 /sonar-core | |
parent | a6cbc7f7c54c2600c21eed8f361addaa074983b8 (diff) | |
download | sonarqube-a589e2539c1d163337daf23b682c95d492b3fbb9.tar.gz sonarqube-a589e2539c1d163337daf23b682c95d492b3fbb9.zip |
SONAR-2642 add DAOs to picocontainer + improve unit tests
Diffstat (limited to 'sonar-core')
14 files changed, 177 insertions, 28 deletions
diff --git a/sonar-core/src/main/java/org/sonar/persistence/DdlUtils.java b/sonar-core/src/main/java/org/sonar/persistence/DdlUtils.java index 14dd56eef2c..520bf1b4c55 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/DdlUtils.java +++ b/sonar-core/src/main/java/org/sonar/persistence/DdlUtils.java @@ -45,6 +45,7 @@ public final class DdlUtils { } /** + * TODO to be replaced by mybatis ScriptRunner * The connection is commited in this method but not closed. */ public static void execute(Connection connection, String dialect) { diff --git a/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java index 82e23455b70..fe5133cf013 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/persistence/MyBatis.java @@ -50,8 +50,10 @@ public class MyBatis implements BatchComponent, ServerComponent { conf.setUseGeneratedKeys(true); conf.setLazyLoadingEnabled(false); - loadMapper(conf, DuplicationMapper.class, "Duplication", Duplication.class); - loadMapper(conf, RuleMapper.class, "Rule", Rule.class); + loadAlias(conf, "Duplication", Duplication.class); + loadAlias(conf, "Rule", Rule.class); + loadMapper(conf, DuplicationMapper.class); + loadMapper(conf, RuleMapper.class); sessionFactory = new SqlSessionFactoryBuilder().build(conf); return this; @@ -69,11 +71,11 @@ public class MyBatis implements BatchComponent, ServerComponent { return sessionFactory.openSession(type); } - private void loadMapper(Configuration conf, Class mapperClass, String alias, Class dtoClass) throws IOException { + + private void loadMapper(Configuration conf, Class mapperClass) throws IOException { // trick to use database-specific XML files for a single Mapper Java interface - InputStream input = getClass().getResourceAsStream("/" + StringUtils.replace(mapperClass.getName(), ".", "/") + ".xml"); + InputStream input = getPathToMapper(mapperClass); try { - conf.getTypeAliasRegistry().registerAlias(alias, dtoClass); XMLMapperBuilder mapperParser = new XMLMapperBuilder(input, conf, mapperClass.getName(), conf.getSqlFragments()); mapperParser.parse(); conf.addLoadedResource(mapperClass.getName()); @@ -83,6 +85,18 @@ public class MyBatis implements BatchComponent, ServerComponent { } } + private InputStream getPathToMapper(Class mapperClass) { + InputStream input = getClass().getResourceAsStream("/" + StringUtils.replace(mapperClass.getName(), ".", "/") + "-" + database.getDialect().getId() + ".xml"); + if (input == null) { + input = getClass().getResourceAsStream("/" + StringUtils.replace(mapperClass.getName(), ".", "/") + ".xml"); + } + return input; + } + + private void loadAlias(Configuration conf, String alias, Class dtoClass) { + conf.getTypeAliasRegistry().registerAlias(alias, dtoClass); + } + private static JdbcTransactionFactory createTransactionFactory() { return new JdbcTransactionFactory(); } diff --git a/sonar-core/src/main/java/org/sonar/persistence/dao/DaoUtils.java b/sonar-core/src/main/java/org/sonar/persistence/dao/DaoUtils.java new file mode 100644 index 00000000000..3af77afc30a --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dao/DaoUtils.java @@ -0,0 +1,32 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.dao; + +import java.util.Arrays; +import java.util.List; + +public final class DaoUtils { + private DaoUtils() { + } + + public static List<Class> getDaoClasses() { + return Arrays.<Class>asList(RuleDao.class, DuplicationDao.class); + } +} diff --git a/sonar-core/src/main/java/org/sonar/persistence/dao/RuleDao.java b/sonar-core/src/main/java/org/sonar/persistence/dao/RuleDao.java index 6391990c517..1332b04d44a 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/dao/RuleDao.java +++ b/sonar-core/src/main/java/org/sonar/persistence/dao/RuleDao.java @@ -46,4 +46,14 @@ public class RuleDao implements BatchComponent, ServerComponent { } } + public Rule selectById(Long id) { + SqlSession sqlSession = mybatis.openSession(); + try { + RuleMapper mapper = sqlSession.getMapper(RuleMapper.class); + return mapper.selectById(id); + } finally { + sqlSession.close(); + } + } + } diff --git a/sonar-core/src/test/java/org/sonar/persistence/DdlUtilsTest.java b/sonar-core/src/test/java/org/sonar/persistence/DdlUtilsTest.java index 29de752bd25..e8ea95f74ea 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/DdlUtilsTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/DdlUtilsTest.java @@ -21,6 +21,7 @@ package org.sonar.persistence; import org.apache.derby.jdbc.EmbeddedDriver; import org.hamcrest.core.Is; +import org.junit.BeforeClass; import org.junit.Test; import java.sql.Connection; @@ -33,6 +34,10 @@ import static org.junit.Assert.assertThat; public class DdlUtilsTest { + static { + DerbyUtils.fixDerbyLogs(); + } + @Test public void shouldSupportOnlyDerby() { assertThat(DdlUtils.supportsDialect("derby"), Is.is(true)); @@ -56,9 +61,6 @@ public class DdlUtilsTest { connection.close(); assertThat(tables, greaterThan(30)); - try { - DriverManager.getConnection("jdbc:derby:memory:sonar;drop=true"); - } catch (Exception e) { - } + DerbyUtils.dropInMemoryDatabase(); } } diff --git a/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java b/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java index 05922626fae..2f11a05c619 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java @@ -24,13 +24,18 @@ import org.hamcrest.core.Is; import org.junit.Test; import org.sonar.api.config.Settings; -import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; import static org.junit.Assert.assertThat; public class DefaultDatabaseTest { + + static { + DerbyUtils.fixDerbyLogs(); + } + + @Test public void shouldLoadDefaultValues() { DefaultDatabase db = new DefaultDatabase(new Settings()); @@ -100,11 +105,7 @@ public class DefaultDatabaseTest { assertThat(db.getDialect().getId(), Is.is("derby")); assertThat(((BasicDataSource) db.getDataSource()).getMaxActive(), Is.is(1)); } finally { - try { - DriverManager.getConnection("jdbc:derby:memory:sonar;drop=true"); - } catch (Exception e) { - // silently ignore - } + DerbyUtils.dropInMemoryDatabase(); } } } diff --git a/sonar-core/src/test/java/org/sonar/persistence/DerbyUtils.java b/sonar-core/src/test/java/org/sonar/persistence/DerbyUtils.java new file mode 100644 index 00000000000..cd3e8c7e103 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/DerbyUtils.java @@ -0,0 +1,49 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence; + +import java.io.OutputStream; +import java.sql.DriverManager; + +public final class DerbyUtils { + + private DerbyUtils() { + } + + public static final OutputStream DEV_NULL = new OutputStream() { + public void write(int b) { + } + }; + + /** + * The embedded derby still creates the file derby.log in the execution directory. This method moves it to target/tmp-test. + */ + public static void fixDerbyLogs() { + System.setProperty("derby.stream.error.field", "org.sonar.persistence.DerbyUtils.DEV_NULL"); + } + + public static void dropInMemoryDatabase() { + try { + DriverManager.getConnection("jdbc:derby:memory:sonar;drop=true"); + } catch (Exception e) { + // silently ignore + } + } +} diff --git a/sonar-core/src/main/java/org/sonar/persistence/InMemoryDatabase.java b/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java index 80cf8bb4914..80cf8bb4914 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/InMemoryDatabase.java +++ b/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java diff --git a/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabaseTest.java b/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabaseTest.java index 65f47737927..5db690b5a97 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabaseTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabaseTest.java @@ -33,6 +33,10 @@ import static org.junit.Assert.assertThat; public class InMemoryDatabaseTest { + static { + DerbyUtils.fixDerbyLogs(); + } + @Test public void shouldExecuteDdlAtStartup() throws SQLException { int tables = 0; diff --git a/sonar-core/src/test/java/org/sonar/persistence/MyBatisTest.java b/sonar-core/src/test/java/org/sonar/persistence/MyBatisTest.java index 18bb9fd5c98..f59f232f72b 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/MyBatisTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/MyBatisTest.java @@ -34,6 +34,11 @@ import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; public class MyBatisTest { + + static { + DerbyUtils.fixDerbyLogs(); + } + private static MyBatis myBatis; private static InMemoryDatabase database; diff --git a/sonar-core/src/test/java/org/sonar/persistence/dao/DaoUtilsTest.java b/sonar-core/src/test/java/org/sonar/persistence/dao/DaoUtilsTest.java new file mode 100644 index 00000000000..efc5579d4f9 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dao/DaoUtilsTest.java @@ -0,0 +1,33 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.persistence.dao; + +import org.junit.Test; + +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertThat; + +public class DaoUtilsTest { + + @Test + public void testGetDaoClasses() { + assertThat(DaoUtils.getDaoClasses().size(), greaterThan(1)); + } +} diff --git a/sonar-core/src/test/java/org/sonar/persistence/model/RuleMapperTest.java b/sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java index 6c82754c961..024deedb625 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/model/RuleMapperTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/dao/RuleDaoTest.java @@ -17,7 +17,7 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.persistence.model; +package org.sonar.persistence.dao; import org.apache.commons.io.IOUtils; import org.apache.ibatis.session.SqlSession; @@ -31,28 +31,32 @@ import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.operation.DatabaseOperation; import org.hamcrest.core.Is; import org.junit.*; +import org.junit.Rule; +import org.sonar.persistence.DerbyUtils; import org.sonar.persistence.InMemoryDatabase; import org.sonar.persistence.MyBatis; +import org.sonar.persistence.model.*; import java.io.InputStream; import java.util.List; import static org.junit.Assert.assertThat; -public class RuleMapperTest { +public class RuleDaoTest { protected static IDatabaseTester databaseTester; - private static MyBatis myBatis; private static InMemoryDatabase database; + private static RuleDao dao; @BeforeClass public static void startDatabase() throws Exception { database = new InMemoryDatabase(); - myBatis = new MyBatis(database); + MyBatis myBatis = new MyBatis(database); database.start(); myBatis.start(); + dao = new RuleDao(myBatis); databaseTester = new DataSourceDatabaseTester(database.getDataSource()); } @@ -67,13 +71,10 @@ public class RuleMapperTest { @Test public void testSelectAll() throws Exception { setupData("selectAll"); - SqlSession sqlSession = myBatis.openSession(); - RuleMapper ruleMapper = sqlSession.getMapper(RuleMapper.class); - List<Rule> rules = ruleMapper.selectAll(); - sqlSession.close(); + List<org.sonar.persistence.model.Rule> rules = dao.selectAll(); assertThat(rules.size(), Is.is(1)); - Rule rule = rules.get(0); + org.sonar.persistence.model.Rule rule = rules.get(0); assertThat(rule.getId(), Is.is(1L)); assertThat(rule.getName(), Is.is("Avoid Null")); assertThat(rule.getDescription(), Is.is("Should avoid NULL")); @@ -84,10 +85,7 @@ public class RuleMapperTest { @Test public void testSelectById() throws Exception { setupData("selectById"); - SqlSession sqlSession = myBatis.openSession(); - RuleMapper ruleMapper = sqlSession.getMapper(RuleMapper.class); - Rule rule = ruleMapper.selectById(2L); - sqlSession.close(); + org.sonar.persistence.model.Rule rule = dao.selectById(2L); assertThat(rule.getId(), Is.is(2L)); assertThat(rule.getName(), Is.is("Avoid Null")); diff --git a/sonar-core/src/test/resources/org/sonar/persistence/model/RuleMapperTest/selectAll.xml b/sonar-core/src/test/resources/org/sonar/persistence/dao/RuleDaoTest/selectAll.xml index cb8db141d81..cb8db141d81 100644 --- a/sonar-core/src/test/resources/org/sonar/persistence/model/RuleMapperTest/selectAll.xml +++ b/sonar-core/src/test/resources/org/sonar/persistence/dao/RuleDaoTest/selectAll.xml diff --git a/sonar-core/src/test/resources/org/sonar/persistence/model/RuleMapperTest/selectById.xml b/sonar-core/src/test/resources/org/sonar/persistence/dao/RuleDaoTest/selectById.xml index 217d08e8c80..217d08e8c80 100644 --- a/sonar-core/src/test/resources/org/sonar/persistence/model/RuleMapperTest/selectById.xml +++ b/sonar-core/src/test/resources/org/sonar/persistence/dao/RuleDaoTest/selectById.xml |