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.testing.compile.CompilationSubject.assertThat; 20 import static dagger.internal.codegen.Compilers.CLASS_PATH_WITHOUT_GUAVA_OPTION; 21 import static dagger.internal.codegen.Compilers.compilerWithOptions; 22 23 import com.google.testing.compile.Compilation; 24 import com.google.testing.compile.Compiler; 25 import com.google.testing.compile.JavaFileObjects; 26 import dagger.testing.golden.GoldenFileRule; 27 import java.util.Collection; 28 import javax.tools.JavaFileObject; 29 import org.junit.Rule; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.Parameterized; 33 import org.junit.runners.Parameterized.Parameters; 34 35 @RunWith(Parameterized.class) 36 public class MapRequestRepresentationTest { 37 @Parameters(name = "{0}") parameters()38 public static Collection<Object[]> parameters() { 39 return CompilerMode.TEST_PARAMETERS; 40 } 41 42 @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule(); 43 44 private final CompilerMode compilerMode; 45 MapRequestRepresentationTest(CompilerMode compilerMode)46 public MapRequestRepresentationTest(CompilerMode compilerMode) { 47 this.compilerMode = compilerMode; 48 } 49 50 @Test mapBindings()51 public void mapBindings() throws Exception { 52 JavaFileObject mapModuleFile = JavaFileObjects.forSourceLines("test.MapModule", 53 "package test;", 54 "", 55 "import dagger.Module;", 56 "import dagger.Provides;", 57 "import dagger.multibindings.IntKey;", 58 "import dagger.multibindings.IntoMap;", 59 "import dagger.multibindings.LongKey;", 60 "import dagger.multibindings.Multibinds;", 61 "import java.util.Map;", 62 "", 63 "@Module", 64 "interface MapModule {", 65 " @Multibinds Map<String, String> stringMap();", 66 " @Provides @IntoMap @IntKey(0) static int provideInt() { return 0; }", 67 " @Provides @IntoMap @LongKey(0) static long provideLong0() { return 0; }", 68 " @Provides @IntoMap @LongKey(1) static long provideLong1() { return 1; }", 69 " @Provides @IntoMap @LongKey(2) static long provideLong2() { return 2; }", 70 "}"); 71 JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.TestComponent", 72 "package test;", 73 "", 74 "import dagger.Component;", 75 "import java.util.Map;", 76 "import javax.inject.Provider;", 77 "", 78 "@Component(modules = MapModule.class)", 79 "interface TestComponent {", 80 " Map<String, String> strings();", 81 " Map<String, Provider<String>> providerStrings();", 82 "", 83 " Map<Integer, Integer> ints();", 84 " Map<Integer, Provider<Integer>> providerInts();", 85 " Map<Long, Long> longs();", 86 " Map<Long, Provider<Long>> providerLongs();", 87 "}"); 88 89 Compilation compilation = daggerCompilerWithoutGuava().compile(mapModuleFile, componentFile); 90 assertThat(compilation).succeeded(); 91 assertThat(compilation) 92 .generatedSourceFile("test.DaggerTestComponent") 93 .hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent")); 94 } 95 96 @Test inaccessible()97 public void inaccessible() throws Exception { 98 JavaFileObject inaccessible = 99 JavaFileObjects.forSourceLines( 100 "other.Inaccessible", 101 "package other;", 102 "", 103 "class Inaccessible {}"); 104 JavaFileObject usesInaccessible = 105 JavaFileObjects.forSourceLines( 106 "other.UsesInaccessible", 107 "package other;", 108 "", 109 "import java.util.Map;", 110 "import javax.inject.Inject;", 111 "", 112 "public class UsesInaccessible {", 113 " @Inject UsesInaccessible(Map<Integer, Inaccessible> map) {}", 114 "}"); 115 116 JavaFileObject module = 117 JavaFileObjects.forSourceLines( 118 "other.TestModule", 119 "package other;", 120 "", 121 "import dagger.Module;", 122 "import dagger.multibindings.Multibinds;", 123 "import java.util.Map;", 124 "", 125 "@Module", 126 "public abstract class TestModule {", 127 " @Multibinds abstract Map<Integer, Inaccessible> ints();", 128 "}"); 129 JavaFileObject componentFile = 130 JavaFileObjects.forSourceLines( 131 "test.TestComponent", 132 "package test;", 133 "", 134 "import dagger.Component;", 135 "import java.util.Map;", 136 "import javax.inject.Provider;", 137 "import other.TestModule;", 138 "import other.UsesInaccessible;", 139 "", 140 "@Component(modules = TestModule.class)", 141 "interface TestComponent {", 142 " UsesInaccessible usesInaccessible();", 143 "}"); 144 145 Compilation compilation = 146 daggerCompilerWithoutGuava().compile(module, inaccessible, usesInaccessible, componentFile); 147 assertThat(compilation).succeeded(); 148 assertThat(compilation) 149 .generatedSourceFile("test.DaggerTestComponent") 150 .hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerTestComponent")); 151 } 152 153 @Test subcomponentOmitsInheritedBindings()154 public void subcomponentOmitsInheritedBindings() throws Exception { 155 JavaFileObject parent = 156 JavaFileObjects.forSourceLines( 157 "test.Parent", 158 "package test;", 159 "", 160 "import dagger.Component;", 161 "", 162 "@Component(modules = ParentModule.class)", 163 "interface Parent {", 164 " Child child();", 165 "}"); 166 JavaFileObject parentModule = 167 JavaFileObjects.forSourceLines( 168 "test.ParentModule", 169 "package test;", 170 "", 171 "import dagger.Module;", 172 "import dagger.Provides;", 173 "import dagger.multibindings.IntoMap;", 174 "import dagger.multibindings.StringKey;", 175 "", 176 "@Module", 177 "class ParentModule {", 178 " @Provides @IntoMap @StringKey(\"parent key\") Object parentKeyObject() {", 179 " return \"parent value\";", 180 " }", 181 "}"); 182 JavaFileObject child = 183 JavaFileObjects.forSourceLines( 184 "test.Child", 185 "package test;", 186 "", 187 "import dagger.Subcomponent;", 188 "import java.util.Map;", 189 "import java.util.Map;", 190 "", 191 "@Subcomponent", 192 "interface Child {", 193 " Map<String, Object> objectMap();", 194 "}"); 195 196 Compilation compilation = daggerCompilerWithoutGuava().compile(parent, parentModule, child); 197 assertThat(compilation).succeeded(); 198 assertThat(compilation) 199 .generatedSourceFile("test.DaggerParent") 200 .hasSourceEquivalentTo(goldenFileRule.goldenFile("test.DaggerParent")); 201 } 202 daggerCompilerWithoutGuava()203 private Compiler daggerCompilerWithoutGuava() { 204 return compilerWithOptions(compilerMode.javacopts()) 205 .withClasspath(CLASS_PATH_WITHOUT_GUAVA_OPTION); 206 } 207 } 208