Commit Graph

106 Commits

Author SHA1 Message Date
Stephane Nicoll aa5d7398d5 Polish 2022-09-15 16:01:49 +02:00
Stephane Nicoll e9997d4004 Polish 2022-09-14 14:40:02 +02:00
Stephane Nicoll 58a2b79699 Harmonize hint registration
Previously, a shortcut method for the default ExecutableMode was
provided, but we found out that the shortcut makes it harder to
determine the intent.

This commit harmonizes hints registration for types, methods, and
fields. An ExecutableMode is now mandatory to register a method or
constructor. Previous methods that infer a mode or provided a
customizer of the builder are deprecated.

Closes gh-29135
2022-09-12 14:51:04 +02:00
Stephane Nicoll 042a4f3518 Polish "Adapt FieldHint to recent GraalVM versions"
See gh-29130
2022-09-12 14:50:28 +02:00
Sam Brannen 93049f98ee Support TestNG with @CompileWithTargetClassAccess 2022-09-09 17:04:52 +02:00
Brian Clozel 323d1907c1 Do not require full type reflection when listing methods/fields
Prior to this commit, the `RuntimeHintsAgent` and its testing
infrastructure would assume that calling `MyClass.class.getMethods()`
requires a reflection hint on the class for introspecting public/private
methods.

GraalVM does not require this, in fact this call only returns methods
that have reflection hints in the native image.

This commit refines the agent behavior for `Class.getMethods()`,
`Class.getDeclaredMethods()`, `Class.getFields()` and
`Class.getDeclaredFields()`. With this change, registering at least one
method/field for reflection is enough to match.

During the execution of Java tests, all methods and fields will be
provided, regardless of hints being registered or not. This could cause
false negatives where we're missing reflection hints on methods or
fields.
This risk is mitigated thanks to additional instrumentation on
`Method.getAnnotations()`, `Method.getParameterTypes()` and
`Method.invoke()`. If a method is found reflectively, chances are it
will be used for further reflection.

Closes gh-29091
2022-09-06 22:14:10 +02:00
Sam Brannen 97b98c3378 Support default package for TypeReference in ResourceHintsPredicates
Prior to this commit, if the TypeReference supplied to
ResourceHintsPredicates.forResource(TypeReference,String) was for a
class declared in the default package (i.e., without a package), the
resolveAbsoluteResourceName() method incorrectly prepended two leading
slashes (//) to the absolute resource name, causing correct matches to
fail.

This commit fixes this by adding special handling for a TypeReference
without a package name. In addition, this commit introduces lenient
handling of resource names by consistently removing a leading slash in
ResourceHintsPredicates.forResource(*) methods. The latter aligns with
absolute resource path handling in other places in the framework, such
as ClassPathResource.

Closes gh-29086
2022-09-06 12:53:31 +02:00
Stephane Nicoll 6e93f1187c Move TestCompiler from generator to generate
This commit harmonizes the package space by moving the TestCompiler
infrastructure from the now outdated "generator" package to "generate".

Closes gh-29082
2022-09-05 16:30:19 +02:00
Stephane Nicoll 58b0251af1 Relocate TestGenerationContext to spring-core-test
This commit moves the test implementation for GenerationContext in
spring-core-test. This also removes the copy we had in testfixtures

See gh-28877
2022-09-05 16:21:17 +02:00
Andy Wilkinson 0daaa1c38a Make QDox an implementation detail of spring-core-test
Previously, com.thoughtworks.qdox:qdox was declared as an api
dependency of spring-core-test despite it only being used in the
module's internals. This resulted in it unnecessarily appearing on the
compile classpath of consuming projects.

This commit moves qdox to be an implementation dependency, removing it
from the compile classpath of consuming projects but ensuring that it's
still available at runtime.

Closes gh-29066
2022-09-03 16:45:05 +02:00
Brian Clozel 7ace1f9dc5 Align RuntimeHintsPredicates with new FieldMode
Closes gh-29063
2022-09-02 18:16:43 +02:00
Phillip Webb 505da5c602 Migrate hint registration to shortcuts
Migrate code to make use of the `MemberCategory` and `FieldMode`
shortcuts.

See gh-29011
2022-09-01 17:26:06 -07:00
Brian Clozel 289d378aeb Polish
This fixes an additional case where invoked methods are public but the
declaring class is package private.

Fixes gh-29046
2022-09-01 15:14:58 +02:00
Johnny Lim 706c1ec8dd Polish 2022-08-31 23:59:51 +09:00
Brian Clozel 3c22404bce Fix RuntimeHintsAgent instrumentation for method invocation
Prior to this commit, the `RuntimeHintsAgent` would instrument
`Method.invoke` in a way that fails when invoked methods are not public.

This commit ensures that before delegating the invocation call, the
instrumentation makes the method accessible before delegating the call.

Fixes gh-29046
2022-08-31 15:19:31 +02:00
Stephane Nicoll 7ca57b7e80 Use consistent registration API in TypeHint.Builder
This commit adapts the registration of fields, constructors, and methods
to provide the same convenience than the reflection-based one available
in ReflectionHints.

See gh-29011
2022-08-24 19:32:02 +02:00
Stephane Nicoll c164b918c0 Apply consistent RuntimeHints defaults
This commit harmonizes the registration of an executable so that
the default method and the method that takes an empty customizer
produces the same hint. The same applies to the readable flag of
a field hint.

Rather than returning a list of executable modes, the "highest" mode
is retained.

See gh-29011
2022-08-24 19:32:01 +02:00
Stephane Nicoll d6afa8df2d Improve registration of the same hint for multiple classes
Based on the feedback in #28977 an easy way to create a list of
type references based on a vararg of classes is helpful when
registering the same hints for several types.
2022-08-18 06:52:34 +02:00
Sam Brannen 7e8d6dbd45 Remove obsolete HintType.CLASS_PROXIES enum constant
See gh-28972
2022-08-17 11:16:45 +02:00
Sam Brannen a875db4692 Document limitations of @CompileWithTargetClassAccess
Specifically, @CompileWithTargetClassAccess cannot be used with
@RepeatedTest and @ParameterizedTest methods since the
CompileWithTargetClassAccessExtension cannot support @TestTemplate
invocations.
2022-08-06 14:57:35 +03:00
Brian Clozel 42b3339cb9 Fix RecordedInvocation instance resolution
This commit fixes `RecordedInvocation` and
`RuntimeHintsInvocationsAssert` so that they don't refer to the recorded
instance for static calls.

This also consistently resolves the `TypeReference` of recorded
instances.

Fixes gh-28907
2022-08-01 17:45:55 +02:00
Sam Brannen e4a7e35ead Polish TestCompiler 2022-08-01 13:41:50 +03:00
Sam Brannen 42ea09b08a Remove Hamcrest support from CompileWithTargetClassAccessClassLoader
CompileWithTargetClassAccessClassLoader is currently only used within the
CompileWithTargetClassAccessExtension which is dedicated to JUnit Jupiter
support which in turn should not have any direct dependencies on Hamcrest.

In other words, the JupiterTestEngine should not load any Hamcrest types
that would cause issues with the CompileWithTargetClassAccessClassLoader.
2022-08-01 13:37:03 +03:00
Sam Brannen d9bf3e54f7 Removed unused method parameter
See gh-28901
2022-08-01 13:29:48 +03:00
Sam Brannen ce850a583c Support overloaded test methods in CompileWithTargetClassAccessExtension
Prior to this commit, CompileWithTargetClassAccessExtension failed to
properly select overloaded test methods because it ignored the method
parameter list when looking up the test method.

This commit addresses this issue by selecting the test method using its
fully qualified method name which takes in account the class name,
method name, and parameter names.

Closes gh-28901
2022-08-01 13:07:58 +03:00
Sam Brannen 8fb27c3857 Polish CompileWithTargetClassAccessExtension 2022-08-01 13:07:58 +03:00
Sam Brannen 4896b115d2 Ensure CompileWithTargetClassAccessExtension only uses Jupiter TestEngine
Commit 9dd7f5412a (which has now been
reverted) addressed the issue of having the TestNG TestEngine for the
JUnit Platform on the test runtime classpath by allowing `org.testng`
types to pass through to the original ClassLoader; however, that fix
merely obfuscated the underlying issue.

The underlying issue is that the CompileWithTargetClassAccessExtension
is only applicable to JUnit Jupiter tests and therefore should launch
the JUnit Platform with only the JUnit Jupiter TestEngine active.

This commit addresses this issue by applying an EngineFilter to include
only the "junit-jupiter" test engine.

Closes gh-28900
2022-08-01 12:48:41 +03:00
Sam Brannen b89b6bd7a8 Revert "Support TestNG with @CompileWithTargetClassAccess"
This reverts commit 9dd7f5412a.
2022-08-01 12:44:42 +03:00
Sam Brannen fa74c353eb Revert "Polishing"
This reverts commit c8648b523b.
2022-08-01 12:44:34 +03:00
Sam Brannen c8648b523b Polishing 2022-07-31 15:07:02 +03:00
Sam Brannen 9dd7f5412a Support TestNG with @CompileWithTargetClassAccess 2022-07-31 14:57:48 +03:00
Sam Brannen e4395f2f8b Clean up warnings and polishing 2022-07-31 14:14:56 +03:00
Sam Brannen 3912ef1507 Polish TestCompiler support 2022-07-30 10:51:14 +03:00
Sam Brannen 1f3ea5133f Polishing 2022-07-15 15:28:47 +02:00
Brian Clozel 77f8408e80 Polish
Closes gh-28751
2022-07-15 14:28:19 +02:00
Sam Brannen c34ccfdb81 Simplify @EneabledIfRuntimeHintsAgent support
Since the RuntimeHintsAgentCondition is based solely on the result of
invoking a boolean static method, there is no need to implement a custom
ExecutionCondition. For such use cases, the @EnabledIf support in JUnit
Jupiter is sufficient.

This commit therefore replaces the custom RuntimeHintsAgentCondition
with use of @EnabledIf as a meta-annotation.
2022-07-15 14:22:06 +02:00
Stephane Nicoll 54a3f66d1d Move RuntimeHints predicate support to a dedicated package
This commit moves `RuntimeHintsPredicate` and its support classes that
form a cohesive concept in a dedicated `.predicate` package.

Closes gh-28799
2022-07-12 16:16:12 +02:00
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