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 org.junit.jupiter.api.Test;
035import org.junit.jupiter.api.io.TempDir;
036import org.postgresql.ds.PGSimpleDataSource;
037
038public class EmbeddedPostgresTest {
039
040    @TempDir
041    public Path tempDir;
042
043    @Test
044    public void testEmbeddedPg() throws Exception {
045        try (EmbeddedPostgres pg = EmbeddedPostgres.defaultInstance();
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
056    @Test
057    public void testEmbeddedPgCreationWithNestedDataDirectory() throws Exception {
058        Path dataPath = Files.createDirectories(tempDir.resolve("data-dir-parent").resolve("data-dir"));
059        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults()
060                .setDataDirectory(dataPath)
061                .build()) {
062            assertEquals(dataPath, pg.getDataDirectory().toPath());
063        }
064    }
065
066    @Test
067    public void testDatasources() throws Exception {
068        try (EmbeddedPostgres pg = EmbeddedPostgres.builderWithDefaults().addConnectionProperty(CONNECT_TIMEOUT.getName(), "20").build()) {
069            DataSource ds1 = pg.createDefaultDataSource();
070
071            DataSource ds2 = pg.createDefaultDatabaseInfo().asDataSource();
072
073            assertSame(ds1.getClass(), ds2.getClass());
074
075            PGSimpleDataSource pds1 = (PGSimpleDataSource) ds1;
076            PGSimpleDataSource pds2 = (PGSimpleDataSource) ds2;
077
078            assertArrayEquals(pds1.getServerNames(), pds2.getServerNames());
079            assertArrayEquals(pds1.getPortNumbers(), pds2.getPortNumbers());
080            assertEquals(pds1.getUser(), pds2.getUser());
081            assertEquals(pds1.getConnectTimeout(), pds2.getConnectTimeout());
082            assertEquals(20, pds1.getConnectTimeout());
083        }
084
085    }
086
087    @Test
088    public void testInitDbOptions() throws IOException {
089
090        String locale = "";
091        String lcMessages = "";
092        EmbeddedPostgres.Builder builder = EmbeddedPostgres.builderWithDefaults();
093        if (EmbeddedUtil.IS_OS_WINDOWS) {
094            locale = "de-de";
095            lcMessages = "de-de";
096        } else if (EmbeddedUtil.IS_OS_MAC) {
097            locale = "de_DE";
098            lcMessages = "de_DE";
099        } else if (EmbeddedUtil.IS_OS_LINUX) {
100            locale = "de_DE.utf8";
101            lcMessages = "de_DE.utf8";
102        } else {
103            fail("System not detected!");
104        }
105        builder.addInitDbConfiguration("locale", locale)
106                .addInitDbConfiguration("lc-messages", lcMessages)
107                .addInitDbConfiguration("no-sync", "");
108
109        try (EmbeddedPostgres pg = builder.build()) {
110            Map<String, String> localeConfig = pg.getLocaleConfiguration();
111            assertEquals(locale, localeConfig.get("locale"));
112            assertEquals(lcMessages, localeConfig.get("lc-messages"));
113            assertEquals("", localeConfig.get("no-sync"));
114
115            List<String> options = pg.createInitDbOptions();
116            assertTrue(options.contains("--locale=" + locale));
117            assertTrue(options.contains("--lc-messages=" + lcMessages));
118            assertTrue(options.contains("--no-sync"));
119        }
120    }
121}