001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package de.softwareforge.testing.postgres.junit5;
015
016import static java.lang.String.format;
017import static org.junit.jupiter.api.Assertions.assertEquals;
018import static org.junit.jupiter.api.Assertions.assertFalse;
019import static org.junit.jupiter.api.Assertions.assertNotNull;
020import static org.junit.jupiter.api.Assertions.assertTrue;
021
022import de.softwareforge.testing.postgres.embedded.DatabaseInfo;
023import de.softwareforge.testing.postgres.embedded.EmbeddedPostgres;
024
025import java.sql.Connection;
026import java.sql.ResultSet;
027import java.sql.SQLException;
028import java.sql.Statement;
029import javax.sql.DataSource;
030
031import org.junit.jupiter.api.Test;
032import org.junit.jupiter.api.extension.ExtendWith;
033
034@ExtendWith(EmbeddedPgExtension.class)
035public class ExtendWithTest {
036
037    @Test
038    public void testEmbeddedPostgres(EmbeddedPostgres pg) throws SQLException {
039        assertNotNull(pg);
040        testDataSource(pg.createDefaultDataSource());
041    }
042
043    @Test
044    public void testDatabaseInfo(DatabaseInfo databaseInfo) throws SQLException {
045        assertNotNull(databaseInfo);
046
047        testDataSource(databaseInfo.asDataSource());
048    }
049
050    @Test
051    public void testDataSource(DataSource dataSource) throws SQLException {
052        assertFalse(existsTable(dataSource, "table1"));
053        assertFalse(existsTable(dataSource, "table2"));
054
055        assertEquals(0, createTable(dataSource, "table1"));
056        assertEquals(0, createTable(dataSource, "table2"));
057
058        assertTrue(existsTable(dataSource, "table1"));
059        assertTrue(existsTable(dataSource, "table2"));
060    }
061
062    static int createTable(DataSource ds, String table) throws SQLException {
063        try (Connection connection = ds.getConnection();
064                Statement statement = connection.createStatement()) {
065            return statement.executeUpdate(format("CREATE TABLE public.%s (a INTEGER)", table));
066        }
067    }
068
069    static boolean existsTable(DataSource ds, String table) throws SQLException {
070        try (Connection connection = ds.getConnection();
071                Statement statement = connection.createStatement()) {
072            String query = format("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '%s')", table);
073            try (ResultSet resultSet = statement.executeQuery(query)) {
074                resultSet.next();
075                return resultSet.getBoolean(1);
076            }
077        }
078    }
079}