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.DatabasePreparer; 018import de.softwareforge.testing.postgres.embedded.EmbeddedPostgres; 019import de.softwareforge.testing.postgres.embedded.EmbeddedPostgresPreparer; 020 021import javax.sql.DataSource; 022 023import edu.umd.cs.findbugs.annotations.NonNull; 024 025/** 026 * Create a new PostgreSQL server that supports multiple databases. Each database is cloned from a template. 027 */ 028public final class MultiDatabaseBuilder { 029 030 private MultiDatabaseBuilder() { 031 throw new AssertionError("MultiDatabaseBuilder 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.multiDatabase(); 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.multiDatabase().withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 052 } 053 054 /** 055 * @deprecated Use {@link #preparedInstance(EmbeddedPostgresPreparer)}. 056 */ 057 @Deprecated 058 @NonNull 059 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull DatabasePreparer preparer) { 060 return EmbeddedPgExtension.multiDatabase().withPreparer(preparer); 061 } 062 063 /** 064 * @deprecated Use {@link #preparedInstanceWithDefaults(EmbeddedPostgresPreparer)}. 065 */ 066 @Deprecated 067 @NonNull 068 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull DatabasePreparer preparer) { 069 return EmbeddedPgExtension.multiDatabase().withPreparer(preparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 070 } 071 072 /** 073 * Create a builder and register a {@link EmbeddedPostgresPreparer<DataSource>} to set up the template database. 074 * 075 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 076 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 077 */ 078 @NonNull 079 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 080 return EmbeddedPgExtension.multiDatabase().withDatabasePreparer(databasePreparer); 081 } 082 083 /** 084 * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied and register a {@link 085 * EmbeddedPostgresPreparer<DataSource>} to set up the template database. 086 * 087 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 088 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 089 */ 090 @NonNull 091 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 092 return EmbeddedPgExtension.multiDatabase().withDatabasePreparer(databasePreparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 093 } 094}