1 /*
2  * Copyright (C) 2022 The Dagger Authors.
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 dagger.functional.kotlinsrc.names
18 
19 import com.google.common.truth.Truth.assertThat
20 import dagger.Component
21 import javax.inject.Inject
22 import javax.inject.Provider
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.junit.runners.JUnit4
26 
27 /** This is a regression test for https://github.com/google/dagger/issues/3069. */
28 @RunWith(JUnit4::class)
29 class ComponentFactoryNameConflictsTest {
30   // A class name "Create" so the private method name in fastInit mode conflicts with the create()
31   // factory method.
32   class Create
33   // Take a dependency so we don't just inline this directly.
34   @Inject
35   internal constructor(@Suppress("UNUSED_PARAMETER") provider: Provider<CreateUsage>)
36 
37   // Use another class to make sure the create class is used but not a direct component entry point.
38   class CreateUsage @Inject internal constructor(@Suppress("UNUSED_PARAMETER") create: Create)
39 
40   @Component
41   internal interface CreateComponent {
createUsagenull42     fun createUsage(): CreateUsage
43   }
44 
45   @Test
46   fun testCreate() {
47     val testComponent = DaggerComponentFactoryNameConflictsTest_CreateComponent.create()
48     val createUsage = testComponent.createUsage()
49     assertThat(createUsage).isNotNull()
50   }
51 
52   // A class name "Builder" so the private method name in fastInit mode conflicts with the builder()
53   // factory method.
54   class Builder
55   // Take a dependency so we don't just inline this directly.
56   @Inject
57   internal constructor(@Suppress("UNUSED_PARAMETER") provider: Provider<BuilderUsage>)
58 
59   class BuilderUsage @Inject internal constructor(@Suppress("UNUSED_PARAMETER") create: Builder)
60 
61   @Component
62   internal interface BuilderComponent {
builderUsagenull63     fun builderUsage(): BuilderUsage
64 
65     @Component.Builder
66     interface OtherBuilder {
67       fun build(): BuilderComponent
68     }
69   }
70 
71   // Technically this test passes without claiming the name "builder" when we add the method (even
72   // though we do anyway for safety) because KeyVariableNamer actually hardcodes a list of common
73   // names to avoid which includes "builder".
74   @Test
testBuildernull75   fun testBuilder() {
76     val testComponent = DaggerComponentFactoryNameConflictsTest_BuilderComponent.builder().build()
77     val builderUsage = testComponent.builderUsage()
78     assertThat(builderUsage).isNotNull()
79   }
80 }
81