mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-29 23:37:41 +00:00
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
* test(java): compare overload resolution with javac in CI * test(conformance): share call resolution reports across tools * test(java): cover the overload conversion matrix with javac (#4057) Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com> * test(java): run Java-backed fixtures in dedicated CI shards * test(java): replace handwritten resolution cases with javac comparisons * test(java): compare lambda and receiver fixtures against javac * fix(ci): restore Java test evidence and use locked toolchain commands * test(skills): fail incomplete CLI generation checks (#4056) * test(java): compare method references with javac * fix(java): resolve qualified bases and static wildcard overloads * test(java): preserve declared dollar names in compiler comparisons * fix(java): preserve distinct call targets for colliding type names * fix(conformance): resolve private nested bases and retain overload selections * fix(java): distinguish variadic overloads in the published graph * fix(java): match javac for nested types, imports and initializer calls * Move Java fixture testing out of Vitest and into compiler conformance * Verify Java graph endpoints and leave call selection to javac * Match javac for annotated bases and legal static imports * Resolve nested Java member types and clean up failed conformance fixtures * Preserve qualified Java bases across unrelated static wildcard imports * test(java): run inline source fixtures in compiler conformance * test(conformance): check fetch callers, lexical bindings and cleanup * test(java): move virtual source and text fixtures into conformance * fix(java): resolve imported member types and qualified base collisions * Match javac constructor overloads and preserve Java conformance coverage * Compare recovered Java inheritance with javac in the conformance runner * Run the Java query compilation smoke test in native conformance * Clarify Java conformance expectations and check populated suites * Write javac conformance records as UTF-8 on Windows * Compile the Java conformance tools for SonarCloud analysis * Compare Java method implementation edges with javac * Resolve fixture writers and inherited Java methods in their lexical context * Resolve qualified Java bases through their accessible type members * Match local qualified Java bases and bounded varargs identities to javac * Match Java call resolution to javac across the conformance corpus Preserve generic declaration identity, follow nested call results, and type lambda aliases and enhanced-loop variables in their declaration scopes. Report proven JDK call targets without creating workspace edges. Compare every selected source and binary call against javac and remove the twelve mismatch lists. * Compare Java recovery targets as sets and check empty destination addresses Keep repeated uses of the same declaration equivalent across graph producers while detecting missing and extra targets. Check raw destination addresses and document the source and classpath inputs to the compiler comparison. * Avoid extra native parent walks while capturing Java call facts Index lambda origins by spelling before walking lexical scopes, and only check value shadowing for qualifiers rooted in an identifier. This restores the capture traversal budget for anonymous classes while preserving javac call targets. * Refresh the Java capture fingerprint for corrected fixture comments The module captures include the revised comments and the following declarations move to earlier lines. Both emitters produce the same 4,288 capture groups when given the same fixture revision. * Preserve Java conformance assertions and use trusted fixture paths Use explicit canonical sort orders, require missing evidence to fail assertions, and keep pending query results in the replay cache. Resolve the Git executable independently of the test repository and isolate the Spring workspace per test. * Resolve inherited Java members through the scoped superclass * Reuse validated analyzer build hashes for parser cache identity * Resolve SonarCloud findings in Java resolution and compiler conformance * Align Java compiler tool packages and pin benchmark baselines to the tested merge * Honor wildcard method families and fixture ownership in Java conformance * fix(java): resolve inherited and annotation member types against javac Keep workspace links tied to import declarations and compare Java varargs overloads through comments. Make generated-project selection, method metadata checks and Java fixture ownership explicit in conformance. * fix(java): resolve bounded and imported bases during dispatch Keep the type selected from a qualified heritage clause available during overload resolution. Compare annotated bounds, inherited static imports, and String.valueOf arguments directly with javac. Detect Java fixture paths on either side of a filesystem rename. * Conform Java method visibility and wildcard lambda bounds to javac Compare method access at source positions, recognize CommonJS fixture reads, and preserve diagnostics when native conformance setup fails. * Match javac for qualified member overloads and selective call diagnostics * Track reassigned Java fixture paths at their point of use * Respect Java visibility and Node path semantics in conformance checks * Align Java graph benchmarks with compiler-verified visibility * Record the public Java record accessor in the mixed-provider benchmark * Address remaining Java conformance analysis findings Keep repository import scans sequential, simplify Java header and overload resolution, and reuse Spring conformance measurements. Preserve compiler comparison assertions and emitted fixture contents. * Match javac on nested binary names, null factories and recovered calls Preserve the qualified annotation target in workspace links. Compare valid and invalid arguments separately so an attributed recovery symbol cannot mask a failed invocation. --------- Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
919 lines
31 KiB
TypeScript
919 lines
31 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { describe, it } from 'node:test';
|
|
import type { SpringMessageProducerFact } from '../../../../../src/core/ingestion/frameworks/spring/message-producers.js';
|
|
import { collectJavaCaptureSideChannel } from '../../../../../src/core/ingestion/languages/java/capture-side-channel.js';
|
|
import { emitJavaScopeCaptures } from '../../../../../src/core/ingestion/languages/java/captures.js';
|
|
import { collectKotlinCaptureSideChannel } from '../../../../../src/core/ingestion/languages/kotlin/capture-side-channel.js';
|
|
import { emitKotlinScopeCaptures } from '../../../../../src/core/ingestion/languages/kotlin/captures.js';
|
|
|
|
const JAVA_FILE = 'src/OrderPublishers.java';
|
|
const KOTLIN_FILE = 'src/OrderPublishers.kt';
|
|
|
|
function javaProducers(code: string): readonly SpringMessageProducerFact[] {
|
|
emitJavaScopeCaptures(code, JAVA_FILE);
|
|
return collectJavaCaptureSideChannel(JAVA_FILE)?.springMessageProducerFacts ?? [];
|
|
}
|
|
|
|
function kotlinProducers(code: string): readonly SpringMessageProducerFact[] {
|
|
emitKotlinScopeCaptures(code, KOTLIN_FILE);
|
|
return collectKotlinCaptureSideChannel(KOTLIN_FILE)?.springMessageProducerFacts ?? [];
|
|
}
|
|
|
|
const signature = (fact: SpringMessageProducerFact) =>
|
|
`${fact.template} ${fact.receiverName}.${fact.methodName}`;
|
|
|
|
const destination = (fact: SpringMessageProducerFact) => fact.args?.[0]?.text;
|
|
|
|
describe('Java Spring messaging producers', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
import com.example.messaging.support.Destinations;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.kafka.core.KafkaTemplate;
|
|
|
|
public class OrderPublishers {
|
|
private final KafkaTemplate<String, String> kafkaTemplate;
|
|
|
|
@Value("\${app.messaging.orders-topic}")
|
|
private String ordersTopic;
|
|
|
|
public void literalDestination(String payload) {
|
|
kafkaTemplate.send("orders", payload);
|
|
}
|
|
|
|
public void constantDestination(String payload) {
|
|
this.kafkaTemplate.send(Destinations.ORDERS, payload);
|
|
}
|
|
|
|
public void configuredDestination(String payload) {
|
|
kafkaTemplate.send(ordersTopic, payload);
|
|
}
|
|
|
|
public void configuredDestinationInline(String payload, Environment environment) {
|
|
kafkaTemplate.send(environment.getProperty("app.messaging.orders-topic"), payload);
|
|
}
|
|
|
|
public void rabbitDestination(String exchange, String routingKey, String payload) {
|
|
rabbitTemplate.convertAndSend(exchange, routingKey, payload);
|
|
}
|
|
|
|
public void jmsDestination(String payload) {
|
|
jmsTemplate.convertAndSend("queue.orders", payload);
|
|
}
|
|
|
|
public void streamBridgeDestination(String payload) {
|
|
streamBridge.send(Destinations.SHIPMENTS_BINDING, payload);
|
|
}
|
|
}
|
|
`);
|
|
|
|
it('recognizes every supported template and method pair', () => {
|
|
assert.deepEqual(facts.map(signature), [
|
|
'kafka kafkaTemplate.send',
|
|
'kafka this.kafkaTemplate.send',
|
|
'kafka kafkaTemplate.send',
|
|
'kafka kafkaTemplate.send',
|
|
'rabbit rabbitTemplate.convertAndSend',
|
|
'jms jmsTemplate.convertAndSend',
|
|
'stream-bridge streamBridge.send',
|
|
]);
|
|
});
|
|
|
|
it('produces a fact for a literal, a constant, and a configuration key alike', () => {
|
|
assert.deepEqual(facts.slice(0, 4).map(destination), [
|
|
'"orders"',
|
|
'Destinations.ORDERS',
|
|
'ordersTopic',
|
|
'environment.getProperty("app.messaging.orders-topic")',
|
|
]);
|
|
});
|
|
|
|
it('captures every call argument positionally, since Java has no named ones', () => {
|
|
assert.deepEqual(facts[4]?.args, [
|
|
{ text: 'exchange' },
|
|
{ text: 'routingKey' },
|
|
{ text: 'payload' },
|
|
]);
|
|
assert.equal(
|
|
facts.flatMap((fact) => fact.args ?? []).every((arg) => !('name' in arg)),
|
|
true,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Java Spring messaging producer receivers', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class ReceiverShapes {
|
|
public void prefixedReceiver(String payload) {
|
|
orderKafkaTemplate.send("orders", payload);
|
|
}
|
|
|
|
public void deepReceiver(String payload) {
|
|
outer.inner.kafkaTemplate.send("orders", payload);
|
|
}
|
|
|
|
public void untypedReceiver(String payload) {
|
|
template.send("orders", payload);
|
|
}
|
|
|
|
public void mapReceiver(String payload) {
|
|
templates.get("orders").send("orders", payload);
|
|
}
|
|
|
|
public void factoryReceiver(String payload) {
|
|
getTemplate().send("orders", payload);
|
|
}
|
|
|
|
public void wrongMethodForKafka(String payload) {
|
|
kafkaTemplate.convertAndSend("orders", payload);
|
|
}
|
|
|
|
public void wrongMethodForRabbit(String payload) {
|
|
rabbitTemplate.send("orders", payload);
|
|
}
|
|
}
|
|
`);
|
|
|
|
it('matches a receiver whose simple name contains the template type name', () => {
|
|
assert.deepEqual(
|
|
facts.map((fact) => fact.receiverName),
|
|
['orderKafkaTemplate', 'outer.inner.kafkaTemplate'],
|
|
);
|
|
});
|
|
|
|
it('does not guess a broker for an untyped, indexed, or returned receiver', () => {
|
|
const receivers = facts.map((fact) => fact.receiverName);
|
|
assert.ok(!receivers.includes('template'));
|
|
assert.equal(
|
|
receivers.some((name) => name.includes('templates.get')),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
receivers.some((name) => name.includes('getTemplate')),
|
|
false,
|
|
);
|
|
});
|
|
|
|
it('requires the method that belongs to the template, not any send-like name', () => {
|
|
assert.deepEqual(
|
|
facts.map((fact) => fact.methodName),
|
|
['send', 'send'],
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Java Spring messaging producer owners', () => {
|
|
it('attributes a publish to its nearest enclosing callable exactly once', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
import java.util.List;
|
|
|
|
public class Owners {
|
|
public Owners(String payload) {
|
|
kafkaTemplate.send("constructor-orders", payload);
|
|
}
|
|
|
|
public void insideLambda(List<String> payloads) {
|
|
payloads.forEach(payload -> kafkaTemplate.send("lambda-orders", payload));
|
|
}
|
|
|
|
public void insideAnonymousClass() {
|
|
Runnable task = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
kafkaTemplate.send("anonymous-orders", "payload");
|
|
}
|
|
};
|
|
task.run();
|
|
}
|
|
|
|
static class Nested {
|
|
void nestedPublish(String payload) {
|
|
kafkaTemplate.send("nested-orders", payload);
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts.map(destination), [
|
|
'"constructor-orders"',
|
|
'"lambda-orders"',
|
|
'"anonymous-orders"',
|
|
'"nested-orders"',
|
|
]);
|
|
assert.equal(new Set(facts.map((fact) => fact.ownerScopeId)).size, 4);
|
|
// A lambda body belongs to the method that declares it, while an anonymous
|
|
// class body belongs to its own `run` method (line 17 starts at @Override).
|
|
assert.equal(facts[1]?.ownerRange.startLine, 11);
|
|
assert.equal(facts[2]?.ownerRange.startLine, 17);
|
|
});
|
|
|
|
it('drops a publish that has no callable owner', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class NoCallableOwner {
|
|
private static final Object WARMUP = kafkaTemplate.send("field-orders", "payload");
|
|
|
|
static {
|
|
kafkaTemplate.send("static-initializer-orders", "payload");
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts, []);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer capture regressions', () => {
|
|
it('leaves the side channel untouched for a file that publishes nothing', () => {
|
|
const sideChannel = (() => {
|
|
emitJavaScopeCaptures(
|
|
`
|
|
package com.example.messaging;
|
|
|
|
public class Quiet {
|
|
public void run() {
|
|
logger.send("orders", "payload");
|
|
}
|
|
}
|
|
`,
|
|
JAVA_FILE,
|
|
);
|
|
return collectJavaCaptureSideChannel(JAVA_FILE);
|
|
})();
|
|
assert.equal(sideChannel === undefined || !('springMessageProducerFacts' in sideChannel), true);
|
|
});
|
|
|
|
it('still captures programmatic bean lookups from the same member-call visit', () => {
|
|
emitJavaScopeCaptures(
|
|
`
|
|
package com.example.messaging;
|
|
|
|
public class Lookups {
|
|
public void run() {
|
|
kafkaTemplate.send("orders", "payload");
|
|
OrderService service = SpringContextUtil.getBean(OrderService.class);
|
|
service.handle();
|
|
}
|
|
}
|
|
`,
|
|
JAVA_FILE,
|
|
);
|
|
const sideChannel = collectJavaCaptureSideChannel(JAVA_FILE);
|
|
assert.equal(sideChannel?.springMessageProducerFacts?.length, 1);
|
|
assert.deepEqual(
|
|
sideChannel?.springDynamicLookupFacts?.map((fact) => fact.targetTypeName),
|
|
['OrderService'],
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer receiver spellings', () => {
|
|
it('captures the inner call of the synchronous send idiom', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Chained {
|
|
public void awaited(String payload) throws Exception {
|
|
kafkaTemplate.send("orders", payload).get();
|
|
}
|
|
|
|
public void withCallback(String payload) {
|
|
kafkaTemplate.send("orders", payload).addCallback(ok -> {}, error -> {});
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts.map(destination), ['"orders"', '"orders"']);
|
|
});
|
|
|
|
it('joins a receiver chain that the source wrapped across lines', () => {
|
|
const java = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Wrapped {
|
|
public void publish(String payload) {
|
|
outer
|
|
.inner
|
|
.kafkaTemplate.send("orders", payload);
|
|
}
|
|
}
|
|
`);
|
|
const kotlin = kotlinProducers(`
|
|
package com.example.messaging
|
|
|
|
class Wrapped {
|
|
fun publish(payload: String) {
|
|
outer
|
|
.inner
|
|
.kafkaTemplate.send("orders", payload)
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(
|
|
java.map((fact) => fact.receiverName),
|
|
['outer.inner.kafkaTemplate'],
|
|
);
|
|
assert.deepEqual(
|
|
kotlin.map((fact) => fact.receiverName),
|
|
['outer.inner.kafkaTemplate'],
|
|
);
|
|
});
|
|
|
|
it('leaves single-line spacing and nested literals in the receiver as written', () => {
|
|
// Joining a wrapped chain must not reach inside a string literal that the
|
|
// receiver expression happens to contain.
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Literals {
|
|
public void publish(String payload) {
|
|
registry.lookup("a . b").kafkaTemplate.send("orders . v1", payload);
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(
|
|
facts.map((fact) => fact.receiverName),
|
|
['registry.lookup("a . b").kafkaTemplate'],
|
|
);
|
|
assert.deepEqual(facts.map(destination), ['"orders . v1"']);
|
|
});
|
|
|
|
it('does not attribute a call that names no receiver', () => {
|
|
const java = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Bare {
|
|
public void publish(String payload) {
|
|
send("orders", payload);
|
|
}
|
|
}
|
|
`);
|
|
const kotlin = kotlinProducers(`
|
|
package com.example.messaging
|
|
|
|
class Bare {
|
|
fun publish(payload: String) {
|
|
send("orders", payload)
|
|
}
|
|
|
|
fun scoped(payload: String) {
|
|
with(kafkaTemplate) {
|
|
send("orders", payload)
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(java, []);
|
|
assert.deepEqual(kotlin, []);
|
|
});
|
|
|
|
it('does not attribute a cast or parenthesized receiver', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Casts {
|
|
public void publish(Object raw, String payload) {
|
|
((org.springframework.kafka.core.KafkaTemplate<String, String>) raw)
|
|
.send("orders", payload);
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts, []);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer side-channel transport', () => {
|
|
it('carries producer facts and handler arguments through a JSON round trip', () => {
|
|
// The worker ships the side channel to the main thread as JSON; a fact
|
|
// shape that does not survive that trip is invisible to every later phase.
|
|
emitJavaScopeCaptures(
|
|
`
|
|
package com.example.messaging;
|
|
|
|
import org.springframework.kafka.annotation.KafkaListener;
|
|
|
|
public class RoundTrip {
|
|
@KafkaListener(topics = "orders")
|
|
public void consume(String payload) {}
|
|
|
|
public void publish(String payload) {
|
|
kafkaTemplate.send("orders", payload);
|
|
}
|
|
}
|
|
`,
|
|
JAVA_FILE,
|
|
);
|
|
const collected = collectJavaCaptureSideChannel(JAVA_FILE);
|
|
const wire = JSON.stringify(collected);
|
|
const restored = JSON.parse(wire) as typeof collected;
|
|
assert.deepEqual(restored?.springMessageProducerFacts, collected?.springMessageProducerFacts);
|
|
assert.deepEqual(restored?.springMessageProducerFacts?.[0]?.args, [
|
|
{ text: '"orders"' },
|
|
{ text: 'payload' },
|
|
]);
|
|
assert.deepEqual(restored?.springNonHttpHandlerFacts?.[0]?.annotations[0]?.args, [
|
|
{ name: 'topics', text: '"orders"' },
|
|
]);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Kafka is the template whose destination shapes are covered above. The other
|
|
* three carry the same burden: a destination is written as a literal, as a
|
|
* constant that lives in another file, or as a name bound from configuration,
|
|
* and capture must produce a fact for all three without preferring any.
|
|
*/
|
|
describe('Spring messaging producer destination kinds per template', () => {
|
|
const JAVA_SOURCE = `
|
|
package com.example.messaging;
|
|
|
|
import com.example.messaging.support.Destinations;
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
import org.springframework.cloud.stream.function.StreamBridge;
|
|
import org.springframework.jms.core.JmsTemplate;
|
|
|
|
public class OrderPublishers {
|
|
private final RabbitTemplate rabbitTemplate;
|
|
private final JmsTemplate jmsTemplate;
|
|
private final StreamBridge streamBridge;
|
|
|
|
@Value("\${app.messaging.orders-exchange}")
|
|
private String ordersExchange;
|
|
|
|
public void rabbitLiteral(String payload) {
|
|
rabbitTemplate.convertAndSend("orders.exchange", "orders.key", payload);
|
|
}
|
|
|
|
public void rabbitConstant(String payload) {
|
|
rabbitTemplate.convertAndSend(Destinations.EXCHANGE, Destinations.ROUTING_KEY, payload);
|
|
}
|
|
|
|
public void rabbitConfigured(String payload) {
|
|
rabbitTemplate.convertAndSend(ordersExchange, "orders.key", payload);
|
|
}
|
|
|
|
public void jmsLiteral(String payload) {
|
|
jmsTemplate.convertAndSend("queue.orders", payload);
|
|
}
|
|
|
|
public void jmsConstant(String payload) {
|
|
jmsTemplate.convertAndSend(Destinations.QUEUE, payload);
|
|
}
|
|
|
|
public void jmsConfigured(String payload) {
|
|
jmsTemplate.convertAndSend(ordersQueue, payload);
|
|
}
|
|
|
|
public void bridgeLiteral(String payload) {
|
|
streamBridge.send("orders-out-0", payload);
|
|
}
|
|
|
|
public void bridgeConstant(String payload) {
|
|
streamBridge.send(Destinations.ORDERS_BINDING, payload);
|
|
}
|
|
|
|
public void bridgeConfigured(String payload) {
|
|
streamBridge.send(ordersBinding, payload);
|
|
}
|
|
|
|
public void notAPublish(String payload) {
|
|
rabbitTemplate.send("orders.exchange", payload);
|
|
jmsTemplate.send("queue.orders", payload);
|
|
}
|
|
}
|
|
`;
|
|
|
|
const KOTLIN_SOURCE = `
|
|
package com.example.messaging
|
|
|
|
import com.example.messaging.support.Destinations
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate
|
|
import org.springframework.cloud.stream.function.StreamBridge
|
|
import org.springframework.jms.core.JmsTemplate
|
|
|
|
class OrderPublishers(
|
|
private val rabbitTemplate: RabbitTemplate,
|
|
private val jmsTemplate: JmsTemplate,
|
|
private val streamBridge: StreamBridge,
|
|
@Value("\\\${app.messaging.orders-exchange}") private val ordersExchange: String,
|
|
) {
|
|
fun rabbitLiteral(payload: String) {
|
|
rabbitTemplate.convertAndSend("orders.exchange", "orders.key", payload)
|
|
}
|
|
|
|
fun rabbitConstant(payload: String) {
|
|
rabbitTemplate.convertAndSend(Destinations.EXCHANGE, Destinations.ROUTING_KEY, payload)
|
|
}
|
|
|
|
fun rabbitConfigured(payload: String) {
|
|
rabbitTemplate.convertAndSend(ordersExchange, "orders.key", payload)
|
|
}
|
|
|
|
fun jmsLiteral(payload: String) {
|
|
jmsTemplate.convertAndSend("queue.orders", payload)
|
|
}
|
|
|
|
fun jmsConstant(payload: String) {
|
|
jmsTemplate.convertAndSend(Destinations.QUEUE, payload)
|
|
}
|
|
|
|
fun jmsConfigured(payload: String) {
|
|
jmsTemplate.convertAndSend(ordersQueue, payload)
|
|
}
|
|
|
|
fun bridgeLiteral(payload: String) {
|
|
streamBridge.send("orders-out-0", payload)
|
|
}
|
|
|
|
fun bridgeConstant(payload: String) {
|
|
streamBridge.send(Destinations.ORDERS_BINDING, payload)
|
|
}
|
|
|
|
fun bridgeConfigured(payload: String) {
|
|
streamBridge.send(ordersBinding, payload)
|
|
}
|
|
|
|
fun notAPublish(payload: String) {
|
|
rabbitTemplate.send("orders.exchange", payload)
|
|
jmsTemplate.send("queue.orders", payload)
|
|
}
|
|
}
|
|
`;
|
|
|
|
const EXPECTED_SIGNATURES = [
|
|
'rabbit rabbitTemplate.convertAndSend',
|
|
'rabbit rabbitTemplate.convertAndSend',
|
|
'rabbit rabbitTemplate.convertAndSend',
|
|
'jms jmsTemplate.convertAndSend',
|
|
'jms jmsTemplate.convertAndSend',
|
|
'jms jmsTemplate.convertAndSend',
|
|
'stream-bridge streamBridge.send',
|
|
'stream-bridge streamBridge.send',
|
|
'stream-bridge streamBridge.send',
|
|
];
|
|
|
|
const EXPECTED_DESTINATIONS = [
|
|
'"orders.exchange"',
|
|
'Destinations.EXCHANGE',
|
|
'ordersExchange',
|
|
'"queue.orders"',
|
|
'Destinations.QUEUE',
|
|
'ordersQueue',
|
|
'"orders-out-0"',
|
|
'Destinations.ORDERS_BINDING',
|
|
'ordersBinding',
|
|
];
|
|
|
|
it('gives Java rabbit, jms, and stream-bridge a fact for all three shapes', () => {
|
|
const facts = javaProducers(JAVA_SOURCE);
|
|
assert.deepEqual(facts.map(signature), EXPECTED_SIGNATURES);
|
|
assert.deepEqual(facts.map(destination), EXPECTED_DESTINATIONS);
|
|
});
|
|
|
|
it('leaves a send that does not belong to its template unrecognized', () => {
|
|
// `notAPublish` is the last method in both fixtures; the signature lists
|
|
// above end at the stream bridge, so `RabbitTemplate.send` and
|
|
// `JmsTemplate.send` produced nothing.
|
|
const receivers = new Set(
|
|
[...javaProducers(JAVA_SOURCE), ...kotlinProducers(KOTLIN_SOURCE)].map(
|
|
(fact) => `${fact.receiverName}.${fact.methodName}`,
|
|
),
|
|
);
|
|
assert.ok(!receivers.has('rabbitTemplate.send'));
|
|
assert.ok(!receivers.has('jmsTemplate.send'));
|
|
});
|
|
|
|
it('resolves no destination while capturing it', () => {
|
|
const texts = [...javaProducers(JAVA_SOURCE), ...kotlinProducers(KOTLIN_SOURCE)].flatMap(
|
|
(fact) => fact.args ?? [],
|
|
);
|
|
// A resolver would have turned the constants and the injected name into
|
|
// addresses; capture must still be looking at the source spelling.
|
|
assert.equal(
|
|
texts.some((argument) => argument.text === 'Destinations.QUEUE'),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
texts.some((argument) => argument.text === 'ordersExchange'),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
texts.some((argument) => argument.text.includes('${app.messaging')),
|
|
false,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer receiver decorations', () => {
|
|
// Template beans are declared under every naming convention a Java or Kotlin
|
|
// codebase uses: a qualifying prefix, a qualifying suffix, a version or index
|
|
// tail, and the constant spelling that `static final` fields take. A rule that
|
|
// only accepted a decorating PREFIX silently dropped the rest, which are the
|
|
// publishes this capture exists to find.
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class DecoratedTemplates {
|
|
public void suffixed(String payload) { kafkaTemplateDlq.send("a", payload); }
|
|
public void constantCase(String payload) { KAFKA_TEMPLATE.send("b", payload); }
|
|
public void snakeCase(String payload) { kafka_template.send("c", payload); }
|
|
public void versioned(String payload) { kafkaTemplateV2.send("d", payload); }
|
|
public void indexed(String payload) { kafkaTemplate2.send("e", payload); }
|
|
public void constantBridge(String payload) { STREAM_BRIDGE.send("f", payload); }
|
|
public void indexedBridge(String payload) { streamBridge2.send("g", payload); }
|
|
public void indexedRabbit(String key, String payload) {
|
|
rabbitTemplate1.convertAndSend(key, payload);
|
|
}
|
|
public void prefixed(String payload) { orderKafkaTemplate.send("h", payload); }
|
|
public void plain(String payload) { kafkaTemplate.send("i", payload); }
|
|
}
|
|
`);
|
|
|
|
it('recognizes a template decorated by prefix, suffix, index, or constant case', () => {
|
|
assert.deepEqual(facts.map(signature), [
|
|
'kafka kafkaTemplateDlq.send',
|
|
'kafka KAFKA_TEMPLATE.send',
|
|
'kafka kafka_template.send',
|
|
'kafka kafkaTemplateV2.send',
|
|
'kafka kafkaTemplate2.send',
|
|
'stream-bridge STREAM_BRIDGE.send',
|
|
'stream-bridge streamBridge2.send',
|
|
'rabbit rabbitTemplate1.convertAndSend',
|
|
'kafka orderKafkaTemplate.send',
|
|
'kafka kafkaTemplate.send',
|
|
]);
|
|
});
|
|
|
|
it('still refuses a receiver that only a type could make a template', () => {
|
|
// Widening the name match must not reach any of these: an undecorated
|
|
// `template` would attribute every `send` in the repository to Kafka, and
|
|
// the rest are not names at all. `config.get("a.kafkaTemplate")` is the
|
|
// sharp one — splitting on the last dot lands inside the string literal.
|
|
assert.deepEqual(
|
|
javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class NotTemplates {
|
|
public void bare(String payload) { template.send("t", payload); }
|
|
public void lookup(String payload) { templates.get("k").send("t", payload); }
|
|
public void factory(String payload) { getTemplate().send("t", payload); }
|
|
public void indexed(String payload) { templates["k"].send("t", payload); }
|
|
public void configured(String payload) {
|
|
config.get("a.kafkaTemplate").send("t", payload);
|
|
}
|
|
public void unrelated(String payload) { mailer.send("t", payload); }
|
|
}
|
|
`),
|
|
[],
|
|
);
|
|
});
|
|
|
|
it('names no broker for a receiver that matches two templates at once', () => {
|
|
// Two signatures share `send` and two share `convertAndSend`, so a
|
|
// substring match lets one receiver name satisfy both. The receiver's type
|
|
// is never resolved, so nothing ranks one match over the other, and the
|
|
// first-listed signature would be published as a definite attribution.
|
|
assert.deepEqual(
|
|
javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class AmbiguousTemplates {
|
|
public void bothSend(String payload) {
|
|
streamBridgeKafkaTemplate.send("a", payload);
|
|
}
|
|
public void bothSendReversed(String payload) {
|
|
kafkaTemplateStreamBridge.send("b", payload);
|
|
}
|
|
public void bothConvertAndSend(String key, String payload) {
|
|
rabbitTemplateJmsTemplate.convertAndSend(key, payload);
|
|
}
|
|
}
|
|
`),
|
|
[],
|
|
);
|
|
});
|
|
|
|
it('names the broker when only one template matches the receiver', () => {
|
|
// The counterpart of the rule above: withholding applies to a genuinely
|
|
// ambiguous name, not to a decorated one that happens to be long. Neither
|
|
// receiver here contains a second template type name.
|
|
assert.deepEqual(
|
|
javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class UnambiguousTemplates {
|
|
public void bridged(String payload) { orderStreamBridge.send("a", payload); }
|
|
public void kafka(String payload) { streamingKafkaTemplate.send("b", payload); }
|
|
}
|
|
`).map(signature),
|
|
['stream-bridge orderStreamBridge.send', 'kafka streamingKafkaTemplate.send'],
|
|
);
|
|
});
|
|
|
|
it('refuses a receiver whose last segment is not a bare identifier', () => {
|
|
// The identifier gate, not the name match, is what rejects this: strip it
|
|
// and `/*c*/kafkaTemplate` matches the type name and the publish is
|
|
// attributed to a receiver spelling that includes a comment.
|
|
assert.deepEqual(
|
|
javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Commented {
|
|
public void commented(String payload) {
|
|
this./*which*/kafkaTemplate.send("orders", payload);
|
|
}
|
|
}
|
|
`),
|
|
[],
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer error recovery', () => {
|
|
it('produces no Java fact when the argument list did not parse', () => {
|
|
// Recovery keeps the tree well formed while inventing what it contains: the
|
|
// unterminated call below absorbs the next method's source and offers it as
|
|
// an argument. A fact whose whole purpose is to name a destination must not
|
|
// report one that was never written.
|
|
assert.deepEqual(
|
|
javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Unfinished {
|
|
public void publish(String payload) {
|
|
kafkaTemplate.send(ORDERS_TOPIC,
|
|
}
|
|
|
|
public void other(String value) {
|
|
System.out.println(value);
|
|
}
|
|
}
|
|
`),
|
|
[],
|
|
);
|
|
});
|
|
|
|
it('still captures a well-formed publish in a file that fails to parse elsewhere', () => {
|
|
// Failing closed is scoped to the broken call, not to the file: a watcher
|
|
// reparse mid-edit must not blank out the publishes that are still intact.
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class PartlyBroken {
|
|
public void good(String payload) {
|
|
kafkaTemplate.send("orders", payload);
|
|
}
|
|
|
|
public void broken(String payload) {
|
|
kafkaTemplate.send(ORDERS_TOPIC,
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts.map(destination), ['"orders"']);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer argument spellings', () => {
|
|
it('gives one Java argument one spelling however the source wrapped it', () => {
|
|
// The receiver already normalized its wraps; leaving the argument raw made
|
|
// the same constant compare unequal to itself, because the text carries the
|
|
// ENCLOSING block's indentation and so changes with nesting depth.
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class WrappedArguments {
|
|
public void single(String payload) {
|
|
kafkaTemplate.send(Destinations.ORDERS, payload);
|
|
}
|
|
|
|
public void wrapped(String payload) {
|
|
kafkaTemplate.send(Destinations
|
|
.ORDERS, payload);
|
|
}
|
|
|
|
public void wrappedDeeper(String payload) {
|
|
if (payload != null) {
|
|
kafkaTemplate.send(Destinations
|
|
.ORDERS, payload);
|
|
}
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts.map(destination), [
|
|
'Destinations.ORDERS',
|
|
'Destinations.ORDERS',
|
|
'Destinations.ORDERS',
|
|
]);
|
|
});
|
|
|
|
it('keeps the newlines inside a multi-line string literal argument', () => {
|
|
// The wrap rule may not reach inside a literal: a Java text block or Kotlin
|
|
// raw string whose newline sits next to a dot is a different VALUE once the
|
|
// newline is removed.
|
|
const java = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class LiteralArguments {
|
|
public void publish(String payload) {
|
|
kafkaTemplate.send("""
|
|
line-a
|
|
.line-b""", payload);
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(java.map(destination), ['"""\nline-a\n.line-b"""']);
|
|
});
|
|
|
|
it('keeps the newlines inside a multi-line literal nested in the receiver', () => {
|
|
// The single-line form of this is already pinned above; the doc comment on
|
|
// the normalizer promised the same for nested literals, and only the
|
|
// single-line case delivered it.
|
|
const java = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class LiteralReceiver {
|
|
public void publish(String payload) {
|
|
registry.get("""
|
|
line-a
|
|
.line-b""").kafkaTemplate.send("orders", payload);
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(
|
|
java.map((fact) => fact.receiverName),
|
|
['registry.get("""\nline-a\n.line-b""").kafkaTemplate'],
|
|
);
|
|
});
|
|
|
|
it('drops a comment between Java arguments without shifting their positions', () => {
|
|
// Comments are named children of a Java argument list, so an unfiltered
|
|
// read reports three arguments for a two-argument call and moves the
|
|
// payload into the destination slot for anything reading by position.
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class CommentedArguments {
|
|
public void publish(String payload) {
|
|
kafkaTemplate.send(/* why */ "orders", payload);
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(
|
|
facts.map((fact) => fact.args),
|
|
[[{ text: '"orders"' }, { text: 'payload' }]],
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('Spring messaging producer owner boundaries', () => {
|
|
it('does not attribute a Java publish across a nested type body', () => {
|
|
// A publish in the field initializer of a class declared inside a method is
|
|
// run when that class is instantiated, which the method may never do. The
|
|
// same construct at the top level of a class already yields no fact, and
|
|
// the rule has to read the same at both depths.
|
|
assert.deepEqual(
|
|
javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Nested {
|
|
public void outer(String payload) {
|
|
class Local {
|
|
private final Object sent = kafkaTemplate.send("local-field", payload);
|
|
}
|
|
}
|
|
|
|
public void anonymous(String payload) {
|
|
Runnable task = new Runnable() {
|
|
private final Object sent = kafkaTemplate.send("anon-field", payload);
|
|
public void run() {}
|
|
};
|
|
}
|
|
}
|
|
`),
|
|
[],
|
|
);
|
|
});
|
|
|
|
it('still attributes a Java publish to a method of a nested type', () => {
|
|
const facts = javaProducers(`
|
|
package com.example.messaging;
|
|
|
|
public class Nested {
|
|
public void anonymous(String payload) {
|
|
Runnable task = new Runnable() {
|
|
public void run() { kafkaTemplate.send("anon-method", payload); }
|
|
};
|
|
}
|
|
|
|
class Inner {
|
|
void publish(String payload) { kafkaTemplate.send("inner-method", payload); }
|
|
}
|
|
}
|
|
`);
|
|
assert.deepEqual(facts.map(destination), ['"anon-method"', '"inner-method"']);
|
|
});
|
|
});
|