This is an automated email from the ASF dual-hosted git repository.

holgerfriedrich pushed a commit to branch karaf-4.4.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.4.x by this push:
     new fdd1b917c0 Make JdbcExampleTest robust to reruns and command 
registration race (backport #2787) (#2790)
fdd1b917c0 is described below

commit fdd1b917c00a237472c3386057972cdac1e3724e
Author: JB Onofré <[email protected]>
AuthorDate: Sat Aug 15 17:06:00 2026 +0200

    Make JdbcExampleTest robust to reruns and command registration race 
(backport #2787) (#2790)
    
    * Make JdbcExampleTest robust to reruns and command registration race
    
    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.
    
    * Fix JdbcExampleTest booking id parsing for ShellTable Unicode column 
separator
    
    The booking:list output is rendered by ShellTable using the Unicode
    box-drawing vertical bar (U+2502) as the column separator, not an ASCII
    '|'. The bookingId helper split on '|' and therefore never located the
    id column, throwing IllegalStateException. Extract the leading id digits
    directly instead.
---
 .../karaf/itests/examples/JdbcExampleTest.java     | 41 ++++++++++++++++++----
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git 
a/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
 
b/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
index 6128b463dd..b598cdd7fa 100644
--- 
a/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
+++ 
b/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
@@ -37,21 +37,50 @@ public class JdbcExampleTest extends BaseTest {
         // install the karaf-jdbc-example feature
         installAndAssertFeature("karaf-jdbc-example");
 
+        // use a unique flight code so the assertions are not disturbed by 
bookings possibly
+        // left over by a previous (retried) run: the example stores bookings 
in an AUTO_INCREMENT
+        // table that is shared across runs of the same Karaf instance
+        String flight = "AF" + System.currentTimeMillis();
+
         // add booking
-        executeCommand("booking:add Foo AF520");
+        executeCommand("booking:add Foo " + flight);
         // list booking
         String bookings = executeCommand("booking:list");
         System.out.println(bookings);
-        assertContains("AF520", bookings);
+        assertContains(flight, bookings);
+
+        // resolve the actual (auto-incremented) id instead of assuming it is 1
+        long id = bookingId(bookings, flight);
+
         // get booking
-        String booking = executeCommand("booking:get 1");
+        String booking = executeCommand("booking:get " + id);
         System.out.println(booking);
-        assertContains("AF520", booking);
+        assertContains(flight, booking);
         // remove booking
-        executeCommand("booking:remove 1");
+        executeCommand("booking:remove " + id);
         bookings = executeCommand("booking:list");
         System.out.println(bookings);
-        assertContainsNot("AF520", bookings);
+        assertContainsNot(flight, bookings);
+    }
+
+    /**
+     * Extracts the booking id (first column) of the row matching the given 
flight code from the
+     * {@code booking:list} shell table output.
+     */
+    private long bookingId(String listOutput, String flight) {
+        // the booking:list output is a ShellTable whose columns are separated 
by the Unicode
+        // box-drawing vertical bar (U+2502), not an ASCII '|', so extract the 
leading id digits
+        // directly rather than splitting on a column separator
+        java.util.regex.Pattern idPattern = 
java.util.regex.Pattern.compile("^\\s*(\\d+)");
+        for (String line : listOutput.split("\\r?\\n")) {
+            if (line.contains(flight)) {
+                java.util.regex.Matcher matcher = idPattern.matcher(line);
+                if (matcher.find()) {
+                    return Long.parseLong(matcher.group(1));
+                }
+            }
+        }
+        throw new IllegalStateException("No booking found for flight " + 
flight + " in:\n" + listOutput);
     }
 
 }

Reply via email to