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.assertFalse;
018import static org.junit.jupiter.api.Assertions.assertTrue;
019
020import java.sql.Connection;
021import java.sql.ResultSet;
022import java.sql.SQLException;
023import java.sql.Statement;
024
025import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
026import org.junit.jupiter.api.Order;
027import org.junit.jupiter.api.Test;
028import org.junit.jupiter.api.TestMethodOrder;
029import org.junit.jupiter.api.extension.RegisterExtension;
030
031@TestMethodOrder(OrderAnnotation.class)
032public class Junit5ClassMemberTest {
033
034    @RegisterExtension
035    public static EmbeddedPgExtension singleDatabase = SingleDatabaseBuilder.instanceWithDefaults().build();
036
037    @RegisterExtension
038    public static EmbeddedPgExtension multiDatabase = MultiDatabaseBuilder.instanceWithDefaults().build();
039
040    @Test
041    @Order(1)
042    public void testTableCreation() throws Exception {
043        // create tables in a single and a multi database
044        // the static extension is the same database across all tests.
045        createTable(singleDatabase, "table1");
046        createTable(multiDatabase, "table2");
047    }
048
049    @Test
050    @Order(2)
051    public void testTableExists() throws Exception {
052        // single database is shared between tests
053        // multi database is not.
054        assertTrue(existsTable(singleDatabase, "table1"));
055        assertFalse(existsTable(multiDatabase, "table2"));
056    }
057
058
059    static int createTable(EmbeddedPgExtension extension, String table) throws SQLException {
060        try (Connection connection = extension.createDataSource().getConnection();
061                Statement statement = connection.createStatement()) {
062            return statement.executeUpdate(format("CREATE TABLE public.%s (a INTEGER)", table));
063        }
064    }
065
066    static boolean existsTable(EmbeddedPgExtension extension, String table) throws SQLException {
067        try (Connection connection = extension.createDataSource().getConnection();
068                Statement statement = connection.createStatement()) {
069            String query = format("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '%s')", table);
070            try (ResultSet resultSet = statement.executeQuery(query)) {
071                resultSet.next();
072                return resultSet.getBoolean(1);
073            }
074        }
075    }
076}