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