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 com.google.common.base.Preconditions.checkState; 017import static org.junit.jupiter.api.Assertions.assertEquals; 018 019import de.softwareforge.testing.postgres.junit5.EmbeddedPgExtension; 020import de.softwareforge.testing.postgres.junit5.SingleDatabaseBuilder; 021 022import java.sql.SQLException; 023import java.util.Map; 024import javax.sql.DataSource; 025 026import edu.umd.cs.findbugs.annotations.NonNull; 027import org.junit.jupiter.api.Test; 028import org.junit.jupiter.api.extension.RegisterExtension; 029import org.postgresql.ds.common.BaseDataSource; 030 031public class ConnectConfigTest { 032 033 private final CapturingDatabasePreparer preparer = new CapturingDatabasePreparer(); 034 035 @RegisterExtension 036 public EmbeddedPgExtension db = SingleDatabaseBuilder.preparedInstanceWithDefaults(preparer) 037 .withInstancePreparer(builder -> builder.addConnectionProperty("connectTimeout", "20")) 038 .build(); 039 040 @Test 041 public void test() throws SQLException { 042 DatabaseInfo databaseInfo = db.createDatabaseInfo(); 043 044 Map<String, String> properties = databaseInfo.connectionProperties(); 045 assertEquals(1, properties.size()); 046 assertEquals("20", properties.get("connectTimeout")); 047 048 BaseDataSource testDatabase = (BaseDataSource) db.createDataSource(); 049 assertEquals("20", testDatabase.getProperty("connectTimeout")); 050 051 BaseDataSource preparerDataSource = (BaseDataSource) preparer.getDataSource(); 052 assertEquals("20", preparerDataSource.getProperty("connectTimeout")); 053 } 054 055 private static class CapturingDatabasePreparer implements EmbeddedPostgresPreparer<DataSource> { 056 057 private DataSource dataSource; 058 059 @Override 060 public void prepare(@NonNull DataSource dataSource) { 061 checkState(this.dataSource == null, "database preparer has been called multiple times"); 062 this.dataSource = dataSource; 063 } 064 065 public DataSource getDataSource() { 066 return dataSource; 067 } 068 } 069}