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 org.junit.jupiter.api.Assertions.assertEquals;
017import static org.junit.jupiter.api.Assertions.assertNotEquals;
018
019import de.softwareforge.testing.postgres.embedded.DatabaseInfo;
020import de.softwareforge.testing.postgres.embedded.FlywayPreparer;
021
022import java.sql.Connection;
023import java.sql.ResultSet;
024import java.sql.SQLException;
025import java.sql.Statement;
026
027import org.junit.jupiter.api.Test;
028import org.junit.jupiter.api.extension.RegisterExtension;
029
030@RequirePostgresVersion(atLeast = "10")
031public class JUnit5FlywayTest {
032
033    @RegisterExtension
034    public static EmbeddedPgExtension singleDatabase = SingleDatabaseBuilder.preparedInstanceWithDefaults(FlywayPreparer.forClasspathLocation("db/testing"))
035            .build();
036
037    @RegisterExtension
038    public static EmbeddedPgExtension multiDatabase = MultiDatabaseBuilder.preparedInstanceWithDefaults(FlywayPreparer.forClasspathLocation("db/testing"))
039            .build();
040
041
042    @Test
043    public void testSingleTables() throws Exception {
044
045        DatabaseInfo firstDatabaseInfo = singleDatabase.createDatabaseInfo();
046        DatabaseInfo secondDatabaseInfo = singleDatabase.createDatabaseInfo();
047
048        // get the same database on every call
049        assertEquals(firstDatabaseInfo, secondDatabaseInfo);
050
051        // make sure tables exist
052        assertEquals("bar", fetchData(firstDatabaseInfo));
053    }
054
055    @Test
056    public void testMultiTables() throws Exception {
057
058        DatabaseInfo firstDatabaseInfo = multiDatabase.createDatabaseInfo();
059        DatabaseInfo secondDatabaseInfo = multiDatabase.createDatabaseInfo();
060
061        // different databases
062        assertNotEquals(firstDatabaseInfo, secondDatabaseInfo);
063
064        // both database contain the data
065        assertEquals("bar", fetchData(firstDatabaseInfo));
066        assertEquals("bar", fetchData(secondDatabaseInfo));
067    }
068
069    private static String fetchData(DatabaseInfo databaseInfo) throws SQLException {
070        try (Connection c = databaseInfo.asDataSource().getConnection();
071                Statement s = c.createStatement()) {
072            try (ResultSet rs = s.executeQuery("SELECT * FROM foo")) {
073                if (!rs.next()) {
074                    return null;
075                }
076                return rs.getString(1);
077            }
078        }
079    }
080}