Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<checkstyle.version>13.4.2</checkstyle.version>
<junit.version>6.1.0</junit.version>
<assertj.version>3.27.7</assertj.version>
<errorprone.version>2.49.0</errorprone.version>
<nullaway.version>0.13.4</nullaway.version>
</properties>
Expand All @@ -37,6 +38,13 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Source: https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

@NullMarked
class JSpecifyAnnotationOrderCheckTest extends CheckTestSupport {
Expand All @@ -17,40 +16,40 @@ class JSpecifyAnnotationOrderCheckTest extends CheckTestSupport {
@Test
void jspecifyAnnotationAsLastPasses() throws Exception {
var v = runCheck(JSpecifyAnnotationOrderCheck.class, BASE + "GoodLast.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void jspecifyAnnotationFollowedByOtherFailsAndHighlightsCloseAnnotation() throws Exception {
var v = runCheck(JSpecifyAnnotationOrderCheck.class, BASE + "BadInMiddle.java");
assertEquals(1, v.size());
assertTrue(v.getFirst().contains(NullMarked.class.getSimpleName()), v.toString());
assertTrue(v.getFirst().startsWith("6:"), "violation should be reported on the @NullMarked line: " + v);
assertThat(v).hasSize(1);
assertThat(v.getFirst()).contains(NullMarked.class.getSimpleName());
assertThat(v.getFirst()).startsWith("6:");
}

@Test
void jspecifyAnnotationAsFirstFailsAndHighlightsCloseAnnotation() throws Exception {
var v = runCheck(JSpecifyAnnotationOrderCheck.class, BASE + "BadFirst.java");
assertEquals(1, v.size());
assertTrue(v.getFirst().startsWith("5:"), "violation should be reported on the @NullMarked line: " + v);
assertThat(v).hasSize(1);
assertThat(v.getFirst()).startsWith("5:");
}

@Test
void noJspecifyAnnotationDoesNotFire() throws Exception {
var v = runCheck(JSpecifyAnnotationOrderCheck.class, BASE + "NoJSpecify.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void customCloseAnnotationsArePicked() throws Exception {
var properties = Map.of("closeAnnotations", "Custom,AnotherClose");

var ok = runCheck(JSpecifyAnnotationOrderCheck.class, BASE + "CustomCloseLast.java", properties);
assertEquals(List.of(), ok);
assertThat(ok).isEmpty();

var bad = runCheck(JSpecifyAnnotationOrderCheck.class, BASE + "CustomCloseInMiddle.java", properties);
assertEquals(1, bad.size());
assertTrue(bad.getFirst().contains("Custom"), bad.toString());
assertThat(bad).hasSize(1);
assertThat(bad.getFirst()).contains("Custom");
}

@Test
Expand All @@ -60,6 +59,6 @@ void closeAnnotationsAreTrimmed() throws Exception {
BASE + "GoodLast.java",
Map.of("closeAnnotations", " NullMarked , NullUnmarked ")
);
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

@NullMarked
class JSpecifyInlineTypeUseCheckTest extends CheckTestSupport {
Expand All @@ -14,67 +12,61 @@ class JSpecifyInlineTypeUseCheckTest extends CheckTestSupport {

@Test
void inlineMethodReturnPasses() throws Exception {
assertEquals(List.of(), runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineMethod.java"));
assertThat(runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineMethod.java")).isEmpty();
}

@Test
void inlineFieldPasses() throws Exception {
assertEquals(List.of(), runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineField.java"));
assertThat(runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineField.java")).isEmpty();
}

@Test
void inlineParameterPasses() throws Exception {
assertEquals(List.of(), runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineParameter.java"));
assertThat(runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineParameter.java")).isEmpty();
}

@Test
void inlineLocalPasses() throws Exception {
assertEquals(List.of(), runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineLocal.java"));
assertThat(runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "GoodInlineLocal.java")).isEmpty();
}

@Test
void annotationOnLineAboveMethodFails() throws Exception {
var v = runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "BadAboveMethod.java");
assertEquals(1, v.size(), v.toString());
assertThat(v).hasSize(1);
}

@Test
void annotationOnLineAboveFieldFails() throws Exception {
var v = runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "BadAboveField.java");
assertEquals(1, v.size(), v.toString());
assertThat(v).hasSize(1);
}

@Test
void annotationOnLineAboveParameterFails() throws Exception {
var v = runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "BadAboveParameter.java");
assertEquals(1, v.size(), v.toString());
assertThat(v).hasSize(1);
}

@Test
void annotationOnLineAboveLocalFails() throws Exception {
var v = runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "BadAboveLocal.java");
assertEquals(1, v.size(), v.toString());
assertThat(v).hasSize(1);
}

@Test
void nonJspecifyNullableImportedFromElsewhereIsIgnored() throws Exception {
assertEquals(
List.of(),
runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "NonJSpecifyNullableAboveFieldIgnored.java")
);
assertThat(runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "NonJSpecifyNullableAboveFieldIgnored.java")).isEmpty();
}

@Test
void nonJspecifyFullyQualifiedNullableIsIgnored() throws Exception {
assertEquals(
List.of(),
runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "NonJSpecifyFqnNullableAboveFieldIgnored.java")
);
assertThat(runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "NonJSpecifyFqnNullableAboveFieldIgnored.java")).isEmpty();
}

@Test
void fullyQualifiedJspecifyNullableAboveFieldFails() throws Exception {
var v = runCheck(JSpecifyInlineTypeUseCheck.class, BASE + "JSpecifyFqnAboveFieldFails.java");
assertEquals(1, v.size(), v.toString());
assertThat(v).hasSize(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

@NullMarked
class JSpecifyMapStructMapperAnnotationCheckTest extends CheckTestSupport {
Expand All @@ -16,98 +14,86 @@ class JSpecifyMapStructMapperAnnotationCheckTest extends CheckTestSupport {

@Test
void wellOrderedMapperPasses() throws Exception {
assertEquals(List.of(), runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodMapper.java"));
assertThat(runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodMapper.java")).isEmpty();
}

@Test
void mapperWithNamedValueArgumentPasses() throws Exception {
assertEquals(
List.of(),
runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodMapperNamedValue.java")
);
assertThat(runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodMapperNamedValue.java")).isEmpty();
}

@Test
void swappedOrderFails() throws Exception {
var v = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "BadOrderSwapped.java");
assertEquals(1, v.size());
assertTrue(v.getFirst().contains("BadOrderSwapped"), v.toString());
assertThat(v).hasSize(1);
assertThat(v.getFirst()).contains("BadOrderSwapped");
}

@Test
void missingAnnotateWithFails() throws Exception {
var v = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "BadMissingAnnotateWith.java");
assertEquals(1, v.size());
assertThat(v).hasSize(1);
}

@Test
void annotateWithWrongClassLiteralFails() throws Exception {
var v = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "BadWrongClassLiteral.java");
assertEquals(1, v.size());
assertThat(v).hasSize(1);
}

@Test
void interfaceWithoutMapperIsIgnored() throws Exception {
assertEquals(
List.of(),
runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "NonMapperInterfaceIgnored.java")
);
assertThat(runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "NonMapperInterfaceIgnored.java")).isEmpty();
}

@Test
void customMapperAnnotationNameIsHonored() throws Exception {
var properties = Map.of("mapperAnnotationName", "MyMapper");

var ok = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "CustomMapperGood.java", properties);
assertEquals(List.of(), ok);
assertThat(ok).isEmpty();

var bad = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "CustomMapperBad.java", properties);
assertEquals(1, bad.size());
assertTrue(bad.getFirst().contains("CustomMapperBad"), bad.toString());
assertThat(bad).hasSize(1);
assertThat(bad.getFirst()).contains("CustomMapperBad");
}

@Test
void customAnnotateWithAnnotationNameIsHonored() throws Exception {
var properties = Map.of("annotateWithAnnotationName", "MyAnnotateWith");

var ok = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "CustomAnnotateWithGood.java", properties);
assertEquals(List.of(), ok);
assertThat(ok).isEmpty();

var bad = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodMapper.java", properties);
assertEquals(1, bad.size(), bad.toString());
assertThat(bad).hasSize(1);
}

@Test
void customNullUnmarkedAnnotationNameIsHonored() throws Exception {
var properties = Map.of("nullUnmarkedAnnotationName", "MyUnmarked");

var ok = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "CustomNullUnmarkedGood.java", properties);
assertEquals(List.of(), ok);
assertThat(ok).isEmpty();

var bad = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodMapper.java", properties);
assertEquals(1, bad.size(), bad.toString());
assertThat(bad).hasSize(1);
}

@Test
void wellOrderedAbstractClassMapperPasses() throws Exception {
assertEquals(
List.of(),
runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodAbstractMapper.java")
);
assertThat(runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "GoodAbstractMapper.java")).isEmpty();
}

@Test
void abstractClassMapperWithSwappedOrderFails() throws Exception {
var v = runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "BadAbstractMapperOrderSwapped.java");
assertEquals(1, v.size());
assertTrue(v.getFirst().contains("BadAbstractMapperOrderSwapped"), v.toString());
assertThat(v).hasSize(1);
assertThat(v.getFirst()).contains("BadAbstractMapperOrderSwapped");
}

@Test
void nonAbstractClassWithMapperAnnotationIsIgnored() throws Exception {
assertEquals(
List.of(),
runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "NonAbstractClassWithMapperAnnotationIgnored.java")
);
assertThat(runCheck(JSpecifyMapStructMapperAnnotationCheck.class, BASE + "NonAbstractClassWithMapperAnnotationIgnored.java")).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

@NullMarked
class JSpecifyOnTopLevelTypesCheckTest extends CheckTestSupport {
Expand All @@ -16,44 +13,44 @@ class JSpecifyOnTopLevelTypesCheckTest extends CheckTestSupport {
@Test
void classMarkedWithNullMarkedPasses() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "GoodMarked.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void classMarkedWithNullUnmarkedPasses() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "GoodUnmarked.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void fullyQualifiedJspecifyAnnotationPasses() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "GoodFullyQualified.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void classWithoutJspecifyAnnotationFails() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "BadMissing.java");
assertEquals(1, v.size());
assertTrue(v.getFirst().contains("BadMissing"), v.toString());
assertThat(v).hasSize(1);
assertThat(v.getFirst()).contains("BadMissing");
}

@Test
void annotationDeclarationIsExempt() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "AnnotationDefExempt.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void nestedClassIsNotChecked() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "NestedClassOnly.java");
assertEquals(List.of(), v);
assertThat(v).isEmpty();
}

@Test
void recordWithoutJspecifyFails() throws Exception {
var v = runCheck(JSpecifyOnTopLevelTypesCheck.class, BASE + "BadRecord.java");
assertEquals(1, v.size());
assertTrue(v.getFirst().contains("BadRecord"), v.toString());
assertThat(v).hasSize(1);
assertThat(v.getFirst()).contains("BadRecord");
}
}