1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.build.lint 18 19 import org.junit.Test 20 import org.junit.runner.RunWith 21 import org.junit.runners.JUnit4 22 23 @RunWith(JUnit4::class) 24 class BanNullMarkedTest : 25 AbstractLintDetectorTest( 26 useDetector = BanNullMarked(), 27 useIssues = listOf(BanNullMarked.ISSUE), 28 stubs = arrayOf(nullMarkedStub) 29 ) { 30 @Test Usage of NullMarked in a package-info filenull31 fun `Usage of NullMarked in a package-info file`() { 32 val input = 33 java( 34 """ 35 @NullMarked 36 package test.pkg; 37 38 import org.jspecify.annotations.NullMarked; 39 """ 40 .trimIndent() 41 ) 42 43 val expected = 44 """ 45 src/test/pkg/package-info.java:1: Error: Should not use @NullMarked annotation [BanNullMarked] 46 @NullMarked 47 ~~~~~~~~~~~ 48 1 errors, 0 warnings 49 """ 50 .trimIndent() 51 52 check(input).expect(expected) 53 } 54 55 @Test Usage of NullMarked on a classnull56 fun `Usage of NullMarked on a class`() { 57 val input = 58 java( 59 """ 60 package test.pkg; 61 62 import org.jspecify.annotations.NullMarked; 63 64 @NullMarked 65 public class Foo {} 66 """ 67 .trimIndent() 68 ) 69 70 val expected = 71 """ 72 src/test/pkg/Foo.java:5: Error: Should not use @NullMarked annotation [BanNullMarked] 73 @NullMarked 74 ~~~~~~~~~~~ 75 1 errors, 0 warnings 76 """ 77 .trimIndent() 78 79 check(input).expect(expected) 80 } 81 82 @Test Usage of NullMarked on a methodnull83 fun `Usage of NullMarked on a method`() { 84 val input = 85 java( 86 """ 87 package test.pkg; 88 89 import org.jspecify.annotations.NullMarked; 90 91 public class Foo { 92 @NullMarked 93 public void foo() {} 94 } 95 """ 96 .trimIndent() 97 ) 98 99 val expected = 100 """ 101 src/test/pkg/Foo.java:6: Error: Should not use @NullMarked annotation [BanNullMarked] 102 @NullMarked 103 ~~~~~~~~~~~ 104 1 errors, 0 warnings 105 """ 106 .trimIndent() 107 108 check(input).expect(expected) 109 } 110 111 @Test Usage of NullMarked on a constructornull112 fun `Usage of NullMarked on a constructor`() { 113 val input = 114 java( 115 """ 116 package test.pkg; 117 118 import org.jspecify.annotations.NullMarked; 119 120 public class Foo { 121 @NullMarked 122 public Foo() {} 123 } 124 """ 125 .trimIndent() 126 ) 127 128 val expected = 129 """ 130 src/test/pkg/Foo.java:6: Error: Should not use @NullMarked annotation [BanNullMarked] 131 @NullMarked 132 ~~~~~~~~~~~ 133 1 errors, 0 warnings 134 """ 135 .trimIndent() 136 137 check(input).expect(expected) 138 } 139 140 companion object { 141 private val nullMarkedStub = 142 java( 143 """ 144 package org.jspecify.annotations; 145 146 import java.lang.annotation.ElementType; 147 import java.lang.annotation.Target; 148 149 @Target({ElementType.MODULE, ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR}) 150 public @interface NullMarked {} 151 """ 152 .trimIndent() 153 ) 154 } 155 } 156