Java Reference
In-Depth Information
resultSet.close();
connection.close();
return
rsdc.getRows();
}
D
private
Connection getConnection()
throws
NamingException, SQLException {
//RETURN SOME DATABASE CONNECTION
}
}
We call the
execute
method from the servlet with the given query; there we try to
obtain a valid connection and execute the query
B
. After that we create a
RowSet-
DynaClass
object from the
ResultSet
C
, and we return its rows
D
.
In order to test the
EJB
with Cactus, we have to instantiate it and then assert against
the result of the execution. We can use, again, mock objects to simulate the
JNDI
lookup, but this approach is unnecessarily complicated, so we won't list it here. Let's
look at the test case for the
EJB
in listing 14.12, and then we'll go through it and dis-
cuss it.
Listing 14.12
Test case for
AdministratorEJB
[...]
public class
TestAdministratorEJB
extends
ServletTestCase {
private
IAdministratorLocal administrator;
public
void
setUp()
throws
Exception {
Properties properties =
new
Properties();
properties.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs",
"org.jboss.naming rg.jnp.interfaces");
B
InitialContext ctx =
new
InitialContext(properties);
administrator = (IAdministratorLocal) ctx.lookup(
"ch14-cactus-ear-cactified/"
+AdministratorBean.class.getSimpleName()
+"/local");
C
}
public
void
testExecute()
throws
Exception {
String sql = "SELECT * FROM CUSTOMER";
Collection result = administrator.execute(sql);
Iterator beans = result.iterator();
assertTrue(beans.hasNext());
DynaBean bean1 = (DynaBean) beans.next();
assertEquals(bean1.get("Id"),
new
Integer(1));
assertEquals(bean1.get("name"), "John Doe");
assertTrue(!beans.hasNext());
}
}
D