Skip to content

Commit 924b5d2

Browse files
Avinash-BhatVanRoy
authored andcommitted
add methods for checking existence on table assertion
part for resolving #12
1 parent 2a2cbfb commit 924b5d2

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public abstract class AbstractDbAssert<D extends AbstractDbData<D>, A extends Ab
4949
/**
5050
* The actual value on which the assertion is.
5151
*/
52-
private final D actual;
52+
protected final D actual;
5353

5454
/**
5555
* Position of navigation to column.

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.assertj.db.api;
1414

15+
import org.assertj.db.api.assertions.AssertOnExistence;
16+
import org.assertj.db.api.assertions.impl.AssertionsOnTableExistence;
1517
import org.assertj.db.type.Table;
1618

1719
/**
@@ -21,7 +23,8 @@
2123
*
2224
*/
2325
public class TableAssert
24-
extends AbstractDbAssert<Table, TableAssert, TableColumnAssert, TableColumnValueAssert, TableRowAssert, TableRowValueAssert> {
26+
extends AbstractDbAssert<Table, TableAssert, TableColumnAssert, TableColumnValueAssert, TableRowAssert, TableRowValueAssert>
27+
implements AssertOnExistence<TableAssert> {
2528

2629
/**
2730
* Constructor.
@@ -31,4 +34,38 @@ public class TableAssert
3134
TableAssert(Table table) {
3235
super(table, TableAssert.class, TableColumnAssert.class, TableRowAssert.class);
3336
}
37+
38+
/**
39+
* Verifies that the table exist.
40+
* <p>
41+
* Example where the assertion verifies that the table exists:
42+
* </p>
43+
*
44+
* <pre><code class='java'>
45+
* assertThat(table).exists();
46+
* </code></pre>
47+
*
48+
* @return {@code this} assertion object.
49+
*/
50+
@Override
51+
public TableAssert exists() {
52+
return AssertionsOnTableExistence.exists(this, info, actual.getName(), actual.getSource(), actual.getDataSource());
53+
}
54+
55+
/**
56+
* Verifies that the table doesn't exist.
57+
* <p>
58+
* Example where the assertion verifies that the table doesn't exists:
59+
* </p>
60+
*
61+
* <pre><code class='java'>
62+
* assertThat(table).doesNotExists();
63+
* </code></pre>
64+
*
65+
* @return {@code this} assertion object.
66+
*/
67+
@Override
68+
public TableAssert doesNotExist() {
69+
return null;
70+
}
3471
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.assertj.db.api.assertions;
2+
3+
/**
4+
* Defines the assertion method on existence of something.
5+
*
6+
* @param <T> The "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
7+
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
8+
* for more details.
9+
* @author Avinash Ananth Narayan R
10+
*/
11+
public interface AssertOnExistence<T extends AssertOnExistence<T>> {
12+
13+
/**
14+
* Verifies that the thing represented exist.
15+
*
16+
* @return {@code this} assertion object.
17+
*/
18+
T exists();
19+
20+
/**
21+
* Verifies that the thing represented doesn't exist.
22+
*
23+
* @return {@code this} assertion object.
24+
*/
25+
T doesNotExist();
26+
}

src/main/java/org/assertj/db/api/assertions/AssertOnRowOfChangeExistence.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* for more details.
2121
* @author Régis Pouiller
2222
*/
23-
public interface AssertOnRowOfChangeExistence<T extends AssertOnRowOfChangeExistence<T>> {
23+
public interface AssertOnRowOfChangeExistence<T extends AssertOnRowOfChangeExistence<T>> extends
24+
AssertOnExistence<T> {
2425

2526
/**
2627
* Verifies that the row of the change exists.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.assertj.db.api.assertions.impl;
2+
3+
import org.assertj.core.api.WritableAssertionInfo;
4+
import org.assertj.core.internal.Failures;
5+
import org.assertj.db.api.AbstractDbAssert;
6+
import org.assertj.db.exception.AssertJDBException;
7+
import org.assertj.db.type.Source;
8+
9+
import java.sql.Connection;
10+
import java.sql.DatabaseMetaData;
11+
import java.sql.DriverManager;
12+
import java.sql.ResultSet;
13+
import java.sql.SQLException;
14+
15+
import javax.sql.DataSource;
16+
17+
import static org.assertj.db.error.ShouldExist.shouldExist;
18+
import static org.assertj.db.error.ShouldNotExist.shouldNotExist;
19+
20+
/**
21+
* Implements the assertion method on the existence of a table
22+
* @author Avinash Ananth Narayan
23+
*/
24+
public class AssertionsOnTableExistence {
25+
26+
/**
27+
* To notice failures in the assertion.
28+
*/
29+
private final static Failures failures = Failures.instance();
30+
31+
private AssertionsOnTableExistence() {
32+
// Empty
33+
}
34+
35+
public static <A extends AbstractDbAssert> A exists(A assertion, WritableAssertionInfo info,
36+
String table, Source source, DataSource dataSource) {
37+
try (Connection connection = getConnection(source, dataSource)) {
38+
DatabaseMetaData metaData = connection.getMetaData();
39+
ResultSet result = metaData.getTables(null, null, table, null);
40+
if (!result.next()) {
41+
throw failures.failure(info, shouldExist());
42+
}
43+
result.close();
44+
} catch (SQLException e) {
45+
throw new AssertJDBException(e);
46+
}
47+
return assertion;
48+
}
49+
50+
public static <A extends AbstractDbAssert> A doesNotExists(A assertion, WritableAssertionInfo info,
51+
String table, Source source, DataSource dataSource) {
52+
try (Connection connection = getConnection(source, dataSource)) {
53+
DatabaseMetaData metaData = connection.getMetaData();
54+
ResultSet result = metaData.getTables(null, null, table, null);
55+
if (result.next()) {
56+
throw failures.failure(info, shouldNotExist());
57+
}
58+
result.close();
59+
} catch (SQLException e) {
60+
throw new AssertJDBException(e);
61+
}
62+
return assertion;
63+
}
64+
65+
private static Connection getConnection(Source source, DataSource dataSource) throws SQLException {
66+
if (source == null && dataSource == null) {
67+
throw new NullPointerException("connection or dataSource must be not null");
68+
}
69+
if (dataSource != null) {
70+
return dataSource.getConnection();
71+
}
72+
return DriverManager.getConnection(source.getUrl(), source.getUser(), source.getPassword());
73+
}
74+
}

0 commit comments

Comments
 (0)