This is an automated email from the ASF dual-hosted git repository. amanin pushed a commit to branch fix/fast-envelope in repository https://gitbox.apache.org/repos/asf/sis.git
commit 602c728c93c4d26823d6373dd33afcb85a0b5faf Author: Alexis Manin <[email protected]> AuthorDate: Tue Jan 21 14:32:41 2020 +0100 chore(Core): Add tests for H2 database support --- core/sis-metadata/pom.xml | 6 +++ .../java/org/apache/sis/test/sql/TestDatabase.java | 43 +++++++++++++---- .../referencing/factory/sql/EPSGInstallerTest.java | 54 +++++++++++++++------- 3 files changed, 79 insertions(+), 24 deletions(-) diff --git a/core/sis-metadata/pom.xml b/core/sis-metadata/pom.xml index 02226e6..9e20bbf 100644 --- a/core/sis-metadata/pom.xml +++ b/core/sis-metadata/pom.xml @@ -174,6 +174,12 @@ <artifactId>postgresql</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>1.4.200</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/core/sis-metadata/src/test/java/org/apache/sis/test/sql/TestDatabase.java b/core/sis-metadata/src/test/java/org/apache/sis/test/sql/TestDatabase.java index 625817f..bfb605b 100644 --- a/core/sis-metadata/src/test/java/org/apache/sis/test/sql/TestDatabase.java +++ b/core/sis-metadata/src/test/java/org/apache/sis/test/sql/TestDatabase.java @@ -17,22 +17,27 @@ package org.apache.sis.test.sql; import java.io.IOException; -import javax.sql.DataSource; import java.sql.Connection; -import java.sql.Statement; import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.SQLDataException; -import org.postgresql.PGProperty; -import org.postgresql.ds.PGSimpleDataSource; -import org.hsqldb.jdbc.JDBCDataSource; -import org.hsqldb.jdbc.JDBCPool; -import org.apache.derby.jdbc.EmbeddedDataSource; +import java.sql.SQLException; +import java.sql.Statement; +import javax.sql.DataSource; + import org.apache.sis.internal.metadata.sql.Initializer; import org.apache.sis.internal.metadata.sql.ScriptRunner; import org.apache.sis.test.TestCase; import org.apache.sis.util.Debug; +import org.apache.derby.jdbc.EmbeddedDataSource; + +import org.h2.jdbcx.JdbcConnectionPool; +import org.h2.jdbcx.JdbcDataSource; +import org.hsqldb.jdbc.JDBCDataSource; +import org.hsqldb.jdbc.JDBCPool; +import org.postgresql.PGProperty; +import org.postgresql.ds.PGSimpleDataSource; + import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; @@ -174,6 +179,28 @@ public strictfp class TestDatabase implements AutoCloseable { }; } + public static TestDatabase createOnH2(final String name, final boolean pooled) throws SQLException { + final String url = "jdbc:h2:mem:".concat(name).concat(";DB_CLOSE_DELAY=-1"); + final DataSource ds; + if (pooled) { + // A null pointer exception occurs on pooled connection is not set + ds = JdbcConnectionPool.create(url, "sis", "sis"); + } else { + final JdbcDataSource tmpDs = new JdbcDataSource(); + tmpDs.setURL(url); + ds = tmpDs; + } + + return new TestDatabase(ds) { + @Override + public void close() throws SQLException { + try (Connection c = ds.getConnection(); Statement s = c.createStatement()) { + s.execute("SHUTDOWN"); + } + } + }; + } + /** * Creates a connection to an existing PostgreSQL database. * This method returns only if all the following conditions are true: diff --git a/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java b/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java index 8ffed08..565cf78 100644 --- a/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java +++ b/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/sql/EPSGInstallerTest.java @@ -16,39 +16,45 @@ */ package org.apache.sis.referencing.factory.sql; -import java.util.Map; -import java.util.Set; -import java.util.HashMap; -import java.util.HashSet; -import java.util.regex.Pattern; import java.io.IOException; -import javax.sql.DataSource; import java.sql.Connection; -import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; -import org.opengis.util.FactoryException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; +import javax.sql.DataSource; + import org.opengis.referencing.crs.GeographicCRS; import org.opengis.referencing.crs.ProjectedCRS; -import org.apache.sis.referencing.CommonCRS; +import org.opengis.util.FactoryException; + +import org.apache.sis.internal.metadata.sql.Reflection; import org.apache.sis.internal.system.Loggers; import org.apache.sis.internal.util.Constants; -import org.apache.sis.internal.metadata.sql.Reflection; +import org.apache.sis.referencing.CommonCRS; +import org.apache.sis.test.DependsOn; +import org.apache.sis.test.LoggingWatcher; +import org.apache.sis.test.TestCase; +import org.apache.sis.test.sql.TestDatabase; import org.apache.sis.util.ComparisonMode; import org.apache.sis.util.Utilities; -// Test dependencies -import org.apache.sis.test.sql.TestDatabase; -import org.apache.sis.test.LoggingWatcher; -import org.apache.sis.test.DependsOn; -import org.apache.sis.test.TestCase; import org.junit.After; import org.junit.Rule; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; +// Test dependencies + /** * Tests {@link EPSGInstaller} indirectly, through {@link EPSGFactory#install(Connection)}. @@ -161,6 +167,22 @@ public final strictfp class EPSGInstallerTest extends TestCase { loggings.assertNextLogContains("EPSG", "jdbc:hsqldb:mem:EPSGInstaller"); loggings.assertNoUnexpectedLog(); } + /** + * Tests the creation of an EPSG database on H2. + * This test is skipped if the SQL scripts are not found. + * + * @throws Exception if an error occurred while creating the database. + */ + @Test + public void testCreationOnH2() throws Exception { + final InstallationScriptProvider scripts = getScripts(); // Needs to be invoked first. + try (TestDatabase db = TestDatabase.createOnH2("EPSGInstaller", false)) { + createAndTest(db.source, scripts); + verifyParameterValues(db.source); + } + loggings.assertNextLogContains("EPSG", "jdbc:h2:mem:EPSGInstaller"); + loggings.assertNoUnexpectedLog(); + } /** * Tests the creation of an EPSG database on PostgreSQL. This test requires a PostgreSQL server
