Commit Graph

19 Commits

Author SHA1 Message Date
Sam Brannen 279f55fdfa Polish RuntimeHintsAgentCondition 2022-07-03 18:57:21 +02:00
Sam Brannen adb2eef749 Clean up warnings and remove unused code 2022-07-03 18:56:47 +02:00
Brian Clozel a27104a485 Polish
See gh-27981
2022-07-01 20:28:32 +02:00
Brian Clozel 444a9bd011 Add testing infrastructure for RuntimeHintsAgent
This commit adds the supporting testing infrastructure using the
`RuntimeHintsAgent`. Given that the agent is loaded by the JVM running
the test suite, we can then use it to record method invocations at
runtime and check whether the prepared `RuntimeHints` match the expected
behavior.

This commit contributes the `RuntimeHintsRecorder`. With this, we can
record relevant method invocations for a given lambda, focusing on a
specific part of the code behavior. This returns a
`RuntimeHintsInvocations` instance, which is an AssertJ assert provider.
From there, we can perform assertions on the recorded invocations and
check that a given collection of hints cover the reflection, resources
and proxies needs at runtime.

This also ships the `@EnabledIfRuntimeHintsAgent` opinionated
annotation: this applies the `RuntimeHintsAgentCondition` JUnit
extension that detects whether the `RuntimeHintsAgent` is loaded by the
current JVM. Tests annotated with this will be skipped if the agent is
not present. This annotation is also tagged with a JUnit `@Tag` to
gather such tests in a specific `"RuntimeHintsTests"` test suite.

In the Spring Framework build, we have chosen to isolate such tests and
not load the agent for the main test suite ("RuntimeHintsTests" tests
are excluded from the main suite). While the agent's intent is to be as
transparent as possible, there are security and access considerations
that could interefere with other tests.
With this approach, we can then create a separate test suite and run
agent tests in a dedicated JVM.

Note that projects using this infrastructure can choose to use the
condition by itself in a custom annotation.

Here is an example of this testing infrastructure:

```
@EnabledIfRuntimeHintsAgent
class MyTestCases {

  @Test
  void hintsForMethodsReflectionShouldMatch() {
      RuntimeHints hints = new RuntimeHints();
      hints.reflection().registerType(String.class,
          hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));

      RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> {
          Method[] methods = String.class.getMethods();
      });
      assertThat(invocations).match(hints);
  }

}
```

See gh-27981
2022-06-30 18:20:22 +02:00
Brian Clozel c86e678788 Add RuntimeHintsAgent Java agent
With the introduction of `RuntimeHints`, we can now contribute
reflection, resources and proxies hints that describe the expected
runtime behavior of the application. While this can be verified at
runtime with smoke tests, managing such tests and compiling to native
there is not very efficient.

This commit introduces the new `RuntimeHintsAgent`, a Java agent that
instruments JDK methods related to `RuntimeHints`.
It is different from the GraalVM agent, which aims at collecting all the
required hints for the runtime behavior of an application and dump those
in the expected format.
Here, the `RuntimeHintsAgent` can collect the related invocations only
for a delimited scope (typically, a lambda within a test) and later
check those against a `RuntimeHints` instance. In the case of testing
`RuntimeHintsRegistrar` implementations, the process is reversed:
instead of manually checking for registered hints in a `RuntimeHints`
instance, tests should exercise the use cases and then check that the
recorded behavior is in line with the prepared hints.

This first commit adds the agent infrastructure that collects the
invocations for all relevant JDK methods.

See gh-27981
2022-06-30 18:20:16 +02:00
Scott Frederick 45ef21f900 Add support for annotation processors with TestCompiler
Closes gh-28582
2022-06-07 16:53:28 -07:00
Scott Frederick 4128a71657 Polish javadoc 2022-06-07 16:49:42 -07:00
Phillip Webb 7119d420ce Define compiled classes when @CompileWithTargetClassAccess is used
Update `DynamicClassLoader` to load classes eagerly if they are being
defined in the parent classloader.

See gh-28580
2022-06-07 16:45:54 -07:00
Phillip Webb 46a2f2d71c Allow @CompileWithTargetClassAccess to work with all classes
Switch from `MethodHandles.privateLookupIn` to a reflection based
approach to that target classes to not need to be explicitly listed.

Closes gh-28580
2022-06-07 14:12:15 -07:00
Phillip Webb 26944f3c8e Implement CompileWithTargetClassAccessClassLoader.findResource
Fixes gh-28574
2022-06-06 12:28:33 -07:00
Phillip Webb 3ebdaeabd3 Publish spring-core-test
Closes gh-28558
2022-06-02 18:07:50 -07:00
Phillip Webb 74caa9213a Simplify SourceFileAssert assertion methods
Remove assertion methods that turned out not to be needed when
testing Spring Framework's AOT generated code.

Closes gh-28556
2022-06-02 18:01:39 -07:00
Sam Brannen bc3b3d01ee Polishing 2022-06-01 14:57:16 +02:00
Phillip Webb 9c12833e16 Add withFiles support to TestCompiler
Update `TestCompiler` so that `withFiles` can be used to quickly
configure the compiler with `InMemoryGeneratedFiles`.

See gh-28414
2022-05-05 13:25:36 -07:00
Phillip Webb 4b82546b97 Improve TestCompiler and allow lookup based class defines
Update the `TestCompiler` so that classes can be defined using
a `Lookup`. This update allows package-private classes to be
accessed without needing a quite so unusual classloader setup.

The `@CompileWithTargetClassAccess` should be added to any
test that needs to use `Lookup` based defines. The test will
run with a completed forked classloader so not to pollute the
main classloader.

This commit also adds some useful additional APIs.

See gh-28120
2022-05-05 12:35:21 -07:00
Phillip Webb d30e6bf647 Add MockSpringFactoriesLoader
Add a `MockSpringFactoriesLoader` class which allows for easier
testing of code that works with a `SpringFactoriesLoader`.

Closes gh-28416
2022-05-05 12:35:21 -07:00
Phillip Webb 63b129a3f7 Check that nullable annotations are from org.springframework.lang
Fix a few incorrect nullable annotation imports and add a checkstyle
rule to ensure they don't return.

Closes gh-28410
2022-05-03 11:14:53 -07:00
Stephane Nicoll 7255a8b48e Polish "Add module to support testing of generated code"
See gh-28120

Co-authored-by: Andy Wilkinson <wilkinsona@vmware.com>
2022-03-09 11:17:21 +01:00
Phillip Webb 653dc5951d Add module to support testing of generated code
Add a new unpublished `spring-core-test` module to support testing of
generated code. The module include a `TestCompiler` class which can be
used to dynamically compile generated Java code. It also include an
AssertJ friendly `SourceFile` class which uses qdox to provide targeted
assertions on specific parts of a generated source file.

See gh-28120
2022-03-09 11:17:21 +01:00