@Test public void defaultURL() { compilationHelper .setArgs( Arrays.asList( "-d", temporaryFolder.getRoot().getAbsolutePath(), "-XepOpt:NullAway:AnnotatedPackages=com.uber")) .addSourceLines( "Test.java", "package com.uber;", "class Test {", " static void foo() {", " // BUG: Diagnostic contains: t.uber.com/nullaway", " Object x = null; x.toString();", " }", "}") .doTest(); }
@Before public void setup() { compilationHelper = CompilationTestHelper.newInstance(NullAway.class, getClass()); }
@Test public void coreNullabilitySkipClass() { compilationHelper.addSourceFile("Shape_Stuff.java").doTest(); compilationHelper.addSourceFile("excluded/Shape_Stuff2.java").doTest(); compilationHelper.addSourceFile("AnnotatedClass.java").addSourceFile("TestAnnot.java").doTest(); }
@Test public void test_autodisposePositiveCasesWithDefaultClassGivenCustomTypes() { compilationHelper.setArgs(ImmutableList.of("-XepOpt:TypesWithScope" + "=com.uber.autodispose.errorprone.ComponentWithLifecycle")); compilationHelper.addSourceFile("UseAutoDisposeDefaultClassPositiveCases.java").doTest(); }
@Test public void cfNullableArrayField() { compilationHelper .addSourceLines( "CFNullable.java", "package com.uber;", "import org.checkerframework.checker.nullness.qual.Nullable;", "import java.util.List;", "abstract class CFNullable<E> {", " List<E> @Nullable [] table;", "}") .doTest(); }
@Test public void testCastToNonNull() { compilationHelper .addSourceFile("Util.java") .addSourceLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", "import static com.uber.nullaway.testdata.Util.castToNonNull;", "class Test {", " Object test1(@Nullable Object o) {", " return castToNonNull(o);", " }", " Object test2(Object o) {", " // BUG: Diagnostic contains: passing known @NonNull parameter 'o' to CastToNonNullMethod", " return castToNonNull(o);", " }", "}") .doTest(); }
@Before public void setup() { compilationHelper = CompilationTestHelper.newInstance(NullAway.class, getClass()); compilationHelper.setArgs( Arrays.asList( "-d", temporaryFolder.getRoot().getAbsolutePath(), "-XepOpt:NullAway:AnnotatedPackages=com.uber,com.ubercab,io.reactivex")); }
/** Initialises the default android classes that are commonly used. */ private void initialiseAndroidCoreClasses() { compilationHelper .addSourceFile("androidstubs/core/Context.java") .addSourceFile("androidstubs/core/Bundle.java") .addSourceFile("androidstubs/core/LayoutInflater.java") .addSourceFile("androidstubs/core/PersistableBundle.java") .addSourceFile("androidstubs/core/View.java") .addSourceFile("androidstubs/core/ViewGroup.java"); } }
/** * Returns a new {@link CompilationTestHelper}. * * @param scannerSupplier the {@link ScannerSupplier} to test * @param clazz the class to use to locate file resources */ @CheckReturnValue public static CompilationTestHelper newInstance(ScannerSupplier scannerSupplier, Class<?> clazz) { return new CompilationTestHelper(scannerSupplier, null, clazz); }
@Test public void arrayIndexUnbox() { compilationHelper .addSourceLines( "Test.java", "package com.uber;", "class Test {", " static void indexUnbox() {", " Integer x = null; int[] fizz = { 0, 1 };", " // BUG: Diagnostic contains: unboxing of a @Nullable value", " int y = fizz[x];", " }", "}") .doTest(); }
@Test public void test_autodisposeNegativeCasesWithDefaultClassGivenExclusiveScope() { compilationHelper.setArgs(ImmutableList.of("-XepOpt:TypesWithScope" + "=com.uber.autodispose.errorprone.ComponentWithLifecycle", "-XepOpt:OverrideScopes")); compilationHelper.addSourceFile("UseAutoDisposeNegativeCasesExcluded.java").doTest(); }
@Before public void setup() { compilationHelper = CompilationTestHelper.newInstance(NullAway.class, getClass()); compilationHelper.setArgs( Arrays.asList( "-d", temporaryFolder.getRoot().getAbsolutePath(), "-XepOpt:NullAway:KnownInitializers=" + "com.uber.nullaway.testdata.CheckFieldInitNegativeCases.Super.doInit," + "com.uber.nullaway.testdata.CheckFieldInitNegativeCases" + ".SuperInterface.doInit2", "-XepOpt:NullAway:AnnotatedPackages=com.uber,com.ubercab,io.reactivex", // We give the following in Regexp format to test that support "-XepOpt:NullAway:UnannotatedSubPackages=com.uber.nullaway.[a-zA-Z0-9.]+.unannotated", "-XepOpt:NullAway:ExcludedClasses=" + "com.uber.nullaway.testdata.Shape_Stuff," + "com.uber.nullaway.testdata.excluded", "-XepOpt:NullAway:ExcludedClassAnnotations=com.uber.nullaway.testdata.TestAnnot", "-XepOpt:NullAway:CastToNonNullMethod=com.uber.nullaway.testdata.Util.castToNonNull", "-XepOpt:NullAway:ExternalInitAnnotations=com.uber.ExternalInit", "-XepOpt:NullAway:ExcludedFieldAnnotations=com.uber.ExternalFieldInit")); }
/** * Returns a new {@link CompilationTestHelper}. * * @param checker the {@link BugChecker} to test * @param clazz the class to use to locate file resources */ @CheckReturnValue public static CompilationTestHelper newInstance( Class<? extends BugChecker> checker, Class<?> clazz) { ScannerSupplier scannerSupplier = ScannerSupplier.fromBugCheckerClasses(checker); String checkName = checker.getAnnotation(BugPattern.class).name(); return new CompilationTestHelper(scannerSupplier, checkName, clazz); }
@Test public void customErrorURL() { compilationHelper .setArgs( Arrays.asList( "-d", temporaryFolder.getRoot().getAbsolutePath(), "-XepOpt:NullAway:AnnotatedPackages=com.uber", "-XepOpt:NullAway:ErrorURL=http://mydomain.com/nullaway")) .addSourceLines( "Test.java", "package com.uber;", "class Test {", " static void foo() {", " // BUG: Diagnostic contains: mydomain.com", " Object x = null; x.toString();", " }", "}") .doTest(); }
@Test public void coreNullabilityNegativeCases() { compilationHelper .addSourceFile("NullAwayNegativeCases.java") .addSourceFile("OtherStuff.java") .addSourceFile("TestAnnot.java") .addSourceFile("unannotated/UnannotatedClass.java") .doTest(); }
@Test public void typeUseJarReturn() { compilationHelper .addSourceLines( "Test.java", "package com.uber;", "import com.uber.lib.*;", "class Test {", " void foo(CFNullableStuff.NullableReturn r) {", " // BUG: Diagnostic contains: dereferenced expression", " r.get().toString();", " }", "}") .doTest(); }
@Test public void test_autodisposePositiveCasesWithCustomClass() { compilationHelper.setArgs(ImmutableList.of("-XepOpt:TypesWithScope" + "=com.uber.autodispose.errorprone.ComponentWithLifecycle")); compilationHelper.addSourceFile("UseAutoDisposeCustomClassPositiveCases.java").doTest(); }
@Before public void setup() { compilationHelper = CompilationTestHelper.newInstance(UseAutoDispose.class, getClass()); }
@Test public void contractNonVarArg() { compilationHelper .setArgs( Arrays.asList( "-d", temporaryFolder.getRoot().getAbsolutePath(), "-XepOpt:NullAway:AnnotatedPackages=com.uber")) .addSourceLines( "NullnessChecker.java", "package com.uber;", "import javax.annotation.Nullable;", "import org.jetbrains.annotations.Contract;", "public class NullnessChecker {", " @Contract(\"null -> fail\")", " static void assertNonNull(@Nullable Object o) { if (o != null) throw new Error(); }", "}") .addSourceLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", "class Test {", " void test(java.util.function.Function<Object, Object> fun) {", " NullnessChecker.assertNonNull(fun.apply(new Object()));", " }", "}") .doTest(); }
@Test public void coreNullabilityNativeModels() { compilationHelper .addSourceFile("NullAwayNativeModels.java") .addSourceFile("androidstubs/WebView.java") .addSourceFile("androidstubs/TextUtils.java") .doTest(); }