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 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.assertTrue;
020
021import java.io.File;
022import java.sql.Connection;
023import java.sql.ResultSet;
024import java.sql.Statement;
025
026import org.junit.jupiter.api.Assumptions;
027import org.junit.jupiter.api.Test;
028
029public class LocalDirectoryPostgresTest {
030
031    // pg-embedded.test.local-dir is set in pom.xml
032    private static final File LOCAL_INSTALL_LOCATION = new File(System.getProperty("pg-embedded.test.local-dir", "/usr/local"));
033    private static final File LOCAL_INSTALL_BIN_POSTGRES = new File(LOCAL_INSTALL_LOCATION, "/bin/postgres");
034
035    @Test
036    public void testEmbeddedPg() throws Exception {
037        Assumptions.assumeTrue(LOCAL_INSTALL_BIN_POSTGRES.exists(), format("Skipping test, PostgreSQL binary not found at %s", LOCAL_INSTALL_BIN_POSTGRES));
038
039        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults()
040                // pg-embedded.test.unix-socket-dir is set in pom.xml. Can not use "just defaults" because some Linux distributions try to be smart and
041                // use /var/run/postgresql as default which is only writable by the PostgresQL unix user. So this test would crash with "Permission denied".
042                .addServerConfiguration(System.getProperty("pg-embedded.test.unix-socket-dir", "unix_socket_directories"), System.getProperty("java.io.tmpdir"))
043                .useLocalPostgresInstallation(LOCAL_INSTALL_LOCATION)
044                .build();
045                Connection c = pg.createDefaultDataSource().getConnection();
046                Statement s = c.createStatement()) {
047            try (ResultSet rs = s.executeQuery("SELECT 1")) {
048                assertTrue(rs.next());
049                assertEquals(1, rs.getInt(1));
050                assertFalse(rs.next());
051            }
052        }
053    }
054}