Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

90 lines
2.1 KiB
Go

package languages
import (
"testing"
"github.com/zzet/gortex/internal/graph"
)
func springListenerNode(nodes []*graph.Node, methodName string) *graph.Node {
for _, n := range nodes {
if n.Name == methodName && n.Meta != nil && n.Meta["spring_listener_type"] != nil {
return n
}
}
return nil
}
func springPublishPlaceholder(edges []*graph.Edge, evType string) *graph.Edge {
for _, e := range edges {
if e.Meta == nil {
continue
}
if v, _ := e.Meta["via"].(string); v != "spring-event" {
continue
}
if t, _ := e.Meta["spring_event_type"].(string); t == evType {
return e
}
}
return nil
}
func TestSpringEvents_TagsListenersAndPublishers(t *testing.T) {
src := `package com.x;
class OrderPlaced {}
class OrderService {
private ApplicationEventPublisher publisher;
void place(int id) {
publisher.publishEvent(new OrderPlaced(id));
}
}
class MailListener {
@EventListener
void on(OrderPlaced e) {}
}
class AuditListener implements ApplicationListener<OrderPlaced> {
public void onApplicationEvent(OrderPlaced e) {}
}
`
res, err := NewJavaExtractor().Extract("App.java", []byte(src))
if err != nil {
t.Fatal(err)
}
on := springListenerNode(res.Nodes, "on")
if on == nil {
t.Fatalf("@EventListener method not tagged")
}
if ty, _ := on.Meta["spring_listener_type"].(string); ty != "OrderPlaced" {
t.Errorf("listener type = %q (want OrderPlaced)", ty)
}
if springListenerNode(res.Nodes, "onApplicationEvent") == nil {
t.Errorf("ApplicationListener.onApplicationEvent not tagged")
}
ph := springPublishPlaceholder(res.Edges, "OrderPlaced")
if ph == nil {
t.Fatalf("no publishEvent placeholder")
}
if ph.From != "App.java::OrderService.place" {
t.Errorf("placeholder From = %q (want App.java::OrderService.place)", ph.From)
}
}
func TestSpringEvents_NonEventListenerNotTagged(t *testing.T) {
src := `package com.x;
class Svc {
@Override
void on(String e) {}
}
`
res, err := NewJavaExtractor().Extract("Svc.java", []byte(src))
if err != nil {
t.Fatal(err)
}
if springListenerNode(res.Nodes, "on") != nil {
t.Errorf("a non-@EventListener method must not be tagged")
}
}