Test that Resources are Closed
package net.unicon;
import java.sql.*;
class ThingTest3 extends GroovyTestCase {
void testTestMe() {
int score = 42;
int erId = -42;
int rowcount = 0;
boolean rsClosed = false; //NEW
def rs = [ getInt : { pos -> assertEquals(1, pos); score; },
next : { -> rowcount++ == 0 }, //lexical scope is important
close : { -> assertFalse(rsClosed); rsClosed = true; } //NEW
] as ResultSet;
boolean stmtClosed = false; //NEW
def stmt = [ executeQuery : { -> rs; }, //no arguments to this closure
setInt : { pos, id ->
assertEquals(1, pos);
assertEquals(erId, id);
},
close : { -> assertFalse(stmtClosed); stmtClosed = true; } //NEW
] as PreparedStatement;
def conn = [ prepareStatement : { q->
assertEquals(Thing3.query, q); //Whoa!!
return stmt; //can't resist "return"
}
] as Connection;
def thing = new Thing3();
assertEquals(score, thing.testMe(conn, erId));
assertTrue(rsClosed); //NEW
assertTrue(stmtClosed); //NEW
}
}
Next