Skip to content

Commit

Permalink
Provide fluent API to build DbElement
Browse files Browse the repository at this point in the history
  • Loading branch information
VanRoy committed Nov 18, 2024
1 parent 3ec0466 commit c06f01f
Show file tree
Hide file tree
Showing 469 changed files with 3,500 additions and 4,695 deletions.
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>
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
24 changes: 12 additions & 12 deletions src/main/java/org/assertj/db/type/AbstractDbData.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,6 @@ public abstract class AbstractDbData<D extends AbstractDbData<D>> extends Abstra
*/
private List<Column> columnsList;

/**
* Default constructor.
*
* @param dataType The type of the data on which is the change.
* @param selfType Class of this element : a subclass of {@code AbstractDbData}.
*/
AbstractDbData(Class<D> selfType, DataType dataType) {
super(selfType);
this.dataType = dataType;
}

/**
* Constructor with a {@link JdbcUrlConnectionProvider}.
*
Expand All @@ -82,11 +71,22 @@ public abstract class AbstractDbData<D extends AbstractDbData<D>> extends Abstra
* @param connectionProvider The {@link JdbcUrlConnectionProvider} to connect to the database (must be not {@code null}).
* @throws NullPointerException If {@code connectionProvider} is {@code null}.
*/
AbstractDbData(Class<D> selfType, DataType dataType, ConnectionProvider connectionProvider) {
protected AbstractDbData(Class<D> selfType, DataType dataType, ConnectionProvider connectionProvider) {
super(selfType, connectionProvider);
this.dataType = dataType;
}

/**
* Only used for tests.
*
* @param selfType Class of DbElement.
* @param dataType Type of DbData.
*/
protected AbstractDbData(Class<D> selfType, DataType dataType) {
super(selfType);
this.dataType = dataType;
}

/**
* Returns the type of the data on which is the change.
*
Expand Down
88 changes: 14 additions & 74 deletions src/main/java/org/assertj/db/type/AbstractDbElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,7 @@ public abstract class AbstractDbElement<D extends AbstractDbElement<D>> implemen
/**
* Database connection provider.
*/
private ConnectionProvider connectionProvider;
/**
* Letter case of the tables.
*
* @since 1.1.0
*/
private LetterCase tableLetterCase = LetterCase.TABLE_DEFAULT;
/**
* Letter case of the columns.
*
* @since 1.1.0
*/
private LetterCase columnLetterCase = LetterCase.COLUMN_DEFAULT;
/**
* Letter case of the primary keys.
*
* @since 1.1.0
*/
private LetterCase primaryKeyLetterCase = LetterCase.PRIMARY_KEY_DEFAULT;

/**
* Default constructor.
*
* @param selfType Class of this element : a sub-class of {@code AbstractDbElement}.
*/
AbstractDbElement(Class<D> selfType) {
myself = selfType.cast(this);
setLetterCases();
}
private final ConnectionProvider connectionProvider;

/**
* Constructor.
Expand All @@ -73,89 +45,57 @@ public abstract class AbstractDbElement<D extends AbstractDbElement<D>> implemen
* @param connectionProvider The {@link ConnectionProvider} to connect to the database (must be not {@code null}).
* @throws NullPointerException If {@code connectionProvider} is {@code null}.
*/
AbstractDbElement(Class<D> selfType, ConnectionProvider connectionProvider) {
this(selfType);
protected AbstractDbElement(Class<D> selfType, ConnectionProvider connectionProvider) {
this.myself = selfType.cast(this);
if (connectionProvider == null) {
throw new IllegalArgumentException("connectionProvider can not be null");
}
this.connectionProvider = connectionProvider;
setLetterCases();
}

/**
* Sets the letter cases from information in parameters.
* Only used for tests.
*
* @param tableLetterCase Letter case of the tables.
* @param columnLetterCase Letter case of the columns.
* @param primaryKeyLetterCase Letter case of the primary keys.
* @return The actual instance.
* @param selfType Class of DbElement.
*/
D setLetterCases(LetterCase tableLetterCase, LetterCase columnLetterCase, LetterCase primaryKeyLetterCase) {
this.tableLetterCase = tableLetterCase;
this.columnLetterCase = columnLetterCase;
this.primaryKeyLetterCase = primaryKeyLetterCase;
return myself;
}

/**
* Sets the letter cases from information in {@code connectionProvider}.
*/
private void setLetterCases() {
if (connectionProvider == null) {
return;
}
tableLetterCase = connectionProvider.getTableLetterCase();
columnLetterCase = connectionProvider.getColumnLetterCase();
primaryKeyLetterCase = connectionProvider.getPrimaryKeyLetterCase();
protected AbstractDbElement(Class<D> selfType) {
this.myself = selfType.cast(this);
this.connectionProvider = null;
}

/**
* {@inheritDoc}
*/
@Override
public LetterCase getColumnLetterCase() {
return columnLetterCase;
return this.connectionProvider.getColumnLetterCase();
}

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

/**
* {@inheritDoc}
*/
@Override
public LetterCase getTableLetterCase() {
return tableLetterCase;
return this.connectionProvider.getTableLetterCase();
}

/**
* Return the connectionProvider.
*
* @return The {@link ConnectionProvider} to connect.
* @see #setConnectionProvider(ConnectionProvider)
*/
public ConnectionProvider getConnectionProvider() {
return connectionProvider;
}

/**
* Sets the connectionProvider.
*
* @param connectionProvider {@link ConnectionProvider} to connect to the database (must be not {@code null}).
* @return The actual instance.
* @throws NullPointerException If {@code connectionProvider} is {@code null}.
*/
public D setConnectionProvider(ConnectionProvider connectionProvider) {
if (connectionProvider == null) {
throw new NullPointerException("connectionProvider must be not null");
}
this.connectionProvider = connectionProvider;
setLetterCases();
return myself;
}

/**
* Returns a {@link Connection} from the {@link ConnectionProvider}
*
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/org/assertj/db/type/AssertDbConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2015-2024 the original author or authors.
*/
package org.assertj.db.type;

/**
* Entry point for creating database element ( Table, Request, Changes ) required to build assertion.
* Use {@link AssertDbConnectionFactory} to construct new instance of this builder.
* <p>
* Provider fluent builder for create Table, Request and Changes :
* <pre>
* <code class='java'>
* AssertDbConnection connection = ....;
* Table table = connection.table(&quot;movie&quot;).build();
* Request request = connection.request(&quot;select * from actor;&quot;).build();
* Changes changes = connection.changes().build();
* </code>
* </pre>
* <p>
* Some more advanced examples :
* <pre>
* <code class='java'>
* AssertDbConnection connection = ....;
* Table table = connection.table(&quot;movie&quot;).columnToCheck(new String[] { &quot;number&quot;, &quot;title&quot; }).build();
* Request request = connection.request(&quot;select * from actor where id = ?;&quot;).parameters(1).build();
* Changes changes = connection.changes().table(&quot;movie&quot;, t -> t.columnToCheck(new String[] { &quot;number&quot;, &quot;title&quot; })).build();
* </code>
* </pre>
*
* @author Julien Roy
* @since 3.0.0
*/
public class AssertDbConnection {

private final ConnectionProvider connectionProvider;

AssertDbConnection(ConnectionProvider connectionProvider) {
if (connectionProvider == null) {
throw new IllegalArgumentException("connectionProvider can not be null");
}
this.connectionProvider = connectionProvider;
}

/**
* Start building Table element.
*
* @param name Name of the table
* @return Table builder
*/
public Table.Builder table(String name) {
return new Table.Builder(this.connectionProvider, name);
}

/**
* Start building Request element.
*
* @param request SQL request
* @return Request builder
*/
public Request.Builder request(String request) {
return new Request.Builder(this.connectionProvider, request);
}

/**
* Start building Changes element.
*
* @return Changes builder
*/
public Changes.Builder changes() {
return new Changes.Builder(this.connectionProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@
import org.assertj.db.type.lettercase.LetterCase;

/**
* Fluent factory to create a connectionProvider from different input ( Jdbc URL or DataSource ).
* Factory to create a {@link AssertDbConnection} from different database connection input ( Jdbc URL or DataSource ).
* Allow to configure behavior of connection provider like letter case or schema metadata retrieval mode.
* <p>
* For create with JDBC URL :
* </p>
* <pre>
* <code class='java'>
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
* Table table = new Table(connectionProvider, &quot;movie&quot;);
* AssertDbConnection connection = AssertDbConnectionFactory.of(&quot;jdbc:h2:mem:test&quot;, &quot;sa&quot;, &quot;&quot;).create();
* Table table = connection.table(&quot;movie&quot;).build();
* </code>
* </pre>
* <p>
* For create with JDBC URL :
* <pre>
* <code class='java'>
* DataSource dataSource = ...;
* ConnectionProvider connectionProvider = ConnectionProviderFactory.of(dataSource).create();
* Table table = new Table(connectionProvider, &quot;song&quot;, new String[] { &quot;number&quot;, &quot;title&quot; }, null);
* AssertDbConnection connection = AssertDbConnectionFactory.of(dataSource).create();
* Table table = connection.table(&quot;song&quot;).columnToCheck(new String[] { &quot;number&quot;, &quot;title&quot; }).build();
* </code>
* </pre>
*
* @author Julien Roy
* @since 3.0.0
*/
public abstract class ConnectionProviderFactory {
public abstract class AssertDbConnectionFactory {

private ConnectionProviderFactory() {
private AssertDbConnectionFactory() {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -83,7 +83,7 @@ private DataSourceConnectionProviderFactory(DataSource dataSource) {
* {@inheritDoc}
*/
@Override
public ConnectionProvider create() {
public ConnectionProvider createConnectionProvider() {
return new DataSourceConnectionProvider(dataSource, this.schemaMetaDataMode.getType(), this.tableLetterCase, this.columnLetterCase, this.primaryKeyLetterCase);
}
}
Expand All @@ -108,7 +108,7 @@ private JdbcUrlConnectionProviderFactory(String url, String user, String passwor
* {@inheritDoc}
*/
@Override
public ConnectionProvider create() {
public ConnectionProvider createConnectionProvider() {
return new JdbcUrlConnectionProvider(url, user, password, this.schemaMetaDataMode.getType(), this.tableLetterCase, this.columnLetterCase, this.primaryKeyLetterCase);
}
}
Expand Down Expand Up @@ -170,6 +170,15 @@ public T schemaMetaDataMode(SchemaMetaDataMode mode) {
*
* @return Connection provider to use for Table, Request or Changes
*/
public abstract ConnectionProvider create();
protected abstract ConnectionProvider createConnectionProvider();

/**
* Build the Connection Provider
*
* @return Connection provider to use for Table, Request or Changes
*/
public AssertDbConnection create() {
return new AssertDbConnection(createConnectionProvider());
}
}
}
Loading

0 comments on commit c06f01f

Please sign in to comment.