Permalink
Browse files

Added new keyword "Row Should Not Exist In Table" and two tests as re…

…quested.

Fixes issue 16
The new keyword has been implemented with only minor changes and will be included in the next release.
  • Loading branch information...
1 parent 159e01d commit 28637e3ed869d9da236f719c28f7d5cce2a41077 spielmann.robert@googlemail.com committed Aug 1, 2011
@@ -900,6 +900,41 @@ public void verifyNumberOfRowsMatchingWhere(String tableName, String where,
}
}
+ /**
+ * This keyword can be used to check the inexistence of content inside
+ * a specific row in a database table defined by a where-clause.
+ * This can be used to validate an exclusion of specific data from a table.
+ *
+ * <pre>
+ * Example:
+ * | Row Should Not Exist In Table | MySampleTable | Name='John Doe' |
+ * </pre>
+ *
+ * @throws SQLException
+ * @throws DatabaseLibraryException
+ */
+ @RobotKeyword("Validate data inexistence from table defined by a where-clause.\n\n"
+ + "Example:\n"
+ + "| Row Should Not Exist In Table | MySampleTable | Name='John Doe' |\n")
+ @ArgumentNames( { "tableName", "whereClause" })
+ public void rowShouldNotExistInTable(String tableName, String whereClause)
+ throws SQLException, DatabaseLibraryException {
+
+ String sql = "select * from " + tableName + " where " + whereClause;
+ Statement stmt = getConnection().createStatement();
+ try {
+ stmt.executeQuery(sql);
+ ResultSet rs = (ResultSet) stmt.getResultSet();
+ if(rs.next() == true) {
+ throw new DatabaseLibraryException("Row exists (but should not) for where-clause: "
+ + whereClause + " in table: " + tableName);
+ }
+ } finally {
+ // stmt.close() automatically takes care of its ResultSet, so no rs.close()
+ stmt.close();
+ }
+ }
+
private void setConnection(Connection connection) {
this.connection = connection;
}
@@ -491,5 +491,22 @@ public void checkCompareQueryResultToFileWhereFileNotFound() throws Exception {
databaseLibrary.compareQueryResultToFile("select name, email from mySampleTable order by Id",
myFileName);
}
+
+ // Tests for "Row Should Not Exist In Table"
+ @Test
+ public void checkRowShouldNotExistInTable() throws SQLException, DatabaseLibraryException {
+ databaseLibrary.rowShouldNotExistInTable("MySampleTable", "Name='John Doe'");
+ }
+
+ @Test
+ public void checkRowShouldNotExistInTableFailsIfRowExists() throws SQLException, DatabaseLibraryException {
+ try {
+ databaseLibrary.rowShouldNotExistInTable("MySampleTable", "Name='Darth Vader'");
+ fail();
+ } catch(DatabaseLibraryException e) {
+ Assert.assertEquals("Row exists (but should not) for where-clause: Name='Darth Vader' in table: MySampleTable", e.getMessage());
+ }
+ }
+
}

0 comments on commit 28637e3

Please sign in to comment.