Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ public class {{classname}}Controller implements {{classname}} {
{{^isDelegate}}
{{^reactive}}

@Nullable
private final NativeWebRequest request;

@Autowired
public {{classname}}Controller(@Nullable NativeWebRequest request) {
public {{classname}}Controller(NativeWebRequest request) {
this.request = request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public JavaFileAssert fileContains(final String... lines) {
.toString();
Assertions.assertThat(actualBody)
.withFailMessage(
"File should contains lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
"File should contain lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualBody
)
.contains(lines);
Expand All @@ -209,7 +209,7 @@ public JavaFileAssert fileDoesNotContain(final String... lines) {
.toString();
Assertions.assertThat(actualBody)
.withFailMessage(
"File should not contains lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
"File should not contain lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualBody
)
.doesNotContain(lines);
Expand All @@ -227,11 +227,25 @@ public JavaFileAssert fileContainsPattern(final String pattern) {
.toString();
Assertions.assertThat(actualBody)
.withFailMessage(
"File should contains pattern\n====\n%s\n====\nbut actually was\n====\n%s\n====",
"File should contain pattern\n====\n%s\n====\nbut actually was\n====\n%s\n====",
pattern, actualBody
)
.containsPattern(pattern);

return this;
}

public JavaFileAssert fileDoesNotContainPattern(final String pattern) {
final String actualBody = actual.getTokenRange()
.orElseThrow(() -> new IllegalStateException("Empty file"))
.toString();
Assertions.assertThat(actualBody)
.withFailMessage(
"File should not contain pattern\n====\n%s\n====\nbut actually was\n====\n%s\n====",
pattern, actualBody
)
.doesNotContainPattern(pattern);

return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.assertj.core.util.CanIgnoreReturnValue;

import java.util.List;
import java.util.regex.Pattern;

@CanIgnoreReturnValue
public class TypeAnnotationsAssert extends AbstractAnnotationsAssert<TypeAnnotationsAssert> {
Expand All @@ -18,4 +19,30 @@ protected TypeAnnotationsAssert(final JavaFileAssert fileAssert, final List<Anno
public JavaFileAssert toType() {
return fileAssert;
}

/**
* assert that the annotation is not specifed in an import.
*
* @param name classname of the annotation. For example "Nullable" or a full qualified class name like "java.util.List"
*/
public TypeAnnotationsAssert doesNotImportAnnotation(final String name) {
String pattern = "import\\s+" +
(name.contains(".")?"" : "[\\w.]+\\.") +
Pattern.quote(name) + ";";
this.toType().fileDoesNotContainPattern(pattern);
return this;
}

/**
* assert that the annotation is imported.
*
* @param name clasname of the annotation. For example "Nullable" or a full qualified class name like "java.util.List"
*/
public TypeAnnotationsAssert doesImportAnnotation(final String name) {
String pattern = "import\\s+" +
(name.contains(".")?"" : "[\\w.]+\\.") +
Pattern.quote(name) + ";";
this.toType().fileContainsPattern(pattern);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6629,6 +6629,7 @@ public void testJspecify(String library, int springBootVersion, String fooApiFil
CONTAINER_DEFAULT_TO_NULL, true,
OPENAPI_NULLABLE, false,
USE_BEANVALIDATION, true,
INTERFACE_ONLY, false,
springVersionProperty, springBootVersion > 2
),
codegenConfigurator ->
Expand All @@ -6648,12 +6649,14 @@ public void testJspecify(String library, int springBootVersion, String fooApiFil
.doesNotContain("findbugs");
}
JavaFileAssert.assertThat(files.get("Foo.java"))
.assertTypeAnnotations().doesImportAnnotation("org.jspecify.annotations.Nullable").toType()
.fileContains(
"private java.time.@Nullable Instant dt;",
"private org.springframework.core.io.@Nullable Resource binary",
"setBinary(org.springframework.core.io.@Nullable Resource binary)"
);
JavaFileAssert.assertThat(files.get(fooApiFilename))
.assertTypeAnnotations().doesImportAnnotation("org.jspecify.annotations.Nullable").toType()
.fileContains(
"java.time.@Nullable Instant dtParam",
"java.time.@Nullable Instant dtQuery",
Expand All @@ -6663,6 +6666,14 @@ public void testJspecify(String library, int springBootVersion, String fooApiFil
.fileContains("@org.jspecify.annotations.NullMarked");
JavaFileAssert.assertThat(files.get("model/package-info.java"))
.fileContains("@org.jspecify.annotations.NullMarked");

if (SPRING_BOOT.equals(library)) {
// Nullable annotation is not (yet) put on NativeWebRequest, but still present as import when useJspecify=true
JavaFileAssert.assertThat(files.get("UploadApiController.java").toPath())
.assertTypeAnnotations()
.doesNotContainWithName("Nullable")
.doesImportAnnotation("org.jspecify.annotations.Nullable");
}
}

// -------------------------------------------------------------------------
Expand Down
Loading