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