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 #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 #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 */ 079 @NonNull 080 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 081 return EmbeddedPgExtension.singleDatabase().withDatabasePreparer(databasePreparer); 082 } 083 084 /** 085 * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied and register a {@link 086 * EmbeddedPostgresPreparer<DataSource>} to set up the database. 087 * 088 * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null. 089 * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further. 090 */ 091 @NonNull 092 public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) { 093 return EmbeddedPgExtension.singleDatabase().withDatabasePreparer(databasePreparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults); 094 } 095}