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.assertArrayEquals;
017import static org.junit.jupiter.api.Assertions.assertEquals;
018import static org.junit.jupiter.api.Assertions.assertFalse;
019import static org.junit.jupiter.api.Assertions.assertSame;
020import static org.junit.jupiter.api.Assertions.assertTrue;
021import static org.junit.jupiter.api.Assertions.fail;
022import static org.postgresql.PGProperty.CONNECT_TIMEOUT;
023
024import java.io.IOException;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.sql.Connection;
028import java.sql.ResultSet;
029import java.sql.Statement;
030import java.util.List;
031import java.util.Map;
032import javax.sql.DataSource;
033
034import de.softwareforge.testing.postgres.junit5.RequirePostgresVersion;
035import org.junit.jupiter.api.Test;
036import org.junit.jupiter.api.io.TempDir;
037import org.postgresql.ds.PGSimpleDataSource;
038
039public class EmbeddedPostgresTest {
040
041    @TempDir
042    public Path tempDir;
043
044    @Test
045    public void testEmbeddedPg() throws Exception {
046        try (EmbeddedPostgres pg = EmbeddedPostgres.defaultInstance();
047                Connection c = pg.createDefaultDataSource().getConnection();
048                Statement s = c.createStatement()) {
049            try (ResultSet rs = s.executeQuery("SELECT 1")) {
050                assertTrue(rs.next());
051                assertEquals(1, rs.getInt(1));
052                assertFalse(rs.next());
053            }
054        }
055    }
056
057    @Test
058    public void testEmbeddedPgCreationWithNestedDataDirectory() throws Exception {
059        Path dataPath = Files.createDirectories(tempDir.resolve("data-dir-parent").resolve("data-dir"));
060        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults()
061                .setDataDirectory(dataPath)
062                .build()) {
063            assertEquals(dataPath, pg.getDataDirectory().toPath());
064        }
065    }
066
067    @Test
068    public void testDatasources() throws Exception {
069        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults().addConnectionProperty(CONNECT_TIMEOUT.getName(), "20").build()) {
070            DataSource ds1 = pg.createDefaultDataSource();
071
072            DataSource ds2 = pg.createDefaultDatabaseInfo().asDataSource();
073
074            assertSame(ds1.getClass(), ds2.getClass());
075
076            PGSimpleDataSource pds1 = (PGSimpleDataSource) ds1;
077            PGSimpleDataSource pds2 = (PGSimpleDataSource) ds2;
078
079            assertArrayEquals(pds1.getServerNames(), pds2.getServerNames());
080            assertArrayEquals(pds1.getPortNumbers(), pds2.getPortNumbers());
081            assertEquals(pds1.getUser(), pds2.getUser());
082            assertEquals(pds1.getConnectTimeout(), pds2.getConnectTimeout());
083            assertEquals(20, pds1.getConnectTimeout());
084        }
085
086    }
087
088    @Test
089    @RequirePostgresVersion(atLeast = "10")
090    public void testInitDbOptions() throws IOException {
091
092        String locale = "";
093        String lcMessages = "";
094        EmbeddedPostgres.Builder builder = EmbeddedPostgres.builderWithDefaults();
095        if (EmbeddedUtil.IS_OS_WINDOWS) {
096            locale = "de-de";
097            lcMessages = "de-de";
098        } else if (EmbeddedUtil.IS_OS_MAC) {
099            locale = "de_DE";
100            lcMessages = "de_DE";
101        } else if (EmbeddedUtil.IS_OS_LINUX) {
102            locale = "de_DE.utf8";
103            lcMessages = "de_DE.utf8";
104        } else {
105            fail("System not detected!");
106        }
107        builder.addInitDbConfiguration("locale", locale)
108                .addInitDbConfiguration("lc-messages", lcMessages)
109                .addInitDbConfiguration("no-sync", "");
110
111        try (EmbeddedPostgres pg = builder.build()) {
112            Map<String, String> localeConfig = pg.getLocaleConfiguration();
113            assertEquals(locale, localeConfig.get("locale"));
114            assertEquals(lcMessages, localeConfig.get("lc-messages"));
115            assertEquals("", localeConfig.get("no-sync"));
116
117            List<String> options = pg.createInitDbOptions();
118            assertTrue(options.contains("--locale=" + locale));
119            assertTrue(options.contains("--lc-messages=" + lcMessages));
120            assertTrue(options.contains("--no-sync"));
121        }
122    }
123}