1
0

spring-event-synthesizer.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Spring application-event bridge (Java).
  3. *
  4. * Spring decouples an event publisher from its listener(s) through the application
  5. * event bus, linked by the EVENT TYPE: `eventPublisher.publishEvent(new XEvent(...))`
  6. * has no static edge to the `@EventListener void on(XEvent e)` that handles it (usually
  7. * in a different file). This bridges each `publishEvent(new XEvent(...))` site to every
  8. * listener of XEvent. Covers all four listener forms — param-typed `@EventListener`,
  9. * annotation-typed `@EventListener(XEvent.class)`, `@TransactionalEventListener`, and the
  10. * older `implements ApplicationListener<XEvent>` / `onApplicationEvent` — fans out to
  11. * multiple listeners of the same event, and proves precision: a published event with no
  12. * listener, and a same-file non-annotated method, both produce no edge.
  13. */
  14. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  15. import * as fs from 'node:fs';
  16. import * as path from 'node:path';
  17. import * as os from 'node:os';
  18. import { CodeGraph } from '../src';
  19. describe('spring-event synthesizer', () => {
  20. let dir: string;
  21. beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spring-event-')); });
  22. afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); });
  23. const write = (rel: string, body: string) => {
  24. const p = path.join(dir, rel);
  25. fs.mkdirSync(path.dirname(p), { recursive: true });
  26. fs.writeFileSync(p, body);
  27. };
  28. it('bridges publishEvent(new X) to every listener form of X, ignoring unheard events and non-listeners', async () => {
  29. write('shop/OrderEvents.java', `package shop;
  30. class OrderShippedEvent { }
  31. class OrderCancelledEvent { }
  32. class UnheardEvent { }
  33. `);
  34. // Publisher — two events, one of them (UnheardEvent) has no listener.
  35. write('shop/OrderService.java', `package shop;
  36. import org.springframework.context.ApplicationEventPublisher;
  37. class OrderService {
  38. private ApplicationEventPublisher publisher;
  39. void ship() {
  40. publisher.publishEvent(new OrderShippedEvent());
  41. publisher.publishEvent(new UnheardEvent());
  42. }
  43. void cancel() {
  44. publisher.publishEvent(new OrderCancelledEvent());
  45. }
  46. }
  47. `);
  48. // Form 1: param-typed @EventListener — plus a same-file NON-listener (no annotation).
  49. write('shop/ShippingListener.java', `package shop;
  50. import org.springframework.context.event.EventListener;
  51. class ShippingListener {
  52. @EventListener
  53. public void onShipped(OrderShippedEvent event) { }
  54. public void helper(OrderShippedEvent event) { }
  55. }
  56. `);
  57. // Form 2: annotation-typed @EventListener(X.class) — fan-out, a 2nd OrderShipped listener.
  58. write('shop/AuditListener.java', `package shop;
  59. import org.springframework.context.event.EventListener;
  60. class AuditListener {
  61. @EventListener(OrderShippedEvent.class)
  62. public void audit(OrderShippedEvent event) { }
  63. }
  64. `);
  65. // Form 3: @TransactionalEventListener — a 3rd OrderShipped listener.
  66. write('shop/TxListener.java', `package shop;
  67. import org.springframework.transaction.event.TransactionalEventListener;
  68. class TxListener {
  69. @TransactionalEventListener
  70. public void afterShipped(OrderShippedEvent event) { }
  71. }
  72. `);
  73. // Form 4: older implements ApplicationListener<X> / onApplicationEvent.
  74. write('shop/LegacyListener.java', `package shop;
  75. import org.springframework.context.ApplicationListener;
  76. class LegacyListener implements ApplicationListener<OrderCancelledEvent> {
  77. @Override
  78. public void onApplicationEvent(OrderCancelledEvent event) { }
  79. }
  80. `);
  81. const cg = await CodeGraph.init(dir, { silent: true });
  82. await cg.indexAll();
  83. const db = (cg as any).db.db;
  84. const edges = db
  85. .prepare(
  86. `SELECT s.name source, t.name target, json_extract(e.metadata,'$.via') via
  87. FROM edges e JOIN nodes s ON s.id = e.source JOIN nodes t ON t.id = e.target
  88. WHERE json_extract(e.metadata,'$.synthesizedBy') = 'spring-event'`
  89. )
  90. .all();
  91. const targets = (src: string) =>
  92. edges.filter((r: any) => r.source === src).map((r: any) => r.target).sort();
  93. // ship() → all three OrderShippedEvent listeners (param-typed, annotation-typed, transactional).
  94. expect(targets('ship')).toEqual(['afterShipped', 'audit', 'onShipped']);
  95. // cancel() → the ApplicationListener<X> form.
  96. expect(targets('cancel')).toEqual(['onApplicationEvent']);
  97. // Every shipped edge is keyed by the event type.
  98. expect(edges.filter((r: any) => r.source === 'ship').every((r: any) => r.via === 'OrderShippedEvent')).toBe(true);
  99. // PRECISION: UnheardEvent has no listener → no edge; the non-annotated helper is never a target.
  100. expect(edges.some((r: any) => r.via === 'UnheardEvent')).toBe(false);
  101. expect(edges.some((r: any) => r.target === 'helper')).toBe(false);
  102. cg.close?.();
  103. });
  104. it('produces no edges in a Spring app with no event bus (clean control)', async () => {
  105. write('shop/PlainService.java', `package shop;
  106. import org.springframework.stereotype.Service;
  107. @Service
  108. class PlainService {
  109. private final Repo repo;
  110. PlainService(Repo repo) { this.repo = repo; }
  111. String find(String id) { return repo.get(id); }
  112. }
  113. `);
  114. const cg = await CodeGraph.init(dir, { silent: true });
  115. await cg.indexAll();
  116. const db = (cg as any).db.db;
  117. const count = db
  118. .prepare(`SELECT count(*) c FROM edges WHERE json_extract(metadata,'$.synthesizedBy') = 'spring-event'`)
  119. .get();
  120. expect(count.c).toBe(0);
  121. cg.close?.();
  122. });
  123. });