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