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 static java.lang.String.format;
17  import static org.junit.jupiter.api.Assertions.assertFalse;
18  import static org.junit.jupiter.api.Assertions.assertTrue;
19  
20  import java.sql.Connection;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.sql.Statement;
24  
25  import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
26  import org.junit.jupiter.api.Order;
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.api.TestMethodOrder;
29  import org.junit.jupiter.api.extension.RegisterExtension;
30  
31  @TestMethodOrder(OrderAnnotation.class)
32  public class Junit5ClassMemberTest {
33  
34      @RegisterExtension
35      public static EmbeddedPgExtension singleDatabase = SingleDatabaseBuilder.instanceWithDefaults().build();
36  
37      @RegisterExtension
38      public static EmbeddedPgExtension multiDatabase = MultiDatabaseBuilder.instanceWithDefaults().build();
39  
40      @Test
41      @Order(1)
42      public void testTableCreation() throws Exception {
43          // create tables in a single and a multi database
44          // the static extension is the same database across all tests.
45          createTable(singleDatabase, "table1");
46          createTable(multiDatabase, "table2");
47      }
48  
49      @Test
50      @Order(2)
51      public void testTableExists() throws Exception {
52          // single database is shared between tests
53          // multi database is not.
54          assertTrue(existsTable(singleDatabase, "table1"));
55          assertFalse(existsTable(multiDatabase, "table2"));
56      }
57  
58  
59      static int createTable(EmbeddedPgExtension extension, String table) throws SQLException {
60          try (Connection connection = extension.createDataSource().getConnection();
61                  Statement statement = connection.createStatement()) {
62              return statement.executeUpdate(format("CREATE TABLE public.%s (a INTEGER)", table));
63          }
64      }
65  
66      static boolean existsTable(EmbeddedPgExtension extension, String table) throws SQLException {
67          try (Connection connection = extension.createDataSource().getConnection();
68                  Statement statement = connection.createStatement()) {
69              String query = format("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '%s')", table);
70              try (ResultSet resultSet = statement.executeQuery(query)) {
71                  resultSet.next();
72                  return resultSet.getBoolean(1);
73              }
74          }
75      }
76  }