Skip to content

Commit 12a49de

Browse files
authored
Make JdbcExampleTest robust to reruns and command registration race (#2787)
The test hard-coded the booking id (1) and asserted on a shared literal flight code (AF520). The example stores bookings in an AUTO_INCREMENT table shared across runs of the same Karaf instance, so when a transient CommandNotFoundException on booking:remove triggered the whole-method @Retry, the rerun's booking:add got id 2 while id 1's row lingered, making booking:remove 1 + assertContainsNot("AF520") fail deterministically. Use a unique flight code per run and resolve the actual auto-incremented id from the booking:list output instead of assuming 1, so leftover rows no longer disturb the assertions and the retry can genuinely recover.
1 parent 9619241 commit 12a49de

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,46 @@ public void test() throws Exception {
3737
// install the karaf-jdbc-example feature
3838
installAndAssertFeature("karaf-jdbc-example");
3939

40+
// use a unique flight code so the assertions are not disturbed by bookings possibly
41+
// left over by a previous (retried) run: the example stores bookings in an AUTO_INCREMENT
42+
// table that is shared across runs of the same Karaf instance
43+
String flight = "AF" + System.currentTimeMillis();
44+
4045
// add booking
41-
executeCommand("booking:add Foo AF520");
46+
executeCommand("booking:add Foo " + flight);
4247
// list booking
4348
String bookings = executeCommand("booking:list");
4449
System.out.println(bookings);
45-
assertContains("AF520", bookings);
50+
assertContains(flight, bookings);
51+
52+
// resolve the actual (auto-incremented) id instead of assuming it is 1
53+
long id = bookingId(bookings, flight);
54+
4655
// get booking
47-
String booking = executeCommand("booking:get 1");
56+
String booking = executeCommand("booking:get " + id);
4857
System.out.println(booking);
49-
assertContains("AF520", booking);
58+
assertContains(flight, booking);
5059
// remove booking
51-
executeCommand("booking:remove 1");
60+
executeCommand("booking:remove " + id);
5261
bookings = executeCommand("booking:list");
5362
System.out.println(bookings);
54-
assertContainsNot("AF520", bookings);
63+
assertContainsNot(flight, bookings);
64+
}
65+
66+
/**
67+
* Extracts the booking id (first column) of the row matching the given flight code from the
68+
* {@code booking:list} shell table output.
69+
*/
70+
private long bookingId(String listOutput, String flight) {
71+
for (String line : listOutput.split("\\r?\\n")) {
72+
if (line.contains(flight)) {
73+
String id = line.split("\\|")[0].trim();
74+
if (id.matches("\\d+")) {
75+
return Long.parseLong(id);
76+
}
77+
}
78+
}
79+
throw new IllegalStateException("No booking found for flight " + flight + " in:\n" + listOutput);
5580
}
5681

5782
}

0 commit comments

Comments
 (0)