diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-17 13:56:35 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-17 13:56:35 +0100 |
commit | 194cac3982b108e718b204f2a3645ea7b1794205 (patch) | |
tree | 792e0a52cd1ab8eb66ed8afb91cb9112e581b864 | |
parent | 1ba8493639f34fb68876004d85ece836e6ab33c4 (diff) | |
download | sonarqube-194cac3982b108e718b204f2a3645ea7b1794205.tar.gz sonarqube-194cac3982b108e718b204f2a3645ea7b1794205.zip |
Fix test on MySQL
4 files changed, 22 insertions, 17 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/db/ComponentLinkDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/db/ComponentLinkDaoTest.java index d1586ef2db4..d8ba219baa8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/db/ComponentLinkDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/db/ComponentLinkDaoTest.java @@ -31,7 +31,6 @@ import org.sonar.core.persistence.DbTester; import org.sonar.test.DbTests; import java.util.List; -import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; @@ -86,13 +85,7 @@ public class ComponentLinkDaoTest { ); session.commit(); - // For an unknown reason, on MySQL the id of the links is set to 2, so we can't use assertDbUnit() to check inserted values - Map<String, Object> result = dbTester.selectFirst("select id as \"id\", component_uuid as \"componentUuid\", link_type as \"type\", name as \"name\", href as \"href\" from project_links"); - assertThat(result.get("id")).isNotNull(); - assertThat(result.get("componentUuid")).isEqualTo("ABCD"); - assertThat(result.get("type")).isEqualTo("homepage"); - assertThat(result.get("name")).isEqualTo("Home"); - assertThat(result.get("href")).isEqualTo("http://www.sonarqube.org"); + dbTester.assertDbUnit(getClass(), "insert-result.xml", new String[]{"id"}, "project_links"); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/event/db/EventDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/event/db/EventDaoTest.java index 9dba34a33fc..30464f7273a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/event/db/EventDaoTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/event/db/EventDaoTest.java @@ -101,7 +101,7 @@ public class EventDaoTest { ); session.commit(); - dbTester.assertDbUnit(getClass(), "insert-result.xml", "events"); + dbTester.assertDbUnit(getClass(), "insert-result.xml", new String[]{"id"}, "events"); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/component/db/ComponentLinkDaoTest/insert-result.xml b/server/sonar-server/src/test/resources/org/sonar/server/component/db/ComponentLinkDaoTest/insert-result.xml new file mode 100644 index 00000000000..8b89e7223ec --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/component/db/ComponentLinkDaoTest/insert-result.xml @@ -0,0 +1,5 @@ +<dataset> + + <project_links id="1" component_uuid="ABCD" link_type="homepage" name="Home" href="http://www.sonarqube.org"/> + +</dataset> diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java b/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java index 48470d92f36..275e7c624a2 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/DbTester.java @@ -37,7 +37,9 @@ import org.dbunit.database.DatabaseConfig; import org.dbunit.database.IDatabaseConnection; import org.dbunit.dataset.CompositeDataSet; import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.ITable; import org.dbunit.dataset.ReplacementDataSet; +import org.dbunit.dataset.filter.DefaultColumnFilter; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.ext.mssql.InsertIdentityOperation; import org.dbunit.operation.DatabaseOperation; @@ -56,12 +58,7 @@ import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; -import java.sql.Clob; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; +import java.sql.*; import java.util.List; import java.util.Map; import java.util.Properties; @@ -282,17 +279,27 @@ public class DbTester extends ExternalResource { } public void assertDbUnit(Class testClass, String filename, String... tables) { + assertDbUnit(testClass, filename, new String[0], tables); + } + + public void assertDbUnit(Class testClass, String filename, String[] excludedColumnNames, String... tables) { IDatabaseConnection connection = null; try { connection = dbUnitConnection(); IDataSet dataSet = connection.createDataSet(); String path = "/" + testClass.getName().replace('.', '/') + "/" + filename; - IDataSet expectedDataSet = dbUnitDataSet(testClass.getResourceAsStream(path)); + InputStream inputStream = testClass.getResourceAsStream(path); + if (inputStream == null) { + throw new IllegalStateException(String.format("File '%s' does not exist", path)); + } + IDataSet expectedDataSet = dbUnitDataSet(inputStream); for (String table : tables) { DiffCollectingFailureHandler diffHandler = new DiffCollectingFailureHandler(); - Assertion.assertEquals(expectedDataSet.getTable(table), dataSet.getTable(table), diffHandler); + ITable filteredTable = DefaultColumnFilter.excludedColumnsTable(dataSet.getTable(table), excludedColumnNames); + ITable filteredExpectedTable = DefaultColumnFilter.excludedColumnsTable(expectedDataSet.getTable(table), excludedColumnNames); + Assertion.assertEquals(filteredExpectedTable, filteredTable, diffHandler); // Evaluate the differences and ignore some column values List diffList = diffHandler.getDiffList(); for (Object o : diffList) { |