1 /* 2 * Copyright (C) 2015 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.internal.codegen; 18 19 import static dagger.internal.codegen.base.ComponentCreatorAnnotation.COMPONENT_FACTORY; 20 import static dagger.internal.codegen.binding.ErrorMessages.creatorMessagesFor; 21 22 import androidx.room.compiler.processing.util.Source; 23 import com.google.common.collect.ImmutableList; 24 import dagger.internal.codegen.binding.ErrorMessages; 25 import dagger.testing.compile.CompilerTests; 26 import dagger.testing.golden.GoldenFileRule; 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.Parameterized; 31 import org.junit.runners.Parameterized.Parameters; 32 33 /** Tests for {@link dagger.Component.Factory} */ 34 @RunWith(Parameterized.class) 35 public class ComponentFactoryTest { 36 @Parameters(name = "{0}") parameters()37 public static ImmutableList<Object[]> parameters() { 38 return CompilerMode.TEST_PARAMETERS; 39 } 40 41 @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule(); 42 43 private final CompilerMode compilerMode; 44 ComponentFactoryTest(CompilerMode compilerMode)45 public ComponentFactoryTest(CompilerMode compilerMode) { 46 this.compilerMode = compilerMode; 47 } 48 49 private static final ErrorMessages.ComponentCreatorMessages MSGS = 50 creatorMessagesFor(COMPONENT_FACTORY); 51 52 @Test testUsesParameterNames()53 public void testUsesParameterNames() throws Exception { 54 Source moduleFile = 55 CompilerTests.javaSource( 56 "test.TestModule", 57 "package test;", 58 "", 59 "import dagger.Module;", 60 "import dagger.Provides;", 61 "", 62 "@Module", 63 "final class TestModule {", 64 " @Provides String string() { return null; }", 65 "}"); 66 67 Source componentFile = 68 CompilerTests.javaSource( 69 "test.TestComponent", 70 "package test;", 71 "", 72 "import dagger.Component;", 73 "", 74 "@Component(modules = TestModule.class)", 75 "interface TestComponent {", 76 " String string();", 77 "", 78 " @Component.Factory", 79 " interface Factory {", 80 " TestComponent newTestComponent(TestModule mod);", 81 " }", 82 "}"); 83 84 CompilerTests.daggerCompiler(moduleFile, componentFile) 85 .withProcessingOptions(compilerMode.processorOptions()) 86 .compile( 87 subject -> { 88 subject.hasErrorCount(0); 89 subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent")); 90 }); 91 } 92 93 @Test testSetterMethodFails()94 public void testSetterMethodFails() { 95 Source componentFile = 96 CompilerTests.javaSource( 97 "test.SimpleComponent", 98 "package test;", 99 "", 100 "import dagger.Component;", 101 "import javax.inject.Provider;", 102 "", 103 "@Component", 104 "abstract class SimpleComponent {", 105 " @Component.Factory", 106 " interface Factory {", 107 " SimpleComponent create();", 108 " Factory set(String s);", 109 " }", 110 "}"); 111 CompilerTests.daggerCompiler(componentFile) 112 .withProcessingOptions(compilerMode.processorOptions()) 113 .compile( 114 subject -> { 115 subject.hasErrorCount(1); 116 subject.hasErrorContaining( 117 String.format( 118 MSGS.twoFactoryMethods(), 119 "test.SimpleComponent test.SimpleComponent.Factory.create()")) 120 .onSource(componentFile) 121 .onLineContaining("Factory set(String s);"); 122 }); 123 } 124 125 @Test testInheritedSetterMethodFails()126 public void testInheritedSetterMethodFails() { 127 Source componentFile = 128 CompilerTests.javaSource( 129 "test.SimpleComponent", 130 "package test;", 131 "", 132 "import dagger.Component;", 133 "import javax.inject.Provider;", 134 "", 135 "@Component", 136 "abstract class SimpleComponent {", 137 " interface Parent {", 138 " SimpleComponent create();", 139 " Parent set(String s);", 140 " }", 141 "", 142 " @Component.Factory", 143 " interface Factory extends Parent {}", 144 "}"); 145 CompilerTests.daggerCompiler(componentFile) 146 .withProcessingOptions(compilerMode.processorOptions()) 147 .compile( 148 subject -> { 149 subject.hasErrorCount(1); 150 subject.hasErrorContaining( 151 String.format( 152 MSGS.twoFactoryMethods(), 153 "test.SimpleComponent test.SimpleComponent.Parent.create()")) 154 .onSource(componentFile) 155 .onLineContaining("interface Factory"); 156 }); 157 } 158 } 159