acquireInstanceLock() — extract exit/dialog seam to enable unit testing

Offen
#169 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
68/100
Issue-Typ
Refactoring
Klarheit
Klar beschrieben
Aktivitätsstatus
Ruhig
Tech-Stack
java
Bereich
testing

Rechercherichtung

Start in App.java around acquireInstanceLock() (the issue points to lines 312–314) and trace how the lock and already-running branch are handled. Add unit tests for acquiring, detecting and releasing the lock, including the injectable callback. Done means those lifecycle cases pass and GUI startup behavior remains unchanged.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

enhancement

Background

App.acquireInstanceLock() uses NIO FileLock to prevent multiple GUI instances from running simultaneously. When a second instance is detected, it calls System.exit(0) inside a SwingUtilities.invokeLater lambda:

SwingUtilities.invokeLater(() -> {
    JOptionPane.showMessageDialog(...);
    System.exit(0);  // ← terminates the test JVM if called during tests
});

This is correct production behavior, but it creates a test hazard: any future test that exercises the GUI startup path and encounters an already-held lock will terminate the entire Maven Surefire JVM rather than failing the individual test cleanly.

Problem

  1. System.exit in test JVM — if a test triggers the "already running" branch, the whole test suite crashes with a cryptic Surefire failure instead of a clean test failure.
  2. No coverage of the lock lifecycle — there are no tests verifying: first call acquires lock → true; second call finds it held → false; lock releases on channel close; next startup succeeds.
    Proposed fix
    Extract the exit and dialog side-effects behind an injectable strategy (e.g. a Runnable or BiConsumer parameter defaulting to the real System.exit + JOptionPane call). This makes acquireInstanceLock() unit-testable by passing a no-op or capture lambda in tests.
// Example seam
static boolean acquireInstanceLock(Runnable onAlreadyRunning) { ... }
// Production call (unchanged behavior)
acquireInstanceLock(() -> {
    SwingUtilities.invokeLater(() -> {
        JOptionPane.showMessageDialog(...);
        System.exit(0);
    });
});

Acceptance criteria

  • acquireInstanceLock() does not call System.exit directly — exit/dialog behavior is injectable
  • Unit test: first call acquires lock, returns true
  • Unit test: second call with lock held returns false and invokes the onAlreadyRunning callback
  • Unit test: releasing the channel allows a subsequent call to succeed
  • Existing GUI startup behavior unchanged

References

  • Flagged in PR code review (Copilot, medium severity) on icon-base branch, App.java lines 312–314
Vorherrschende Sprache
Java
Sterne
6
Forks
6
Ø Merge
10 Std. 11 Min.
Gemergte PRs (30 T.)
12

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus JDiskMark/jdm-java

Alle Issues in JDiskMark/jdm-java

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.