1 /*
2  * Copyright 2022 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 IgnoreClassLevelDetectorTest :
25     AbstractLintDetectorTest(
26         useDetector = IgnoreClassLevelDetector(),
27         useIssues = listOf(IgnoreClassLevelDetector.ISSUE),
28     ) {
29     @Test
Detection of class level ignore in Kotlin sourcesnull30     fun `Detection of class level ignore in Kotlin sources`() {
31         val input =
32             arrayOf(
33                 kotlin(
34                     """
35                 package java.androidx
36 
37                 import org.junit.Ignore
38                 import org.junit.Test
39 
40                 @Ignore("Class")
41                 class TestClass {
42                     @Test
43                     fun oneTest() {}
44 
45                     @Test
46                     @Ignore
47                     fun twoTest() {}
48                 }
49             """
50                 ),
51                 Stubs.IgnoreAnnotation,
52                 Stubs.TestAnnotation
53             )
54 
55         val expected =
56             """
57 src/java/androidx/TestClass.kt:7: Error: @Ignore should not be used at the class level. Move the annotation to each test individually. [IgnoreClassLevelDetector]
58                 @Ignore("Class")
59                 ~~~~~~~~~~~~~~~~
60 1 errors, 0 warnings
61         """
62 
63         check(*input).expect(expected)
64     }
65 
66     @Test
Detection of class level ignore in Java sourcesnull67     fun `Detection of class level ignore in Java sources`() {
68         val input =
69             arrayOf(
70                 java(
71                     """
72                     package java.androidx;
73 
74                     import org.junit.Ignore;
75                     import org.junit.Test;
76 
77                     @Ignore
78                     public class TestClass {
79                         @Test
80                         public void oneTest() {}
81 
82                         @Ignore
83                         @Test
84                         public void twoTest() {}
85                     }
86                 """
87                 ),
88                 Stubs.IgnoreAnnotation,
89                 Stubs.TestAnnotation
90             )
91 
92         val expected =
93             """
94 src/java/androidx/TestClass.java:7: Error: @Ignore should not be used at the class level. Move the annotation to each test individually. [IgnoreClassLevelDetector]
95                     @Ignore
96                     ~~~~~~~
97 1 errors, 0 warnings
98         """
99 
100         check(*input).expect(expected)
101     }
102 
103     @Test
Test level ignore allowed in Kotlin sourcesnull104     fun `Test level ignore allowed in Kotlin sources`() {
105         val input =
106             arrayOf(
107                 kotlin(
108                     """
109                     package java.androidx
110 
111                     import org.junit.Ignore
112                     import org.junit.Test
113 
114                     class IgnoreTestLevelKotlin {
115                         @Test
116                         @Ignore("Test")
117                         fun oneTest() {}
118 
119                         @Test
120                         fun twoTest() {}
121                     }
122                 """
123                 ),
124                 Stubs.IgnoreAnnotation,
125                 Stubs.TestAnnotation
126             )
127 
128         check(*input).expectClean()
129     }
130 
131     @Test
Test level ignore allowed in Java sourcesnull132     fun `Test level ignore allowed in Java sources`() {
133         val input =
134             arrayOf(
135                 java(
136                     """
137                 package java.androidx;
138 
139                 import org.junit.Ignore;
140                 import org.junit.Test;
141 
142                 public class Test {
143                     @Test
144                     @Ignore
145                     public void oneTest() {}
146 
147                     @Test
148                     public void twoTest() {}
149                 }
150             """
151                 ),
152                 Stubs.IgnoreAnnotation,
153                 Stubs.TestAnnotation
154             )
155 
156         check(*input).expectClean()
157     }
158 }
159