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