Skip to content

Commit

Permalink
add methods for checking existence on table assertion
Browse files Browse the repository at this point in the history
part for resolving #12
  • Loading branch information
Avinash-Bhat authored and VanRoy committed Dec 24, 2019
1 parent 2a2cbfb commit 924b5d2
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/assertj/db/api/AbstractDbAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class AbstractDbAssert<D extends AbstractDbData<D>, A extends Ab
/**
* The actual value on which the assertion is.
*/
private final D actual;
protected final D actual;

/**
* Position of navigation to column.
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/org/assertj/db/api/TableAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.assertj.db.api;

import org.assertj.db.api.assertions.AssertOnExistence;
import org.assertj.db.api.assertions.impl.AssertionsOnTableExistence;
import org.assertj.db.type.Table;

/**
Expand All @@ -21,7 +23,8 @@
*
*/
public class TableAssert
extends AbstractDbAssert<Table, TableAssert, TableColumnAssert, TableColumnValueAssert, TableRowAssert, TableRowValueAssert> {
extends AbstractDbAssert<Table, TableAssert, TableColumnAssert, TableColumnValueAssert, TableRowAssert, TableRowValueAssert>
implements AssertOnExistence<TableAssert> {

/**
* Constructor.
Expand All @@ -31,4 +34,38 @@ public class TableAssert
TableAssert(Table table) {
super(table, TableAssert.class, TableColumnAssert.class, TableRowAssert.class);
}

/**
* Verifies that the table exist.
* <p>
* Example where the assertion verifies that the table exists:
* </p>
*
* <pre><code class='java'>
* assertThat(table).exists();
* </code></pre>
*
* @return {@code this} assertion object.
*/
@Override
public TableAssert exists() {
return AssertionsOnTableExistence.exists(this, info, actual.getName(), actual.getSource(), actual.getDataSource());
}

/**
* Verifies that the table doesn't exist.
* <p>
* Example where the assertion verifies that the table doesn't exists:
* </p>
*
* <pre><code class='java'>
* assertThat(table).doesNotExists();
* </code></pre>
*
* @return {@code this} assertion object.
*/
@Override
public TableAssert doesNotExist() {
return null;
}
}
26 changes: 26 additions & 0 deletions src/main/java/org/assertj/db/api/assertions/AssertOnExistence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.assertj.db.api.assertions;

/**
* Defines the assertion method on existence of something.
*
* @param <T> The "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
* for more details.
* @author Avinash Ananth Narayan R
*/
public interface AssertOnExistence<T extends AssertOnExistence<T>> {

/**
* Verifies that the thing represented exist.
*
* @return {@code this} assertion object.
*/
T exists();

/**
* Verifies that the thing represented doesn't exist.
*
* @return {@code this} assertion object.
*/
T doesNotExist();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
* for more details.
* @author Régis Pouiller
*/
public interface AssertOnRowOfChangeExistence<T extends AssertOnRowOfChangeExistence<T>> {
public interface AssertOnRowOfChangeExistence<T extends AssertOnRowOfChangeExistence<T>> extends
AssertOnExistence<T> {

/**
* Verifies that the row of the change exists.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.assertj.db.api.assertions.impl;

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 java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import static org.assertj.db.error.ShouldExist.shouldExist;
import static org.assertj.db.error.ShouldNotExist.shouldNotExist;

/**
* Implements the assertion method on the existence of a table
* @author Avinash Ananth Narayan
*/
public class AssertionsOnTableExistence {

/**
* To notice failures in the assertion.
*/
private final static Failures failures = Failures.instance();

private AssertionsOnTableExistence() {
// Empty
}

public static <A extends AbstractDbAssert> A exists(A assertion, WritableAssertionInfo info,
String table, Source source, DataSource dataSource) {
try (Connection connection = getConnection(source, dataSource)) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet result = metaData.getTables(null, null, table, null);
if (!result.next()) {
throw failures.failure(info, shouldExist());
}
result.close();
} catch (SQLException e) {
throw new AssertJDBException(e);
}
return assertion;
}

public static <A extends AbstractDbAssert> A doesNotExists(A assertion, WritableAssertionInfo info,
String table, Source source, DataSource dataSource) {
try (Connection connection = getConnection(source, dataSource)) {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet result = metaData.getTables(null, null, table, null);
if (result.next()) {
throw failures.failure(info, shouldNotExist());
}
result.close();
} catch (SQLException e) {
throw new AssertJDBException(e);
}
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());
}
}

0 comments on commit 924b5d2

Please sign in to comment.