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.embedded;
015
016import static org.junit.jupiter.api.Assertions.assertEquals;
017import static org.junit.jupiter.api.Assertions.assertNotEquals;
018import static org.junit.jupiter.api.Assertions.assertThrows;
019import static org.junit.jupiter.api.Assertions.assertTrue;
020
021import java.io.IOException;
022import java.sql.Connection;
023import java.sql.ResultSet;
024import java.sql.SQLException;
025import java.sql.Statement;
026
027import org.flywaydb.core.api.FlywayException;
028import org.junit.jupiter.api.Test;
029
030public class FlywayPreparerTest {
031
032    @Test
033    public void testWorking() throws Exception {
034        try (DatabaseManager manager = DatabaseManager.multiDatabases()
035                .withInstancePreparer(EmbeddedPostgres.Builder::withDefaults)
036                .withDatabasePreparer(FlywayPreparer.forClasspathLocation("db/testing"))
037                .build()
038                .start()) {
039
040            DatabaseInfo firstDatabaseInfo = manager.getDatabaseInfo();
041            DatabaseInfo secondDatabaseInfo = manager.getDatabaseInfo();
042
043            // different databases
044            assertNotEquals(firstDatabaseInfo, secondDatabaseInfo);
045
046            // both database contain the data
047            assertEquals("bar", fetchData(firstDatabaseInfo));
048            assertEquals("bar", fetchData(secondDatabaseInfo));
049        }
050    }
051
052    @Test
053    public void testMissing() throws Exception {
054        try (DatabaseManager manager = DatabaseManager.singleDatabase()
055                .withInstancePreparer(EmbeddedPostgres.Builder::withDefaults)
056                .withDatabasePreparer(FlywayPreparer
057                        .forClasspathLocation("db/non-existing")
058                        .addCustomizer(c -> c.failOnMissingLocations(true)))
059                .build()
060                ) {
061
062            IOException e = assertThrows(IOException.class, manager::start);
063
064            assertTrue(e.getCause() instanceof FlywayException);
065            assertTrue(e.getMessage().contains("Unable to resolve location classpath:db/non-existing"));
066        }
067    }
068
069
070    @Test
071    public void testBroken() throws Exception {
072        try (DatabaseManager manager = DatabaseManager.singleDatabase()
073                .withInstancePreparer(EmbeddedPostgres.Builder::withDefaults)
074                .withDatabasePreparer(FlywayPreparer
075                        .forClasspathLocation("db/broken")
076                        .addCustomizer(c -> c.failOnMissingLocations(true)))
077                .build()
078        ) {
079
080            IOException e = assertThrows(IOException.class, manager::start);
081
082            assertTrue(e.getCause() instanceof FlywayException);
083            // 42P01 - relation does not exist
084            assertTrue(e.getMessage().contains("42P01"));
085        }
086    }
087
088
089    private static String fetchData(DatabaseInfo databaseInfo) throws SQLException {
090        try (Connection c = databaseInfo.asDataSource().getConnection();
091                Statement s = c.createStatement()) {
092            try (ResultSet rs = s.executeQuery("SELECT * FROM foo")) {
093                if (!rs.next()) {
094                    return null;
095                }
096                return rs.getString(1);
097            }
098        }
099    }
100}