1 /* 2 * Copyright (C) 2017 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 com.google.common.collect.Sets.cartesianProduct; 20 import static com.google.common.collect.Sets.immutableEnumSet; 21 import static com.google.testing.compile.CompilationSubject.assertThat; 22 import static dagger.internal.codegen.CompilerMode.DEFAULT_MODE; 23 import static dagger.internal.codegen.CompilerMode.FAST_INIT_MODE; 24 import static dagger.internal.codegen.GeneratedLines.GENERATED_CODE_ANNOTATIONS; 25 import static dagger.internal.codegen.GeneratedLines.IMPORT_GENERATED_ANNOTATION; 26 import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.SUBCOMPONENT_BUILDER; 27 import static dagger.internal.codegen.binding.ComponentCreatorAnnotation.SUBCOMPONENT_FACTORY; 28 29 import com.google.common.collect.ImmutableList; 30 import com.google.common.collect.Iterables; 31 import com.google.testing.compile.Compilation; 32 import dagger.internal.codegen.binding.ComponentCreatorAnnotation; 33 import java.util.Collection; 34 import java.util.List; 35 import java.util.Set; 36 import javax.tools.JavaFileObject; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.junit.runners.Parameterized; 40 import org.junit.runners.Parameterized.Parameters; 41 42 @RunWith(Parameterized.class) 43 public class SubcomponentCreatorRequestFulfillmentTest extends ComponentCreatorTestHelper { 44 @Parameters(name = "compilerMode={0}, creatorKind={1}") parameters()45 public static Collection<Object[]> parameters() { 46 Set<List<Object>> params = 47 cartesianProduct( 48 immutableEnumSet(DEFAULT_MODE, FAST_INIT_MODE), 49 immutableEnumSet(SUBCOMPONENT_FACTORY, SUBCOMPONENT_BUILDER)); 50 return ImmutableList.copyOf(Iterables.transform(params, Collection::toArray)); 51 } 52 SubcomponentCreatorRequestFulfillmentTest( CompilerMode compilerMode, ComponentCreatorAnnotation componentCreatorAnnotation)53 public SubcomponentCreatorRequestFulfillmentTest( 54 CompilerMode compilerMode, ComponentCreatorAnnotation componentCreatorAnnotation) { 55 super(compilerMode, componentCreatorAnnotation); 56 } 57 58 @Test testInlinedSubcomponentCreators_componentMethod()59 public void testInlinedSubcomponentCreators_componentMethod() { 60 JavaFileObject subcomponent = 61 preprocessedJavaFile( 62 "test.Sub", 63 "package test;", 64 "", 65 "import dagger.Subcomponent;", 66 "", 67 "@Subcomponent", 68 "interface Sub {", 69 " @Subcomponent.Builder", 70 " interface Builder {", 71 " Sub build();", 72 " }", 73 "}"); 74 JavaFileObject usesSubcomponent = 75 preprocessedJavaFile( 76 "test.UsesSubcomponent", 77 "package test;", 78 "", 79 "import javax.inject.Inject;", 80 "", 81 "class UsesSubcomponent {", 82 " @Inject UsesSubcomponent(Sub.Builder subBuilder) {}", 83 "}"); 84 JavaFileObject component = 85 preprocessedJavaFile( 86 "test.C", 87 "package test;", 88 "", 89 "import dagger.Component;", 90 "", 91 "@Component", 92 "interface C {", 93 " Sub.Builder sBuilder();", 94 " UsesSubcomponent usesSubcomponent();", 95 "}"); 96 97 JavaFileObject generatedComponent = 98 preprocessedJavaFile( 99 "test.DaggerC", 100 "package test;", 101 "", 102 IMPORT_GENERATED_ANNOTATION, 103 "", 104 GENERATED_CODE_ANNOTATIONS, 105 "final class DaggerC implements C {", 106 " @Override", 107 " public Sub.Builder sBuilder() {", 108 " return new SubBuilder();", 109 " }", 110 "", 111 " @Override", 112 " public UsesSubcomponent usesSubcomponent() {", 113 " return new UsesSubcomponent(new SubBuilder());", 114 " }", 115 "", 116 " private final class SubBuilder implements Sub.Builder {", 117 " @Override", 118 " public Sub build() {", 119 " return new SubImpl();", 120 " }", 121 " }", 122 "", 123 " private final class SubImpl implements Sub {", 124 " private SubImpl() {}", 125 " }", 126 "}"); 127 128 Compilation compilation = compile(subcomponent, usesSubcomponent, component); 129 assertThat(compilation).succeeded(); 130 assertThat(compilation) 131 .generatedSourceFile("test.DaggerC") 132 .containsElementsIn(generatedComponent); 133 } 134 } 135