View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package de.softwareforge.testing.postgres.junit5;
15  
16  import de.softwareforge.testing.postgres.embedded.DatabaseManager;
17  import de.softwareforge.testing.postgres.embedded.DatabasePreparer;
18  import de.softwareforge.testing.postgres.embedded.EmbeddedPostgres;
19  import de.softwareforge.testing.postgres.embedded.EmbeddedPostgresPreparer;
20  
21  import javax.sql.DataSource;
22  
23  import edu.umd.cs.findbugs.annotations.NonNull;
24  
25  /**
26   * Create a new PostgreSQL server that supports multiple databases. Each database is cloned from a template.
27   */
28  public final class MultiDatabaseBuilder {
29  
30      private MultiDatabaseBuilder() {
31          throw new AssertionError("MultiDatabaseBuilder can not be instantiated");
32      }
33  
34      /**
35       * Create a builder without any customizations applied.
36       *
37       * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further.
38       */
39      @NonNull
40      public static DatabaseManager.Builder<EmbeddedPgExtension> instance() {
41          return EmbeddedPgExtension.multiDatabase();
42      }
43  
44      /**
45       * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied.
46       *
47       * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further.
48       */
49      @NonNull
50      public static DatabaseManager.Builder<EmbeddedPgExtension> instanceWithDefaults() {
51          return EmbeddedPgExtension.multiDatabase().withInstancePreparer(EmbeddedPostgres.Builder::withDefaults);
52      }
53  
54      /**
55       * @deprecated Use {@link #preparedInstance(EmbeddedPostgresPreparer)}.
56       */
57      @Deprecated
58      @NonNull
59      public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull DatabasePreparer preparer) {
60          return EmbeddedPgExtension.multiDatabase().withPreparer(preparer);
61      }
62  
63      /**
64       * @deprecated Use {@link #preparedInstanceWithDefaults(EmbeddedPostgresPreparer)}.
65       */
66      @Deprecated
67      @NonNull
68      public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull DatabasePreparer preparer) {
69          return EmbeddedPgExtension.multiDatabase().withPreparer(preparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults);
70      }
71  
72      /**
73       * Create a builder and register a {@link EmbeddedPostgresPreparer<DataSource>} to set up the template database.
74       *
75       * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null.
76       * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further.
77       */
78      @NonNull
79      public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstance(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) {
80          return EmbeddedPgExtension.multiDatabase().withDatabasePreparer(databasePreparer);
81      }
82  
83      /**
84       * Create a builder with standard initializations ({@link EmbeddedPostgres.Builder#withDefaults()}) applied and register a {@link
85       * EmbeddedPostgresPreparer<DataSource>} to set up the template database.
86       *
87       * @param databasePreparer A {@link EmbeddedPostgresPreparer<DataSource>} instance. Must not be null.
88       * @return A {@link DatabaseManager.Builder<EmbeddedPgExtension>} instance that can be customized further.
89       */
90      @NonNull
91      public static DatabaseManager.Builder<EmbeddedPgExtension> preparedInstanceWithDefaults(@NonNull EmbeddedPostgresPreparer<DataSource> databasePreparer) {
92          return EmbeddedPgExtension.multiDatabase().withDatabasePreparer(databasePreparer).withInstancePreparer(EmbeddedPostgres.Builder::withDefaults);
93      }
94  }