Skip to content

Commit e6d1dc4

Browse files
committed
Cacheable database schema metadata
1 parent 28b7ce2 commit e6d1dc4

File tree

319 files changed

+3249
-3402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

319 files changed

+3249
-3402
lines changed

src/main/java/org/assertj/db/api/Assertions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import org.assertj.db.exception.AssertJDBException;
2424
import org.assertj.db.type.Changes;
25+
import org.assertj.db.type.ConnectionProvider;
2526
import org.assertj.db.type.Request;
26-
import org.assertj.db.type.Source;
2727
import org.assertj.db.type.Table;
2828

2929
/**
@@ -33,14 +33,14 @@
3333
* The assertion methods are defined in <a href="assertions/package-summary.html">assertions package</a>.
3434
* </p>
3535
* <p>
36-
* Example with a {@link Source} and a {@link Table} with test on the content on the first row of the {@code movie}
36+
* Example with a {@link ConnectionProvider} and a {@link Table} with test on the content on the first row of the {@code movie}
3737
* table that the {@code title} column contains "Alien" like text and the next column contains 1979 like number :
3838
* </p>
3939
*
4040
* <pre>
4141
* <code class='java'>
42-
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
43-
* Table table = new Table(source, &quot;movie&quot;);
42+
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
43+
* Table table = new Table(connectionProvider, &quot;movie&quot;);
4444
* assertThat(table)
4545
* .row()
4646
* .value("title")
@@ -71,8 +71,8 @@
7171
*
7272
* <pre>
7373
* <code class='java'>
74-
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
75-
* Table table = new Table(source, &quot;movie&quot;);
74+
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
75+
* Table table = new Table(connectionProvider, &quot;movie&quot;);
7676
* assertThat(table)
7777
* .row()
7878
* .value("title")

src/main/java/org/assertj/db/api/BDDAssertions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import org.assertj.db.exception.AssertJDBException;
2121
import org.assertj.db.type.Changes;
22+
import org.assertj.db.type.ConnectionProvider;
2223
import org.assertj.db.type.Request;
23-
import org.assertj.db.type.Source;
2424
import org.assertj.db.type.Table;
2525

2626
/**
@@ -33,14 +33,14 @@
3333
* The assertion methods are defined in <a href="assertions/package-summary.html">assertions package</a>.
3434
* </p>
3535
* <p>
36-
* Example with a {@link Source} and a {@link Table} with test on the content on the first row of the {@code movie}
36+
* Example with a {@link ConnectionProvider} and a {@link Table} with test on the content on the first row of the {@code movie}
3737
* table that the {@code title} column contains "Alien" as text and the next column contains 1979 as a number :
3838
* </p>
3939
*
4040
* <pre>
4141
* <code class='java'>
42-
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
43-
* Table table = new Table(source, &quot;movie&quot;);
42+
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
43+
* Table table = new Table(connectionProvider, &quot;movie&quot;);
4444
* then(table)
4545
* .row()
4646
* .value("title")
@@ -71,8 +71,8 @@
7171
*
7272
* <pre>
7373
* <code class='java'>
74-
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
75-
* Table table = new Table(source, &quot;movie&quot;);
74+
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
75+
* Table table = new Table(connectionProvider, &quot;movie&quot;);
7676
* then(table)
7777
* .row()
7878
* .value("title")

src/main/java/org/assertj/db/api/TableAssert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TableAssert(Table table) {
4848
*/
4949
@Override
5050
public TableAssert exists() {
51-
return AssertionsOnTableExistence.exists(this, info, actual.getName(), actual.getSource(), actual.getDataSource());
51+
return AssertionsOnTableExistence.exists(this, info, actual.getName(), actual.getConnectionProvider());
5252
}
5353

5454
/**
@@ -65,6 +65,6 @@ public TableAssert exists() {
6565
*/
6666
@Override
6767
public TableAssert doesNotExist() {
68-
return AssertionsOnTableExistence.doesNotExists(this, info, actual.getName(), actual.getSource(), actual.getDataSource());
68+
return AssertionsOnTableExistence.doesNotExists(this, info, actual.getName(), actual.getConnectionProvider());
6969
}
7070
}

src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnTableExistence.java

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717

1818
import java.sql.Connection;
1919
import java.sql.DatabaseMetaData;
20-
import java.sql.DriverManager;
2120
import java.sql.ResultSet;
2221
import java.sql.SQLException;
23-
import javax.sql.DataSource;
2422

2523
import org.assertj.core.api.WritableAssertionInfo;
2624
import org.assertj.core.internal.Failures;
2725
import org.assertj.db.api.AbstractDbAssert;
2826
import org.assertj.db.exception.AssertJDBException;
29-
import org.assertj.db.type.Source;
27+
import org.assertj.db.type.ConnectionProvider;
3028

3129
/**
3230
* Implements the assertion method on the existence of a table.
@@ -47,18 +45,20 @@ private AssertionsOnTableExistence() {
4745
/**
4846
* Verifies that the table exists.
4947
*
50-
* @param <A> The type of the assertion which call this method.
51-
* @param assertion The assertion which call this method.
52-
* @param info Writable information about an assertion.
53-
* @param table The table name to search in DB.
54-
* @param source The source to connect to DB.
55-
* @param dataSource The source to connect to DB.
48+
* @param <A> The type of the assertion which call this method.
49+
* @param assertion The assertion which call this method.
50+
* @param info Writable information about an assertion.
51+
* @param table The table name to search in DB.
52+
* @param connectionProvider The provider to connect to DB.
5653
* @return {@code this} assertion object.
5754
* @throws AssertionError If the table does not exist.
5855
*/
5956
public static <A extends AbstractDbAssert<?, ?, ?, ?, ?, ?>> A exists(A assertion, WritableAssertionInfo info,
60-
String table, Source source, DataSource dataSource) {
61-
try (Connection connection = getConnection(source, dataSource)) {
57+
String table, ConnectionProvider connectionProvider) {
58+
if (connectionProvider == null) {
59+
throw new NullPointerException("connectionProvider must be not null");
60+
}
61+
try (Connection connection = connectionProvider.getConnection()) {
6262
DatabaseMetaData metaData = connection.getMetaData();
6363
ResultSet result = metaData.getTables(null, null, table, null);
6464
if (!result.next()) {
@@ -75,18 +75,20 @@ private AssertionsOnTableExistence() {
7575
/**
7676
* Verifies that the database not contains the table.
7777
*
78-
* @param <A> The type of the assertion which call this method.
79-
* @param assertion The assertion which call this method.
80-
* @param info Writable information about an assertion.
81-
* @param table The table name to search in DB.
82-
* @param source The source to connect to DB.
83-
* @param dataSource The source to connect to DB.
78+
* @param <A> The type of the assertion which call this method.
79+
* @param assertion The assertion which call this method.
80+
* @param info Writable information about an assertion.
81+
* @param table The table name to search in DB.
82+
* @param connectionProvider The provider to connect to DB.
8483
* @return {@code this} assertion object.
8584
* @throws AssertionError If the table does not exist.
8685
*/
8786
public static <A extends AbstractDbAssert<?, ?, ?, ?, ?, ?>> A doesNotExists(A assertion, WritableAssertionInfo info,
88-
String table, Source source, DataSource dataSource) {
89-
try (Connection connection = getConnection(source, dataSource)) {
87+
String table, ConnectionProvider connectionProvider) {
88+
if (connectionProvider == null) {
89+
throw new NullPointerException("connectionProvider must be not null");
90+
}
91+
try (Connection connection = connectionProvider.getConnection()) {
9092
DatabaseMetaData metaData = connection.getMetaData();
9193
ResultSet result = metaData.getTables(null, null, table, null);
9294
if (result.next()) {
@@ -98,14 +100,4 @@ private AssertionsOnTableExistence() {
98100
}
99101
return assertion;
100102
}
101-
102-
private static Connection getConnection(Source source, DataSource dataSource) throws SQLException {
103-
if (source == null && dataSource == null) {
104-
throw new NullPointerException("connection or dataSource must be not null");
105-
}
106-
if (dataSource != null) {
107-
return dataSource.getConnection();
108-
}
109-
return DriverManager.getConnection(source.getUrl(), source.getUser(), source.getPassword());
110-
}
111103
}

src/main/java/org/assertj/db/type/SourceWithLetterCase.java renamed to src/main/java/org/assertj/db/type/AbstractConnectionProvider.java

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,56 @@
1212
*/
1313
package org.assertj.db.type;
1414

15+
import java.lang.reflect.InvocationTargetException;
16+
1517
import org.assertj.db.type.lettercase.LetterCase;
16-
import org.assertj.db.type.lettercase.WithLetterCase;
1718

1819
/**
19-
* A source to indicates the information to connect to the database with letter case.
20+
* Base implementation for ConnectionProvider that handle letter case and schema metadata management.
2021
*
21-
* @author Régis Pouiller
22-
* @since 1.1.0
22+
* @author Julien Roy
23+
* @since 3.0.0
2324
*/
24-
public class SourceWithLetterCase extends Source implements WithLetterCase {
25+
abstract class AbstractConnectionProvider implements ConnectionProvider {
26+
27+
private final SchemaMetadata schemaMetadata;
2528

26-
/**
27-
* Letter case of the tables.
28-
*/
2929
private final LetterCase tableLetterCase;
30-
/**
31-
* Letter case of the columns.
32-
*/
3330
private final LetterCase columnLetterCase;
34-
/**
35-
* Letter case of the primary keys.
36-
*/
3731
private final LetterCase primaryKeyLetterCase;
3832

39-
/**
40-
* Constructor with the information.
41-
*
42-
* @param url URL to the database.
43-
* @param user User to connect.
44-
* @param password Password to connect.
45-
* @param tableLetterCase Letter case of the tables.
46-
* @param columnLetterCase Letter case of the columns.
47-
* @param primaryKeyLetterCase Letter case of the primary keys.
48-
*/
49-
public SourceWithLetterCase(String url, String user, String password,
50-
LetterCase tableLetterCase, LetterCase columnLetterCase, LetterCase primaryKeyLetterCase) {
51-
52-
super(url, user, password);
33+
protected AbstractConnectionProvider(Class<? extends SchemaMetadata> schemaMetadataType, LetterCase tableLetterCase, LetterCase columnLetterCase, LetterCase primaryKeyLetterCase) {
34+
this.schemaMetadata = instantiateSchemaMetadata(schemaMetadataType);
5335
this.tableLetterCase = tableLetterCase;
5436
this.columnLetterCase = columnLetterCase;
5537
this.primaryKeyLetterCase = primaryKeyLetterCase;
5638
}
5739

58-
/**
59-
* {@inheritDoc}
60-
*/
40+
private SchemaMetadata instantiateSchemaMetadata(Class<? extends SchemaMetadata> schemaMetadataType) {
41+
try {
42+
return schemaMetadataType.getConstructor(ConnectionProvider.class).newInstance(this);
43+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
44+
throw new IllegalArgumentException("Schema metadata instantiation failure", e);
45+
}
46+
}
47+
48+
@Override
49+
public LetterCase getTableLetterCase() {
50+
return tableLetterCase;
51+
}
52+
6153
@Override
6254
public LetterCase getColumnLetterCase() {
6355
return columnLetterCase;
6456
}
6557

66-
/**
67-
* {@inheritDoc}
68-
*/
6958
@Override
7059
public LetterCase getPrimaryKeyLetterCase() {
7160
return primaryKeyLetterCase;
7261
}
7362

74-
/**
75-
* {@inheritDoc}
76-
*/
7763
@Override
78-
public LetterCase getTableLetterCase() {
79-
return tableLetterCase;
64+
public SchemaMetadata getMetaData() {
65+
return this.schemaMetadata;
8066
}
8167
}

0 commit comments

Comments
 (0)