-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add methods for checking existence on table assertion
part for resolving #12
- Loading branch information
1 parent
2a2cbfb
commit 924b5d2
Showing
5 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/org/assertj/db/api/assertions/AssertOnExistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "<a href="http://bit.ly/1IZIRcY" | ||
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>" | ||
* 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnTableExistence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |