1 /*
2  * Copyright 2019 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.replacewith
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 ReplaceWithDetectorMethodTest {
25 
26     @Test
staticMethodExplicitClassnull27     fun staticMethodExplicitClass() {
28         val input =
29             arrayOf(
30                 javaSample("replacewith.ReplaceWithUsageJava"),
31                 javaSample("replacewith.StaticMethodExplicitClass")
32             )
33 
34         val expected =
35             """
36 src/replacewith/StaticMethodExplicitClass.java:25: Hint: Replacement available [ReplaceWith]
37         ReplaceWithUsageJava.toString(this);
38         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39 0 errors, 0 warnings, 1 hint
40         """
41                 .trimIndent()
42 
43         val expectedFixDiffs =
44             """
45 Fix for src/replacewith/StaticMethodExplicitClass.java line 25: Replace with `this.toString()`:
46 @@ -25 +25
47 -         ReplaceWithUsageJava.toString(this);
48 +         this.toString();
49         """
50                 .trimIndent()
51 
52         check(*input).expect(expected).expectFixDiffs(expectedFixDiffs)
53     }
54 
55     @Test
methodImplicitThisnull56     fun methodImplicitThis() {
57         val input = arrayOf(javaSample("replacewith.MethodImplicitThis"))
58 
59         val expected =
60             """
61 src/replacewith/MethodImplicitThis.java:33: Hint: Replacement available [ReplaceWith]
62         oldMethod(null);
63         ~~~~~~~~~~~~~~~
64 0 errors, 0 warnings, 1 hint
65         """
66                 .trimIndent()
67 
68         val expectedFixDiffs =
69             """
70 Fix for src/replacewith/MethodImplicitThis.java line 33: Replace with `newMethod(null)`:
71 @@ -33 +33
72 -         oldMethod(null);
73 +         newMethod(null);
74         """
75                 .trimIndent()
76 
77         check(*input).expect(expected).expectFixDiffs(expectedFixDiffs)
78     }
79 
80     @Test
methodExplicitThisnull81     fun methodExplicitThis() {
82         val input = arrayOf(javaSample("replacewith.MethodExplicitThis"))
83 
84         val expected =
85             """
86 src/replacewith/MethodExplicitThis.java:33: Hint: Replacement available [ReplaceWith]
87         this.oldMethod(null);
88              ~~~~~~~~~~~~~~~
89 0 errors, 0 warnings, 1 hint
90         """
91                 .trimIndent()
92 
93         val expectedFixDiffs =
94             """
95 Fix for src/replacewith/MethodExplicitThis.java line 33: Replace with `newMethod(null)`:
96 @@ -33 +33
97 -         this.oldMethod(null);
98 +         this.newMethod(null);
99         """
100                 .trimIndent()
101 
102         check(*input).expect(expected).expectFixDiffs(expectedFixDiffs)
103     }
104 }
105