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.junit5; 016 017import de.softwareforge.testing.postgres.embedded.DatabaseManager; 018import de.softwareforge.testing.postgres.embedded.EmbeddedPostgres; 019import de.softwareforge.testing.postgres.embedded.EmbeddedPostgresPreparer; 020 021import jakarta.annotation.Nonnull; 022import javax.sql.DataSource; 023 024 025/** 026 * Create a new PostgreSQL server that supports a single database. 027 */ 028public final class SingleDatabaseBuilder { 029 030 private SingleDatabaseBuilder() { 031 throw new AssertionError("SingleDatabaseBuilder can not be instantiated"); 032 } 033 034 /** 035 * Create a builder without any customizations applied. 036 * 037 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 038 */ 039 @Nonnull 040 public static DatabaseManager.Builder<EmbeddedPgExtension> instance() { 041 return EmbeddedPgExtension.singleDatabase(); 042 } 043 044 /** 045 * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied. 046 * 047 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 048 */ 049 @Nonnull 050 public static DatabaseManager.Builder<EmbeddedPgExtension> instanceWithDefaults() { 051 return EmbeddedPgExtension.singleDatabase().withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 052 } 053 054 /** 055 * Create a builder and register a {@link EmbeddedPostgresPreparer<DataSource>} to set up the database. 056 * 057 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 058 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 059 * @since 3.0 060 */ 061 @Nonnull 062 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@Nonnull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 063 return EmbeddedPgExtension.singleDatabase().withDatabasePreparer(databasePreparer); 064 } 065 066 /** 067 * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied and register a 068 * {@link EmbeddedPostgresPreparer<DataSource>} to set up the database. 069 * 070 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 071 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 072 * @since 3.0 073 */ 074 @Nonnull 075 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@Nonnull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 076 return EmbeddedPgExtension.singleDatabase().withDatabasePreparer(databasePreparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 077 } 078}