Search in sources :

Example 6 with TestDatabase

use of org.apache.sis.test.sql.TestDatabase in project sis by apache.

the class EPSGInstallerTest method testCreationOnPostgreSQL.

/**
 * Tests the creation of an EPSG database on PostgreSQL. This test requires a PostgreSQL server
 * running on {@code "localhost"} with an empty database named {@code "SpatialMetadataTest"}.
 * See {@linkplain TestDatabase#createOnPostgreSQL here} for more information.
 *
 * @throws Exception if an error occurred while creating the database.
 *
 * @since 0.8
 */
@Test
public void testCreationOnPostgreSQL() throws Exception {
    // Needs to be invoked first.
    final InstallationScriptProvider scripts = getScripts();
    try (TestDatabase db = TestDatabase.createOnPostgreSQL("EPSG", false)) {
        createAndTest(db.source, scripts);
        verifyParameterValues(db.source);
    }
    loggings.assertNextLogContains("EPSG", "jdbc:postgresql://localhost/SpatialMetadataTest");
    loggings.assertNoUnexpectedLog();
}
Also used : TestDatabase(org.apache.sis.test.sql.TestDatabase) Test(org.junit.Test)

Example 7 with TestDatabase

use of org.apache.sis.test.sql.TestDatabase in project sis by apache.

the class DataScriptFormatter method main.

/**
 * Compacts the {@code Data.sql} file provided by EPSG. This method expects two arguments.
 * The first argument is the file of the SQL script to read, which must exist.
 * The second argument is the file where to write the compacted SQL script,
 * which will be overwritten without warning if it exists.
 * The values of those arguments are typically:
 *
 * <ol>
 *   <li>{@code $EPSG_SCRIPTS/PostgreSQL_Table_Script.sql}</li>
 *   <li>{@code sis-epsg/src/main/resources/org/apache/sis/referencing/factory/sql/epsg/Data.sql}</li>
 * </ol>
 *
 * @param  arguments  the source files and the destination file.
 * @throws Exception if an error occurred while reading of writing the file.
 */
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void main(String[] arguments) throws Exception {
    if (arguments.length != 2) {
        System.err.println("Expected two arguments: source SQL file and target SQL file.");
        return;
    }
    try (TestDatabase db = TestDatabase.create("dummy");
        Connection c = db.source.getConnection()) {
        final DataScriptFormatter f = new DataScriptFormatter(c);
        f.run(Paths.get(arguments[0]), Paths.get(arguments[1]));
    }
}
Also used : TestDatabase(org.apache.sis.test.sql.TestDatabase) Connection(java.sql.Connection)

Example 8 with TestDatabase

use of org.apache.sis.test.sql.TestDatabase in project sis by apache.

the class PostgresTest method testSpatialFeatures.

/**
 * Tests reading and writing features and rasters.
 *
 * @throws Exception if an error occurred while testing the database.
 */
@Test
public void testSpatialFeatures() throws Exception {
    try (TestDatabase database = TestDatabase.createOnPostgreSQL(SQLStoreTest.SCHEMA, true)) {
        database.executeSQL(PostgresTest.class, "file:SpatialFeatures.sql");
        final StorageConnector connector = new StorageConnector(database.source);
        connector.setOption(OptionKey.GEOMETRY_LIBRARY, GeometryLibrary.JTS);
        final ResourceDefinition table = ResourceDefinition.table(null, SQLStoreTest.SCHEMA, "SpatialData");
        try (SQLStore store = new SQLStore(new SQLStoreProvider(), connector, table)) {
            /*
                 * Invoke the private `model()` method. We have to use reflection because the class
                 * is not in the same package and we do not want to expose the method in public API.
                 */
            final Method modelAccessor = SQLStore.class.getDeclaredMethod("model");
            modelAccessor.setAccessible(true);
            final Postgres<?> pg = (Postgres<?>) modelAccessor.invoke(store);
            try (Connection connection = database.source.getConnection();
                ExtendedInfo info = new ExtendedInfo(pg, connection)) {
                testInfoStatements(info);
                testGeometryGetter(info, connection);
                testRasterReader(TestRaster.USHORT, info, connection);
            }
            /*
                 * Tests through public API.
                 */
            final FeatureSet resource = store.findResource("SpatialData");
            try (Stream<AbstractFeature> features = resource.features(false)) {
                features.forEach(PostgresTest::validate);
            }
            final Envelope envelope = resource.getEnvelope().get();
            assertEquals(envelope.getMinimum(0), -72, 1);
            assertEquals(envelope.getMaximum(1), 43, 1);
        }
    }
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) SQLStore(org.apache.sis.storage.sql.SQLStore) SQLStoreProvider(org.apache.sis.storage.sql.SQLStoreProvider) TestDatabase(org.apache.sis.test.sql.TestDatabase) ResourceDefinition(org.apache.sis.storage.sql.ResourceDefinition) Connection(java.sql.Connection) AbstractFeature(org.apache.sis.feature.AbstractFeature) Method(java.lang.reflect.Method) Envelope(org.opengis.geometry.Envelope) FeatureSet(org.apache.sis.storage.FeatureSet) Test(org.junit.Test) GeometryGetterTest(org.apache.sis.internal.sql.feature.GeometryGetterTest) SQLStoreTest(org.apache.sis.storage.sql.SQLStoreTest)

Example 9 with TestDatabase

use of org.apache.sis.test.sql.TestDatabase in project sis by apache.

the class ScriptRunnerTest method testOnDerby.

/**
 * Tests {@link ScriptRunner} with an in-memory Derby database.
 * This method delegates its work to all other methods in this class that expect a {@link ScriptRunner} argument.
 *
 * @throws SQLException if an error occurred while executing the script runner.
 */
@Test
public void testOnDerby() throws SQLException {
    try (TestDatabase db = TestDatabase.create("ScriptRunner");
        Connection c = db.source.getConnection()) {
        final ScriptRunner sr = new ScriptRunner(c, 3);
        testSupportedFlags(sr);
        testRegularExpressions(sr);
    }
}
Also used : TestDatabase(org.apache.sis.test.sql.TestDatabase) Connection(java.sql.Connection) Test(org.junit.Test)

Example 10 with TestDatabase

use of org.apache.sis.test.sql.TestDatabase in project sis by apache.

the class EPSGInstallerTest method testCreationOnH2.

/**
 * 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 {
    // Needs to be invoked first.
    final InstallationScriptProvider scripts = getScripts();
    try (TestDatabase db = TestDatabase.createOnH2("EPSGInstaller")) {
        createAndTest(db.source, scripts);
        verifyParameterValues(db.source);
    }
    loggings.assertNextLogContains("EPSG", "jdbc:h2:mem:EPSGInstaller");
    loggings.assertNoUnexpectedLog();
}
Also used : TestDatabase(org.apache.sis.test.sql.TestDatabase) Test(org.junit.Test)

Aggregations

TestDatabase (org.apache.sis.test.sql.TestDatabase)13 Test (org.junit.Test)11 Connection (java.sql.Connection)3 StorageConnector (org.apache.sis.storage.StorageConnector)3 SQLStoreProvider (org.apache.sis.storage.sql.SQLStoreProvider)2 Method (java.lang.reflect.Method)1 AbstractFeature (org.apache.sis.feature.AbstractFeature)1 SQLBuilder (org.apache.sis.internal.metadata.sql.SQLBuilder)1 GeometryGetterTest (org.apache.sis.internal.sql.feature.GeometryGetterTest)1 SchemaModifier (org.apache.sis.internal.sql.feature.SchemaModifier)1 TableReference (org.apache.sis.internal.sql.feature.TableReference)1 DataStore (org.apache.sis.storage.DataStore)1 FeatureSet (org.apache.sis.storage.FeatureSet)1 ResourceDefinition (org.apache.sis.storage.sql.ResourceDefinition)1 SQLStore (org.apache.sis.storage.sql.SQLStore)1 SQLStoreTest (org.apache.sis.storage.sql.SQLStoreTest)1 Envelope (org.opengis.geometry.Envelope)1