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/**
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 {@link
068     * 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}