diff --git a/src/main/java/org/assertj/db/api/AbstractDbAssert.java b/src/main/java/org/assertj/db/api/AbstractDbAssert.java index 3bfbaf22..cdcd86b1 100644 --- a/src/main/java/org/assertj/db/api/AbstractDbAssert.java +++ b/src/main/java/org/assertj/db/api/AbstractDbAssert.java @@ -49,7 +49,7 @@ public abstract class AbstractDbAssert, A extends Ab /** * The actual value on which the assertion is. */ - private final D actual; + protected final D actual; /** * Position of navigation to column. diff --git a/src/main/java/org/assertj/db/api/TableAssert.java b/src/main/java/org/assertj/db/api/TableAssert.java index 64b5981e..6d4a5d4e 100644 --- a/src/main/java/org/assertj/db/api/TableAssert.java +++ b/src/main/java/org/assertj/db/api/TableAssert.java @@ -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; /** @@ -21,7 +23,8 @@ * */ public class TableAssert - extends AbstractDbAssert { + extends AbstractDbAssert + implements AssertOnExistence { /** * Constructor. @@ -31,4 +34,38 @@ public class TableAssert TableAssert(Table table) { super(table, TableAssert.class, TableColumnAssert.class, TableRowAssert.class); } + + /** + * Verifies that the table exist. + *

+ * Example where the assertion verifies that the table exists: + *

+ * + *

+   * assertThat(table).exists();
+   * 
+ * + * @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. + *

+ * Example where the assertion verifies that the table doesn't exists: + *

+ * + *

+   * assertThat(table).doesNotExists();
+   * 
+ * + * @return {@code this} assertion object. + */ + @Override + public TableAssert doesNotExist() { + return null; + } } diff --git a/src/main/java/org/assertj/db/api/assertions/AssertOnExistence.java b/src/main/java/org/assertj/db/api/assertions/AssertOnExistence.java new file mode 100644 index 00000000..3b532f7c --- /dev/null +++ b/src/main/java/org/assertj/db/api/assertions/AssertOnExistence.java @@ -0,0 +1,26 @@ +package org.assertj.db.api.assertions; + +/** + * Defines the assertion method on existence of something. + * + * @param The "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" + * for more details. + * @author Avinash Ananth Narayan R + */ +public interface AssertOnExistence> { + + /** + * 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(); +} diff --git a/src/main/java/org/assertj/db/api/assertions/AssertOnRowOfChangeExistence.java b/src/main/java/org/assertj/db/api/assertions/AssertOnRowOfChangeExistence.java index 8ead946b..21ef11a7 100644 --- a/src/main/java/org/assertj/db/api/assertions/AssertOnRowOfChangeExistence.java +++ b/src/main/java/org/assertj/db/api/assertions/AssertOnRowOfChangeExistence.java @@ -20,7 +20,8 @@ * for more details. * @author RĂ©gis Pouiller */ -public interface AssertOnRowOfChangeExistence> { +public interface AssertOnRowOfChangeExistence> extends + AssertOnExistence { /** * Verifies that the row of the change exists. diff --git a/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnTableExistence.java b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnTableExistence.java new file mode 100644 index 00000000..9aa074f5 --- /dev/null +++ b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnTableExistence.java @@ -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 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 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()); + } +}