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