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/** 027 * Create a new PostgreSQL server that supports a single database. 028 */ 029public final class SingleDatabaseBuilder { 030 031 private SingleDatabaseBuilder() { 032 throw new AssertionError("SingleDatabaseBuilder can not be instantiated"); 033 } 034 035 /** 036 * Create a builder without any customizations applied. 037 * 038 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 039 */ 040 @NonNull 041 public static DatabaseManager.Builder<EmbeddedPgExtension> instance() { 042 return EmbeddedPgExtension.singleDatabase(); 043 } 044 045 /** 046 * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied. 047 * 048 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 049 */ 050 @NonNull 051 public static DatabaseManager.Builder<EmbeddedPgExtension> instanceWithDefaults() { 052 return EmbeddedPgExtension.singleDatabase().withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 053 } 054 055 /** 056 * @deprecated Use {@link SingleDatabaseBuilder#preparedInstance(EmbeddedPostgresPreparer)}. 057 */ 058 @Deprecated 059 @NonNull 060 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull DatabasePreparer preparer) { 061 return EmbeddedPgExtension.singleDatabase().withPreparer(preparer); 062 } 063 064 /** 065 * @deprecated Use {@link SingleDatabaseBuilder#preparedInstanceWithDefaults(EmbeddedPostgresPreparer)}. 066 */ 067 @Deprecated 068 @NonNull 069 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull DatabasePreparer preparer) { 070 return EmbeddedPgExtension.singleDatabase().withPreparer(preparer).withCustomizer(EmbeddedPostgres.Builder::withDefaults); 071 } 072 073 /** 074 * Create a builder and register a {@link EmbeddedPostgresPreparer<DataSource>} to set up the database. 075 * 076 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 077 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 078 * @since 3.0 079 */ 080 @NonNull 081 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 082 return EmbeddedPgExtension.singleDatabase().withDatabasePreparer(databasePreparer); 083 } 084 085 /** 086 * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied and register a {@link 087 * EmbeddedPostgresPreparer<DataSource>} to set up the database. 088 * 089 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 090 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 091 * @since 3.0 092 */ 093 @NonNull 094 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 095 return EmbeddedPgExtension.singleDatabase().withDatabasePreparer(databasePreparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 096 } 097}