1 /* 2 * Copyright 2021 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 @file:Suppress("UnstableApiUsage") 18 19 package androidx.build.lint 20 21 import org.junit.Test 22 import org.junit.runner.RunWith 23 import org.junit.runners.JUnit4 24 25 @RunWith(JUnit4::class) 26 class BanSynchronizedMethodsTest : 27 AbstractLintDetectorTest( 28 useDetector = BanSynchronizedMethods(), 29 useIssues = listOf(BanSynchronizedMethods.ISSUE), 30 ) { 31 32 @Test Detection of synchronized methods in Java sourcesnull33 fun `Detection of synchronized methods in Java sources`() { 34 val input = 35 java( 36 "src/androidx/SynchronizedMethodJava.java", 37 """ 38 public class SynchronizedMethodJava { 39 40 public synchronized void someMethod() { 41 } 42 } 43 """ 44 .trimIndent() 45 ) 46 47 val expected = 48 """ 49 src/androidx/SynchronizedMethodJava.java:3: Error: Use of synchronized methods is not recommended [BanSynchronizedMethods] 50 public synchronized void someMethod() { 51 ^ 52 1 errors, 0 warnings 53 """ 54 .trimIndent() 55 56 check(input).expect(expected) 57 } 58 59 @Test Detection of synchronized methods in Kotlin sourcesnull60 fun `Detection of synchronized methods in Kotlin sources`() { 61 val input = 62 kotlin( 63 "src/androidx/SynchronizedMethodKotlin.kt", 64 """ 65 class SynchronizedMethodKotlin { 66 67 @Synchronized 68 fun someMethod() {} 69 } 70 """ 71 .trimIndent() 72 ) 73 74 val expected = 75 """ 76 src/androidx/SynchronizedMethodKotlin.kt:3: Error: Use of synchronized methods is not recommended [BanSynchronizedMethods] 77 @Synchronized 78 ^ 79 1 errors, 0 warnings 80 """ 81 .trimIndent() 82 83 check(input).expect(expected) 84 } 85 } 86