diff options
author | Manu Sridharan <msridhar@gmail.com> | 2021-12-23 10:42:52 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-23 10:42:52 -0800 |
commit | a479cca0d60754a8bdcb686e265e80e138d43791 (patch) | |
tree | da49a02216ffd6fa6afe0be827521979be845059 | |
parent | 6af3cfbf00c469b200ce1b72de471ee342658145 (diff) | |
download | nullaway-a479cca0d60754a8bdcb686e265e80e138d43791.tar.gz |
Add tests for JDK 16+ records (#527)
-rw-r--r-- | jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayRecordTests.java | 181 | ||||
-rw-r--r-- | jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwaySwitchTests.java (renamed from jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayJDK17Test.java) | 2 |
2 files changed, 182 insertions, 1 deletions
diff --git a/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayRecordTests.java b/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayRecordTests.java new file mode 100644 index 0000000..8135deb --- /dev/null +++ b/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayRecordTests.java @@ -0,0 +1,181 @@ +package com.uber.nullaway.jdk17; + +import com.google.errorprone.CompilationTestHelper; +import com.uber.nullaway.NullAway; +import java.util.Arrays; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class NullAwayRecordTests { + + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private CompilationTestHelper defaultCompilationHelper; + + @Before + public void setup() { + defaultCompilationHelper = + CompilationTestHelper.newInstance(NullAway.class, getClass()) + .setArgs( + Arrays.asList( + "-d", + temporaryFolder.getRoot().getAbsolutePath(), + "-XepOpt:NullAway:AnnotatedPackages=com.uber")); + } + + @Test + public void testRecordConstructorCalls() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " record Rec(Object first, @Nullable Object second) { }", + " public void testRecordConstructors() {", + " // BUG: Diagnostic contains: passing @Nullable parameter 'null'", + " Rec rec1 = new Rec(null, null);", + " Rec rec2 = new Rec(new Object(), null);", + " }", + "}") + .doTest(); + } + + @Test + public void testRecordInstanceMethodCalls() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " record Rec(Object first, @Nullable Object second) { }", + " public void testRecordConstructors() {", + " Rec rec = new Rec(new Object(), null);", + " rec.first().toString();", + " // BUG: Diagnostic contains: dereferenced expression rec.second()", + " rec.second().toString();", + " }", + "}") + .doTest(); + } + + @Test + public void testRecordInstanceMethods() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " record Rec(Object first, @Nullable Object second) {", + " Object m1() {", + " // BUG: Diagnostic contains: returning @Nullable expression from method", + " return second();", + " }", + " Object m2() {", + " return first();", + " }", + " void derefs() {", + " first().toString();", + " // BUG: Diagnostic contains: dereferenced expression second()", + " second().toString();", + " }", + " }", + "}") + .doTest(); + } + + @Test + public void testRecordFields() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " record Rec(Object first, @Nullable Object second) {", + " Object m1() {", + " // BUG: Diagnostic contains: returning @Nullable expression from method", + " return second;", + " }", + " Object m2() {", + " return first;", + " }", + " void derefs() {", + " first.toString();", + " // BUG: Diagnostic contains: dereferenced expression second", + " second.toString();", + " }", + " }", + "}") + .doTest(); + } + + @Test + public void testRecordConstructor() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " record Rec(Object first, @Nullable Object second) {", + " Rec {", + " first.toString();", + " // BUG: Diagnostic contains: dereferenced expression second", + " second.toString();", + " }", + " }", + "}") + .doTest(); + } + + @Test + public void testRecordImplementsInterface() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " interface I1 { Object m1(); }", + " interface I2 { void m2(@Nullable Object x); }", + " record Rec1(Object first, @Nullable Object second) implements I1 {", + " // BUG: Diagnostic contains: method returns @Nullable, but superclass method", + " @Nullable public Object m1() { return second(); }", + " }", + " record Rec2(Object first, @Nullable Object second) implements I1 {", + " public Object m1() { return first(); }", + " }", + " record Rec3(Object first, @Nullable Object second) implements I2 {", + " // BUG: Diagnostic contains: parameter x is @NonNull, but parameter in superclass", + " public void m2(Object x) { x.toString(); }", + " }", + " record Rec4(Object first, @Nullable Object second) implements I2 {", + " public void m2(@Nullable Object x) { if (x != null) { x.toString(); } }", + " }", + "}") + .doTest(); + } + + @Test + public void testLocalRecord() { + defaultCompilationHelper + .addSourceLines( + "Records.java", + "package com.uber;", + "import javax.annotation.Nullable;", + "class Records {", + " public void testLocalRecord() {", + " record Rec(Object first, @Nullable Object second) { }", + " // BUG: Diagnostic contains: passing @Nullable parameter 'null'", + " Rec rec1 = new Rec(null, null);", + " Rec rec2 = new Rec(new Object(), null);", + " }", + "}") + .doTest(); + } +} diff --git a/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayJDK17Test.java b/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwaySwitchTests.java index 0b672db..1b47d2f 100644 --- a/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwayJDK17Test.java +++ b/jdk17-unit-tests/src/test/java/com/uber/nullaway/jdk17/NullAwaySwitchTests.java @@ -30,7 +30,7 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; /** NullAway unit tests involving language features available on JDK 17 but not JDK 11. */ -public class NullAwayJDK17Test { +public class NullAwaySwitchTests { @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |