MARMOTTA-584: added a new check if the postgis extension is available before testing
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/d2333889 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/d2333889 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/d2333889 Branch: refs/heads/MARMOTTA-584 Commit: d233388945cae8b9bcf9f6b7306867b03f61acc3 Parents: bf53221 Author: Sergio Fernández <[email protected]> Authored: Fri Sep 11 12:41:52 2015 +0200 Committer: Sergio Fernández <[email protected]> Committed: Fri Sep 11 12:41:52 2015 +0200 ---------------------------------------------------------------------- .../geosparql/GeoSPARQLFunctionsTest.java | 91 ++++++++++++++------ .../apache/marmotta/kiwi/sail/KiWiStore.java | 1 - .../kiwi/test/junit/KiWiDatabaseRunner.java | 2 - 3 files changed, 67 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/d2333889/libraries/kiwi/kiwi-geosparql/src/test/java/org/apache/marmotta/kiwi/sparql/geosparql/GeoSPARQLFunctionsTest.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-geosparql/src/test/java/org/apache/marmotta/kiwi/sparql/geosparql/GeoSPARQLFunctionsTest.java b/libraries/kiwi/kiwi-geosparql/src/test/java/org/apache/marmotta/kiwi/sparql/geosparql/GeoSPARQLFunctionsTest.java index c5c24e5..ac6eb55 100644 --- a/libraries/kiwi/kiwi-geosparql/src/test/java/org/apache/marmotta/kiwi/sparql/geosparql/GeoSPARQLFunctionsTest.java +++ b/libraries/kiwi/kiwi-geosparql/src/test/java/org/apache/marmotta/kiwi/sparql/geosparql/GeoSPARQLFunctionsTest.java @@ -19,14 +19,18 @@ package org.apache.marmotta.kiwi.sparql.geosparql; import info.aduna.iteration.Iterations; import org.apache.commons.io.IOUtils; import org.apache.marmotta.kiwi.config.KiWiConfiguration; +import org.apache.marmotta.kiwi.persistence.KiWiPersistence; +import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; import org.apache.marmotta.kiwi.sail.KiWiStore; import org.apache.marmotta.kiwi.sparql.sail.KiWiSparqlSail; +import org.apache.marmotta.kiwi.test.helper.DBConnectionChecker; +import org.apache.marmotta.kiwi.test.junit.KiWiDatabaseRunner; import org.junit.*; -import org.junit.internal.AssumptionViolatedException; -import org.junit.rules.TestWatcher; -import org.junit.runner.Description; import org.junit.runner.RunWith; -import org.openrdf.query.*; +import org.openrdf.query.BindingSet; +import org.openrdf.query.QueryLanguage; +import org.openrdf.query.TupleQuery; +import org.openrdf.query.TupleQueryResult; import org.openrdf.repository.Repository; import org.openrdf.repository.RepositoryConnection; import org.openrdf.repository.RepositoryException; @@ -35,12 +39,13 @@ import org.openrdf.rio.RDFFormat; import org.openrdf.rio.RDFParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; -import org.apache.marmotta.kiwi.persistence.pgsql.PostgreSQLDialect; -import org.apache.marmotta.kiwi.test.helper.DBConnectionChecker; -import org.apache.marmotta.kiwi.test.junit.KiWiDatabaseRunner; /** * Test suite for all the functions of GeoSPARQL implemented. @@ -74,10 +79,10 @@ public class GeoSPARQLFunctionsTest { @Before public void initDatabase() throws RepositoryException, IOException, RDFParseException { - if (!PostgreSQLDialect.class.equals(this.dbConfig.getDialect().getClass())) { - log.warn("PostgreSQL not available! (using {})", this.dbConfig.getDialect().getClass().getSimpleName()); - Assume.assumeTrue(false); - } + Assume.assumeTrue(String.format("PostgreSQL not available! (using %s)", this.dbConfig.getDialect().getClass().getSimpleName()), + PostgreSQLDialect.class.equals(this.dbConfig.getDialect().getClass())); + + Assume.assumeTrue("PostGIS not available!", checkPostGIS(dbConfig)); store = new KiWiStore(dbConfig); store.setDropTablesOnShutdown(true); @@ -91,15 +96,61 @@ public class GeoSPARQLFunctionsTest { RepositoryConnection conn = repository.getConnection(); try { conn.begin(); - conn.add(this.getClass().getResourceAsStream("/demo_data_spain_provinces.rdf"), "http://localhost/test/", RDFFormat.RDFXML); - conn.add(this.getClass().getResourceAsStream("/demo_data_spain_towns.rdf"), "http://localhost/test/", RDFFormat.RDFXML); - conn.add(this.getClass().getResourceAsStream("/demo_data_spain_rivers.rdf"), "http://localhost/test/", RDFFormat.RDFXML); + conn.add(this.getClass().getResourceAsStream("/demo_data_spain_provinces.rdf"), "http://localhost/test/geosparql", RDFFormat.RDFXML); + conn.add(this.getClass().getResourceAsStream("/demo_data_spain_towns.rdf"), "http://localhost/test/geosparql", RDFFormat.RDFXML); + conn.add(this.getClass().getResourceAsStream("/demo_data_spain_rivers.rdf"), "http://localhost/test/geosparql", RDFFormat.RDFXML); conn.commit(); } finally { conn.close(); } } + /** + * Performs a basic test to see if PostGIS extension is + * actually available to use + * + * @return + */ + private boolean checkPostGIS(KiWiConfiguration config) { + final KiWiPersistence persistence = new KiWiPersistence(config); + try { + persistence.initialise(); + + final Connection conn = persistence.getJDBCConnection(); + if (conn != null) { + try { + //try(PreparedStatement stmt = conn.prepareStatement("CREATE EXTENSION IF NOT EXISTS postgis;")) { + // stmt.executeQuery(); + //} + + try (PreparedStatement stmt = conn.prepareStatement("SELECT PostGIS_full_version();")) { + ResultSet result = stmt.executeQuery(); + if (result.next()) { + log.info("Using PostGIS {}...", result.getString("postgis_full_version")); + } + } + + //try(PreparedStatement stmt = conn.prepareStatement("DROP EXTENSION IF NOT EXISTS postgis;")) { + // stmt.executeQuery(); + //} + + return true; + } catch (SQLException e) { + log.warn("Checking PostGIS extension has failed: {}", e.getMessage()); + return false; + } + } else { + log.warn("Impossible to get a JDBC connection"); + return false; + } + } catch (Exception e) { + log.warn("Impossible to get a JDBC connection: {}", e.getMessage()); + return false; + } finally { + persistence.shutdown(); + } + } + @After public void dropDatabase() throws RepositoryException, SQLException { if (store != null && store.isInitialized()) { @@ -109,15 +160,6 @@ public class GeoSPARQLFunctionsTest { } } - @Rule - public TestWatcher watchman = new TestWatcher() { - @Override - protected void skipped(AssumptionViolatedException e, Description description) { - super.skipped(e, description); - log.warn("{} skipped because a precondition failed", description.getMethodName()); - } - }; - @Test public void testSfContains() throws Exception { testQueryBoolean("sfContains.sparql", "contains"); @@ -319,7 +361,7 @@ public class GeoSPARQLFunctionsTest { } private void testQueryGeometry(String filename) throws Exception { - String queryString = IOUtils.toString(this.getClass().getResourceAsStream(filename), "UTF-8"); + String queryString = IOUtils.toString(this.getClass().getResourceAsStream("/" + filename), "UTF-8"); RepositoryConnection conn = repository.getConnection(); try { @@ -342,4 +384,5 @@ public class GeoSPARQLFunctionsTest { conn.close(); } } + } http://git-wip-us.apache.org/repos/asf/marmotta/blob/d2333889/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiStore.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiStore.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiStore.java index ae2bf6c..2ac179e 100644 --- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiStore.java +++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiStore.java @@ -38,7 +38,6 @@ import java.sql.SQLException; */ public class KiWiStore extends NotifyingSailBase { - /** * The repository-wide value factory, using the valueFactoryConnection above. Will be initialised when * getValueFactory() is called for the first time. http://git-wip-us.apache.org/repos/asf/marmotta/blob/d2333889/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/junit/KiWiDatabaseRunner.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/junit/KiWiDatabaseRunner.java b/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/junit/KiWiDatabaseRunner.java index 1f01f3d..6308852 100644 --- a/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/junit/KiWiDatabaseRunner.java +++ b/libraries/kiwi/kiwi-triplestore/src/test/java/org/apache/marmotta/kiwi/test/junit/KiWiDatabaseRunner.java @@ -241,8 +241,6 @@ public class KiWiDatabaseRunner extends Suite { return method.getName(); } - - @Override protected void validateConstructor(List<Throwable> errors) { validateOnlyOneConstructor(errors);
