Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"""Unit tests for Spring Boot framework edges (F1)."""
from __future__ import annotations
from datetime import datetime
from pathlib import Path
import networkx as nx
from repowise.core.ingestion.framework_edges import add_framework_edges
from repowise.core.ingestion.models import FileInfo, ParsedFile
from repowise.core.ingestion.parser import ASTParser
from repowise.core.ingestion.resolvers.context import ResolverContext
def _file_info(rel: str, abs_path: str, language: str) -> FileInfo:
return FileInfo(
path=rel,
abs_path=abs_path,
language=language,
size_bytes=100,
git_hash="",
last_modified=datetime.now(),
is_test=False,
is_config=False,
is_api_contract=False,
is_entry_point=False,
)
def _build_parsed(repo: Path) -> dict[str, ParsedFile]:
parser = ASTParser()
out: dict[str, ParsedFile] = {}
for src in list(repo.rglob("*.java")) + list(repo.rglob("*.kt")):
rel = src.resolve().relative_to(repo.resolve()).as_posix()
lang = "java" if src.suffix == ".java" else "kotlin"
fi = _file_info(rel, str(src.resolve()), lang)
out[rel] = parser.parse_file(fi, src.read_bytes())
return out
def _ctx(repo: Path, parsed: dict[str, ParsedFile]) -> ResolverContext:
path_set = set(parsed.keys())
stem_map: dict[str, list[str]] = {}
for p in path_set:
stem = Path(p).stem.lower()
stem_map.setdefault(stem, []).append(p)
return ResolverContext(
path_set=path_set, stem_map=stem_map, graph=nx.DiGraph(), repo_path=repo
)
class TestSpringDI:
def test_autowired_field_injection(self, tmp_path: Path) -> None:
(tmp_path / "UserService.java").write_text(
"import org.springframework.stereotype.Service;\n"
"@Service\npublic class UserService {}\n"
)
(tmp_path / "UserController.java").write_text(
"import org.springframework.beans.factory.annotation.Autowired;\n"
"import org.springframework.web.bind.annotation.RestController;\n"
"@RestController\npublic class UserController {\n"
" @Autowired private UserService userService;\n"
"}\n"
)
parsed = _build_parsed(tmp_path)
graph = nx.DiGraph()
for p in parsed:
graph.add_node(p)
ctx = _ctx(tmp_path, parsed)
add_framework_edges(graph, parsed, ctx, tech_stack=["spring"])
assert graph.has_edge("UserController.java", "UserService.java")
def test_constructor_injection_resolves_interface_to_impl(self, tmp_path: Path) -> None:
(tmp_path / "PaymentService.java").write_text(
"public interface PaymentService {}\n"
)
(tmp_path / "StripePayment.java").write_text(
"import org.springframework.stereotype.Service;\n"
"@Service\npublic class StripePayment implements PaymentService {}\n"
)
(tmp_path / "CheckoutController.java").write_text(
"import org.springframework.web.bind.annotation.RestController;\n"
"@RestController\npublic class CheckoutController {\n"
" private final PaymentService payments;\n"
" public CheckoutController(PaymentService payments) { this.payments = payments; }\n"
"}\n"
)
parsed = _build_parsed(tmp_path)
graph = nx.DiGraph()
for p in parsed:
graph.add_node(p)
ctx = _ctx(tmp_path, parsed)
add_framework_edges(graph, parsed, ctx, tech_stack=["spring"])
# Edge to either the interface file or its impl is acceptable.
assert graph.has_edge("CheckoutController.java", "StripePayment.java") or graph.has_edge(
"CheckoutController.java", "PaymentService.java"
)
def test_bean_factory_links_config_to_return_type(self, tmp_path: Path) -> None:
(tmp_path / "Greeter.java").write_text("public class Greeter {}\n")
(tmp_path / "AppConfig.java").write_text(
"import org.springframework.context.annotation.Bean;\n"
"import org.springframework.context.annotation.Configuration;\n"
"@Configuration\npublic class AppConfig {\n"
" @Bean\n public Greeter greeter() { return new Greeter(); }\n"
"}\n"
)
parsed = _build_parsed(tmp_path)
graph = nx.DiGraph()
for p in parsed:
graph.add_node(p)
ctx = _ctx(tmp_path, parsed)
add_framework_edges(graph, parsed, ctx, tech_stack=["spring"])
assert graph.has_edge("AppConfig.java", "Greeter.java")
class TestSpringKotlin:
def test_kotlin_service_with_autowired_field(self, tmp_path: Path) -> None:
(tmp_path / "Repo.kt").write_text(
"import org.springframework.stereotype.Repository\n"
"@Repository\nclass Repo\n"
)
(tmp_path / "Svc.kt").write_text(
"import org.springframework.beans.factory.annotation.Autowired\n"
"import org.springframework.stereotype.Service\n"
"@Service\nclass Svc {\n"
" @Autowired lateinit var repo: Repo\n"
"}\n"
)
parsed = _build_parsed(tmp_path)
graph = nx.DiGraph()
for p in parsed:
graph.add_node(p)
ctx = _ctx(tmp_path, parsed)
add_framework_edges(graph, parsed, ctx, tech_stack=["spring"])
# The Java-style regex won't match Kotlin `@Autowired lateinit var`, so this
# currently exercises only the gate (Spring detection works for Kotlin).
# The edge may or may not be present — assert the slice runs without error.
assert graph.number_of_nodes() == 2
class TestServletInitializer:
"""A class the servlet container bootstraps is a root; no source names it."""
def _graph(self, tmp_path: Path, files: dict[str, str]) -> nx.DiGraph:
for rel, text in files.items():
(tmp_path / rel).write_text(text)
parsed = _build_parsed(tmp_path)
graph = nx.DiGraph()
for p in parsed:
graph.add_node(p)
add_framework_edges(graph, parsed, _ctx(tmp_path, parsed), tech_stack=["spring"])
return graph
def test_spring_boot_servlet_initializer_is_an_entry_point(self, tmp_path: Path) -> None:
graph = self._graph(
tmp_path,
{
"ServletInitializer.java": (
"import org.springframework.boot.builder.SpringApplicationBuilder;\n"
"import org.springframework.boot.web.servlet.support"
".SpringBootServletInitializer;\n"
"public class ServletInitializer extends SpringBootServletInitializer {\n"
" @Override\n"
" protected SpringApplicationBuilder configure("
"SpringApplicationBuilder application) {\n"
" return application.sources(AdminApplication.class);\n"
" }\n"
"}\n"
),
},
)
node = graph.nodes["ServletInitializer.java"]
assert node.get("is_entry_point") is True
assert node.get("framework_role") == "spring_servlet_initializer"
def test_mvc_dispatcher_initializer_is_an_entry_point(self, tmp_path: Path) -> None:
graph = self._graph(
tmp_path,
{
"WebInit.java": (
"import org.springframework.web.servlet.support"
".AbstractAnnotationConfigDispatcherServletInitializer;\n"
"public class WebInit\n"
" extends AbstractAnnotationConfigDispatcherServletInitializer {}\n"
),
},
)
assert graph.nodes["WebInit.java"].get("is_entry_point") is True
def test_kotlin_servlet_initializer_is_an_entry_point(self, tmp_path: Path) -> None:
graph = self._graph(
tmp_path,
{
"ServletInitializer.kt": (
"import org.springframework.boot.web.servlet.support"
".SpringBootServletInitializer\n"
"class ServletInitializer : SpringBootServletInitializer()\n"
),
},
)
assert graph.nodes["ServletInitializer.kt"].get("is_entry_point") is True
def test_another_base_is_not_an_entry_point(self, tmp_path: Path) -> None:
graph = self._graph(
tmp_path,
{
"Widget.java": (
"import org.springframework.stereotype.Component;\n"
"public class Widget extends BaseWidget {}\n"
),
},
)
assert not graph.nodes["Widget.java"].get("is_entry_point")
class TestSpringGate:
def test_non_spring_unaffected(self, tmp_path: Path) -> None:
(tmp_path / "Plain.java").write_text("public class Plain {}\n")
parsed = _build_parsed(tmp_path)
graph = nx.DiGraph()
for p in parsed:
graph.add_node(p)
ctx = _ctx(tmp_path, parsed)
count = add_framework_edges(graph, parsed, ctx, tech_stack=[])
assert count == 0
def _build_graph(repo: Path):
"""A real graph, so the symbol-level edges have symbol nodes to land on."""
from repowise.core.ingestion import GraphBuilder
parsed = _build_parsed(repo)
builder = GraphBuilder(repo)
for pf in parsed.values():
builder.add_file(pf)
graph = builder.build()
add_framework_edges(graph, parsed, _ctx(repo, parsed), tech_stack=["spring"])
return graph
def _bound(graph) -> set[tuple[str, str]]:
return {
(s, t)
for s, t, d in graph.edges(data=True)
if d.get("edge_type") == "framework_binds"
}
class TestSpringSymbolEdges:
def test_injection_links_the_two_declarations(self, tmp_path: Path) -> None:
(tmp_path / "UserService.java").write_text(
"import org.springframework.stereotype.Service;\n"
"@Service\npublic class UserService {}\n"
)
(tmp_path / "UserController.java").write_text(
"import org.springframework.beans.factory.annotation.Autowired;\n"
"import org.springframework.web.bind.annotation.RestController;\n"
"@RestController\npublic class UserController {\n"
" @Autowired private UserService userService;\n"
"}\n"
)
bound = _bound(_build_graph(tmp_path))
assert (
"UserController.java::UserController",
"UserService.java::UserService",
) in bound
def test_a_nested_configuration_owns_its_own_injection(self, tmp_path: Path) -> None:
# The injection regexes read whole-file text. Attributing every hit to
# the type the file is named for gives the outer class an injection it
# does not declare, and nested @Configuration classes are a standard
# idiom.
(tmp_path / "PaymentGateway.java").write_text(
"import org.springframework.stereotype.Service;\n"
"@Service\npublic class PaymentGateway {}\n"
)
(tmp_path / "AppConfig.java").write_text(
"import org.springframework.context.annotation.Configuration;\n"
"import org.springframework.beans.factory.annotation.Autowired;\n"
"@Configuration\npublic class AppConfig {\n"
" @Configuration\n"
" static class Inner {\n"
" @Autowired private PaymentGateway gateway;\n"
" }\n"
"}\n"
)
bound = _bound(_build_graph(tmp_path))
target = "PaymentGateway.java::PaymentGateway"
assert ("AppConfig.java::AppConfig", target) not in bound
assert any(src.startswith("AppConfig.java::") for src, dst in bound if dst == target)
def test_an_ambiguous_type_name_stays_unclaimed(self, tmp_path: Path) -> None:
# Two files declaring `UserService` cannot settle which one is injected,
# and picking whichever was walked first is a coin flip.
for sub in ("a", "b"):
d = tmp_path / sub
d.mkdir()
(d / "UserService.java").write_text(
"import org.springframework.stereotype.Service;\n"
"@Service\npublic class UserService {}\n"
)
(tmp_path / "UserController.java").write_text(
"import org.springframework.beans.factory.annotation.Autowired;\n"
"import org.springframework.web.bind.annotation.RestController;\n"
"@RestController\npublic class UserController {\n"
" @Autowired private UserService userService;\n"
"}\n"
)
bound = _bound(_build_graph(tmp_path))
assert not [d for _s, d in bound if d.endswith("::UserService")]
def test_springs_value_annotation_is_not_lomboks(self, tmp_path: Path) -> None:
# Lombok's @Value makes a class immutable and takes no argument;
# Spring's takes a property expression. Reading one as the other turns
# every field in the class into a constructor parameter.
(tmp_path / "MailService.java").write_text(
"import org.springframework.stereotype.Service;\n"
"@Service\npublic class MailService {}\n"
)
(tmp_path / "User.java").write_text("public class User {}\n")
(tmp_path / "UserResource.java").write_text(
"import org.springframework.web.bind.annotation.RestController;\n"
"import org.springframework.beans.factory.annotation.Value;\n"
"@RestController\npublic class UserResource {\n"
' @Value("${app.name}")\n'
" private String applicationName;\n"
" private final User cachedUser = null;\n"
" public UserResource(MailService mailService) {}\n"
"}\n"
)
bound = _bound(_build_graph(tmp_path))
assert ("UserResource.java::UserResource", "MailService.java::MailService") in bound
assert ("UserResource.java::UserResource", "User.java::User") not in bound
class TestSpringRouteHandlers:
def test_each_mapped_method_is_bound_from_its_module(self, tmp_path: Path) -> None:
(tmp_path / "OwnerController.java").write_text(
"import org.springframework.web.bind.annotation.*;\n"
"@RestController\n"
'@RequestMapping("/owners")\n'
"public class OwnerController {\n"
" @GetMapping\n"
" public String list() { return \"\"; }\n"
' @PostMapping(value = {"/new", "/create"})\n'
' @PreAuthorize("hasRole(\'ADMIN\')")\n'
" public String create(@RequestBody String body) { return body; }\n"
' @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)\n'
" public void delete() {}\n"
" private void helper() {}\n"
"}\n"
)
bound = _bound(_build_graph(tmp_path))
module = "OwnerController.java::__module__"
handlers = {t.rsplit("::", 1)[-1] for s, t in bound if s == module}
assert handlers == {"list", "create", "delete"}
def test_a_kotlin_handler_and_a_commented_mapping(self, tmp_path: Path) -> None:
(tmp_path / "PetController.kt").write_text(
"import org.springframework.web.bind.annotation.*\n"
"@RestController\n"
"class PetController {\n"
' // @GetMapping("/old")\n'
" fun old(): String = \"\"\n"
' @GetMapping(value = ["/pets"])\n'
" fun pets(): String = \"\"\n"
"}\n"
)
bound = _bound(_build_graph(tmp_path))
assert bound == {("PetController.kt::__module__", "PetController.kt::PetController::pets")}
def test_a_one_line_kotlin_expression_body(self, tmp_path: Path) -> None:
(tmp_path / "Ping.kt").write_text(
"import org.springframework.web.bind.annotation.*\n"
"@RestController\n"
"class Ping {\n"
' @GetMapping("/ping") fun ping() = "pong"\n'
' @GetMapping("/pong")\n'
' fun pong() = "ping"\n'
"}\n"
)
bound = _bound(_build_graph(tmp_path))
assert bound == {
("Ping.kt::__module__", "Ping.kt::Ping::ping"),
("Ping.kt::__module__", "Ping.kt::Ping::pong"),
}
def test_a_feign_client_serves_nothing(self, tmp_path: Path) -> None:
(tmp_path / "Owners.java").write_text(
"import org.springframework.web.bind.annotation.GetMapping;\n"
'@FeignClient("owners")\n'
'public interface Owners { @GetMapping("/owners") String all(); }\n'
)
assert not _bound(_build_graph(tmp_path))