Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements with cacheable schema and fluent builder #293

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 0 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,5 @@
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>4.0.0.4121</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
12 changes: 6 additions & 6 deletions src/main/java/org/assertj/db/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import org.assertj.db.exception.AssertJDBException;
import org.assertj.db.type.Changes;
import org.assertj.db.type.ConnectionProvider;
import org.assertj.db.type.Request;
import org.assertj.db.type.Source;
import org.assertj.db.type.Table;

/**
Expand All @@ -33,14 +33,14 @@
* The assertion methods are defined in <a href="assertions/package-summary.html">assertions package</a>.
* </p>
* <p>
* Example with a {@link Source} and a {@link Table} with test on the content on the first row of the {@code movie}
* Example with a {@link ConnectionProvider} and a {@link Table} with test on the content on the first row of the {@code movie}
* table that the {@code title} column contains "Alien" like text and the next column contains 1979 like number :
* </p>
*
* <pre>
* <code class='java'>
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
* Table table = new Table(source, &quot;movie&quot;);
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
* Table table = new Table(connectionProvider, &quot;movie&quot;);
* assertThat(table)
* .row()
* .value("title")
Expand Down Expand Up @@ -71,8 +71,8 @@
*
* <pre>
* <code class='java'>
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
* Table table = new Table(source, &quot;movie&quot;);
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
* Table table = new Table(connectionProvider, &quot;movie&quot;);
* assertThat(table)
* .row()
* .value("title")
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/assertj/db/api/BDDAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import org.assertj.db.exception.AssertJDBException;
import org.assertj.db.type.Changes;
import org.assertj.db.type.ConnectionProvider;
import org.assertj.db.type.Request;
import org.assertj.db.type.Source;
import org.assertj.db.type.Table;

/**
Expand All @@ -33,14 +33,14 @@
* The assertion methods are defined in <a href="assertions/package-summary.html">assertions package</a>.
* </p>
* <p>
* Example with a {@link Source} and a {@link Table} with test on the content on the first row of the {@code movie}
* Example with a {@link ConnectionProvider} and a {@link Table} with test on the content on the first row of the {@code movie}
* table that the {@code title} column contains "Alien" as text and the next column contains 1979 as a number :
* </p>
*
* <pre>
* <code class='java'>
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
* Table table = new Table(source, &quot;movie&quot;);
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
* Table table = new Table(connectionProvider, &quot;movie&quot;);
* then(table)
* .row()
* .value("title")
Expand Down Expand Up @@ -71,8 +71,8 @@
*
* <pre>
* <code class='java'>
* Source source = new Source(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;);
* Table table = new Table(source, &quot;movie&quot;);
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
* Table table = new Table(connectionProvider, &quot;movie&quot;);
* then(table)
* .row()
* .value("title")
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/assertj/db/api/ErrorCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ public class ErrorCollector {
private static final String INTERCEPT_METHOD_NAME = "intercept";

private static final String CLASS_NAME = ErrorCollector.class.getName();

// scope : the current soft-assertion object
private final List<Throwable> errors = new ArrayList<>();
// scope : the last assertion call (might be nested)
private final LastResult lastResult = new LastResult();
/**
* Construct empty error collector.
*/
public ErrorCollector() {
}

// scope : the current soft-assertion object
private final List<Throwable> errors = new ArrayList<>();
// scope : the last assertion call (might be nested)
private final LastResult lastResult = new LastResult();

private static int countErrorCollectorProxyCalls() {
int nbCalls = 0;
for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/assertj/db/api/TableAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TableAssert(Table table) {
*/
@Override
public TableAssert exists() {
return AssertionsOnTableExistence.exists(this, info, actual.getName(), actual.getSource(), actual.getDataSource());
return AssertionsOnTableExistence.exists(this, info, actual.getName(), actual.getConnectionProvider());
}

/**
Expand All @@ -65,6 +65,6 @@ public TableAssert exists() {
*/
@Override
public TableAssert doesNotExist() {
return AssertionsOnTableExistence.doesNotExists(this, info, actual.getName(), actual.getSource(), actual.getDataSource());
return AssertionsOnTableExistence.doesNotExists(this, info, actual.getName(), actual.getConnectionProvider());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;

import org.assertj.core.api.WritableAssertionInfo;
import org.assertj.core.internal.Failures;
import org.assertj.db.api.AbstractDbAssert;
import org.assertj.db.exception.AssertJDBException;
import org.assertj.db.type.Source;
import org.assertj.db.type.ConnectionProvider;

/**
* Implements the assertion method on the existence of a table.
Expand All @@ -47,18 +45,20 @@ private AssertionsOnTableExistence() {
/**
* Verifies that the table exists.
*
* @param <A> The type of the assertion which call this method.
* @param assertion The assertion which call this method.
* @param info Writable information about an assertion.
* @param table The table name to search in DB.
* @param source The source to connect to DB.
* @param dataSource The source to connect to DB.
* @param <A> The type of the assertion which call this method.
* @param assertion The assertion which call this method.
* @param info Writable information about an assertion.
* @param table The table name to search in DB.
* @param connectionProvider The provider to connect to DB.
* @return {@code this} assertion object.
* @throws AssertionError If the table does not exist.
*/
public static <A extends AbstractDbAssert<?, ?, ?, ?, ?, ?>> A exists(A assertion, WritableAssertionInfo info,
String table, Source source, DataSource dataSource) {
try (Connection connection = getConnection(source, dataSource)) {
String table, ConnectionProvider connectionProvider) {
if (connectionProvider == null) {
throw new NullPointerException("connectionProvider must be not null");
}
try (Connection connection = connectionProvider.getConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet result = metaData.getTables(null, null, table, null);
if (!result.next()) {
Expand All @@ -75,18 +75,20 @@ private AssertionsOnTableExistence() {
/**
* Verifies that the database not contains the table.
*
* @param <A> The type of the assertion which call this method.
* @param assertion The assertion which call this method.
* @param info Writable information about an assertion.
* @param table The table name to search in DB.
* @param source The source to connect to DB.
* @param dataSource The source to connect to DB.
* @param <A> The type of the assertion which call this method.
* @param assertion The assertion which call this method.
* @param info Writable information about an assertion.
* @param table The table name to search in DB.
* @param connectionProvider The provider to connect to DB.
* @return {@code this} assertion object.
* @throws AssertionError If the table does not exist.
*/
public static <A extends AbstractDbAssert<?, ?, ?, ?, ?, ?>> A doesNotExists(A assertion, WritableAssertionInfo info,
String table, Source source, DataSource dataSource) {
try (Connection connection = getConnection(source, dataSource)) {
String table, ConnectionProvider connectionProvider) {
if (connectionProvider == null) {
throw new NullPointerException("connectionProvider must be not null");
}
try (Connection connection = connectionProvider.getConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet result = metaData.getTables(null, null, table, null);
if (result.next()) {
Expand All @@ -98,14 +100,4 @@ private AssertionsOnTableExistence() {
}
return assertion;
}

private static Connection getConnection(Source source, DataSource dataSource) throws SQLException {
if (source == null && dataSource == null) {
throw new NullPointerException("connection or dataSource must be not null");
}
if (dataSource != null) {
return dataSource.getConnection();
}
return DriverManager.getConnection(source.getUrl(), source.getUser(), source.getPassword());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,56 @@
*/
package org.assertj.db.type;

import java.lang.reflect.InvocationTargetException;

import org.assertj.db.type.lettercase.LetterCase;
import org.assertj.db.type.lettercase.WithLetterCase;

/**
* A source to indicates the information to connect to the database with letter case.
* Base implementation for ConnectionProvider that handle letter case and schema metadata management.
*
* @author Régis Pouiller
* @since 1.1.0
* @author Julien Roy
* @since 3.0.0
*/
public class SourceWithLetterCase extends Source implements WithLetterCase {
abstract class AbstractConnectionProvider implements ConnectionProvider {

private final SchemaMetadata schemaMetadata;

/**
* Letter case of the tables.
*/
private final LetterCase tableLetterCase;
/**
* Letter case of the columns.
*/
private final LetterCase columnLetterCase;
/**
* Letter case of the primary keys.
*/
private final LetterCase primaryKeyLetterCase;

/**
* Constructor with the information.
*
* @param url URL to the database.
* @param user User to connect.
* @param password Password to connect.
* @param tableLetterCase Letter case of the tables.
* @param columnLetterCase Letter case of the columns.
* @param primaryKeyLetterCase Letter case of the primary keys.
*/
public SourceWithLetterCase(String url, String user, String password,
LetterCase tableLetterCase, LetterCase columnLetterCase, LetterCase primaryKeyLetterCase) {

super(url, user, password);
protected AbstractConnectionProvider(Class<? extends SchemaMetadata> schemaMetadataType, LetterCase tableLetterCase, LetterCase columnLetterCase, LetterCase primaryKeyLetterCase) {
this.schemaMetadata = instantiateSchemaMetadata(schemaMetadataType);
this.tableLetterCase = tableLetterCase;
this.columnLetterCase = columnLetterCase;
this.primaryKeyLetterCase = primaryKeyLetterCase;
}

/**
* {@inheritDoc}
*/
private SchemaMetadata instantiateSchemaMetadata(Class<? extends SchemaMetadata> schemaMetadataType) {
try {
return schemaMetadataType.getConstructor(ConnectionProvider.class).newInstance(this);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalArgumentException("Schema metadata instantiation failure", e);
}
}

@Override
public LetterCase getTableLetterCase() {
return tableLetterCase;
}

@Override
public LetterCase getColumnLetterCase() {
return columnLetterCase;
}

/**
* {@inheritDoc}
*/
@Override
public LetterCase getPrimaryKeyLetterCase() {
return primaryKeyLetterCase;
}

/**
* {@inheritDoc}
*/
@Override
public LetterCase getTableLetterCase() {
return tableLetterCase;
public SchemaMetadata getMetaData() {
return this.schemaMetadata;
}
}
Loading