我运行了JUnit测试,在名为delete
的持久化类中调用了以下PStudent
方法:
@Override
public boolean delete(int studentID) {
System.out.println("delete - studentID = " + studentID);
if (studentID < 1)
throw new IllegalArgumentException("Parameter \"studentID\" "
+ "must be greater than 0!");
Student student = get(studentID);
if (student == null)
return false;
// I would do important things now if only the ResultSet in get()
// weren't always empty
return true;
}
这是从get()
内部调用时似乎无法正常工作的delete()
方法(相同的子类):
@Override
public Student get(int studentID) {
if (studentID < 1)
throw new IllegalArgumentException("Parameter \"studentID\" "
+ "muss größer als 0 sein!");
Student student = null;
// get student data
try {
String sql = "SELECT * FROM student WHERE studentID = ?";
PreparedStatement ps = conn.prepareStatement(sql);
System.out.println("get - studentID = " + studentID);
// SQuirreL showed that this ID did exist in "student"
ps.setInt(1, studentID);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
// This is what normally happens when this method is called
// directly (some 'student.something = rs.getInt("something")'
// commands). Works all fine.
student = getStudentFromResults(rs);
} else {
// This is what happens when called from within delete().
errorLog.log(Level.WARNING, "Empty ResultSet!");
return null;
}
} catch (SQLException e) {
errorLog.log(Level.WARNING, "Error during query!", e);
return null;
}
return student;
}
正如评论所示,get
方法返回一个空的ResultSet
,尽管查询是正确的(get
方法在JUnit测试期间的任何时候直接调用时工作正常相同参数),参数studentID
本身是正确的(通过不同点的几个System.out.println
命令检查)。这是相应的控制台输出(ID“2”是正确的):
delete - studentID = 2
get - studentID = 2
mar 02, 2016 12:17:32 EM some.package.SomeClass get
WARNING: Empty ResultSet!
连接conn
的检索方式如下:
String URL = "jdbc:derby://localhost:1527/my_db";
Connection conn = DriverManager.getConnection(URL, "user", "password");
编辑:这些是JUnit测试。所有以P开头的类名都是表示逻辑和数据库之间接口的持久化类。对于每个这些对象,在实例化期间打开一个连接(如上所示):
public class LoggingTest {
private PActionLogElement pLog;
@Before
public void setUp() {
MyDataSource.setTestMode(true);
try {
pLog = new PActionLogElement(1);
} catch (SQLException e) {
fail("PActionLogElement(int): Verbindung zur DB konnte nicht "
+ "aufgebaut werden!");
}
// empty log in order to make sure that the assertions work properly
pLog.delete(null);
pLog.close();
pLog = null;
}
@After
public void tearDown() {
MyDataSource.setTestMode(false);
}
@Test
public void testPStudentLogging() {
PStudent ps = null;
try {
ps = new PStudent(1);
} catch (SQLException e) {
fail("testPStudentLogging(): PStudent(int) -- Verbindung zur "
+ "DB konnte nicht hergestellt werden!");
}
try {
pLog = new PActionLogElement(1);
} catch (SQLException e) {
fail("testPStudentLogging(): PActionLogElement(int) -- "
+ "Verbindung zur DB konnte nicht hergestellt werden!");
}
// Set up new model object and fill it with (correct) values
Student s = new Student();
s.setFirstName("Test");
s.setLastName("Person");
s.setEmail("test@person.de");
s.setPassword("test");
Set<GlobalRoleEnum> globalRoles = new HashSet<>();
globalRoles.add(GlobalRoleEnum.OTHER);
s.setGlobalRoles(globalRoles);
s.setMatrNum("123");
s.setDegreeCourse("Katastrophenmanagement");
/******************
* PStudent.add() *
******************/
int studentID = ps.add(s);
if (studentID == BusinessObjectPersistence.FAIL)
fail("testPStudentLogging(): PStudent.add() gibt FAIL zurück.");
s.setStudentID(studentID);
// System.out.println("studentID = " + studentID);
List<ActionLogElement> log = pLog.get(null);
assertEquals(2, log.size());
assertTrue(log.get(1).getContent().contains(s.getFirstName()));
assertTrue(log.get(1).getContent().contains(s.getLastName()));
assertTrue(log.get(1).getContent().contains(
"(studentID " + studentID + ")"));
// clear log
pLog.delete(null);
/*********************
* PStudent.update() *
*********************/
s.setDegreeCourse("Skandinavistik");
if (!ps.update(s))
fail("testPStudentLogging(): PStudent.update() stinkt.");
log = pLog.get(null);
assertEquals(2, log.size());
assertTrue(log.get(1).getContent().contains(s.getFirstName()));
assertTrue(log.get(1).getContent().contains(s.getLastName()));
assertTrue(log.get(1).getContent().contains(
"(studentID " + studentID + ")"));
// clear log
pLog.delete(null);
/*********************
* PStudent.delete() *
*********************/
if (!ps.delete(studentID))
fail("testPStudentLogging(): PStudent.delete() hat "
+ "Stimmungsschwankungen.");
// Executes "SELECT * FROM log" and returns all log elements
log = pLog.get(null);
assertEquals(2, log.size());
assertTrue(log.get(1).getContent().contains(s.getFirstName()));
assertTrue(log.get(1).getContent().contains(s.getLastName()));
assertTrue(log.get(1).getContent().contains(
"(studentID " + studentID + ")"));
// close connections
pLog.close();
ps.close();
}
}