/* * Copyright (C) 2017 The Dagger Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dagger.internal.codegen; import androidx.room.compiler.processing.util.Source; import com.google.common.collect.ImmutableList; import dagger.testing.compile.CompilerTests; import dagger.testing.golden.GoldenFileRule; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class MapRequestRepresentationWithGuavaTest { @Parameters(name = "{0}") public static ImmutableList parameters() { return CompilerMode.TEST_PARAMETERS; } @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule(); private final CompilerMode compilerMode; public MapRequestRepresentationWithGuavaTest(CompilerMode compilerMode) { this.compilerMode = compilerMode; } @Test public void mapBindings() throws Exception { Source mapModuleFile = CompilerTests.javaSource( "test.MapModule", "package test;", "", "import dagger.Module;", "import dagger.Provides;", "import dagger.multibindings.IntKey;", "import dagger.multibindings.IntoMap;", "import dagger.multibindings.LongKey;", "import dagger.multibindings.Multibinds;", "import java.util.Map;", "", "@Module", "interface MapModule {", " @Multibinds Map stringMap();", " @Provides @IntoMap @IntKey(0) static int provideInt() { return 0; }", " @Provides @IntoMap @LongKey(0) static long provideLong0() { return 0; }", " @Provides @IntoMap @LongKey(1) static long provideLong1() { return 1; }", " @Provides @IntoMap @LongKey(2) static long provideLong2() { return 2; }", "}"); Source subcomponentModuleFile = CompilerTests.javaSource( "test.SubcomponentMapModule", "package test;", "", "import dagger.Module;", "import dagger.Provides;", "import dagger.multibindings.IntKey;", "import dagger.multibindings.IntoMap;", "import dagger.multibindings.LongKey;", "import dagger.multibindings.Multibinds;", "import java.util.Map;", "", "@Module", "interface SubcomponentMapModule {", " @Provides @IntoMap @LongKey(3) static long provideLong3() { return 3; }", " @Provides @IntoMap @LongKey(4) static long provideLong4() { return 4; }", " @Provides @IntoMap @LongKey(5) static long provideLong5() { return 5; }", "}"); Source componentFile = CompilerTests.javaSource( "test.TestComponent", "package test;", "", "import dagger.Component;", "import java.util.Map;", "import javax.inject.Provider;", "", "@Component(modules = MapModule.class)", "interface TestComponent {", " Map strings();", " Map> providerStrings();", "", " Map ints();", " Map> providerInts();", " Map longs();", " Map> providerLongs();", "", " Sub sub();", "}"); Source subcomponent = CompilerTests.javaSource( "test.Sub", "package test;", "", "import dagger.Subcomponent;", "import java.util.Map;", "import javax.inject.Provider;", "", "@Subcomponent(modules = SubcomponentMapModule.class)", "interface Sub {", " Map longs();", " Map> providerLongs();", "}"); CompilerTests.daggerCompiler(mapModuleFile, componentFile, subcomponentModuleFile, subcomponent) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent")); }); } @Test public void inaccessible() throws Exception { Source inaccessible = CompilerTests.javaSource( "other.Inaccessible", "package other;", "", "class Inaccessible {}"); Source usesInaccessible = CompilerTests.javaSource( "other.UsesInaccessible", "package other;", "", "import java.util.Map;", "import javax.inject.Inject;", "", "public class UsesInaccessible {", " @Inject UsesInaccessible(Map map) {}", "}"); Source module = CompilerTests.javaSource( "other.TestModule", "package other;", "", "import dagger.Module;", "import dagger.multibindings.Multibinds;", "import java.util.Map;", "", "@Module", "public abstract class TestModule {", " @Multibinds abstract Map ints();", "}"); Source componentFile = CompilerTests.javaSource( "test.TestComponent", "package test;", "", "import dagger.Component;", "import java.util.Map;", "import javax.inject.Provider;", "import other.TestModule;", "import other.UsesInaccessible;", "", "@Component(modules = TestModule.class)", "interface TestComponent {", " UsesInaccessible usesInaccessible();", "}"); CompilerTests.daggerCompiler(module, inaccessible, usesInaccessible, componentFile) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent")); }); } @Test public void subcomponentOmitsInheritedBindings() throws Exception { Source parent = CompilerTests.javaSource( "test.Parent", "package test;", "", "import dagger.Component;", "", "@Component(modules = ParentModule.class)", "interface Parent {", " Child child();", "}"); Source parentModule = CompilerTests.javaSource( "test.ParentModule", "package test;", "", "import dagger.Module;", "import dagger.Provides;", "import dagger.multibindings.IntoMap;", "import dagger.multibindings.StringKey;", "", "@Module", "class ParentModule {", " @Provides @IntoMap @StringKey(\"parent key\") Object parentKeyObject() {", " return \"parent value\";", " }", "}"); Source child = CompilerTests.javaSource( "test.Child", "package test;", "", "import dagger.Subcomponent;", "import java.util.Map;", "", "@Subcomponent", "interface Child {", " Map objectMap();", "}"); CompilerTests.daggerCompiler(parent, parentModule, child) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerParent")); }); } @Test public void productionComponents() throws Exception { Source mapModuleFile = CompilerTests.javaSource( "test.MapModule", "package test;", "", "import dagger.Module;", "import dagger.multibindings.Multibinds;", "import java.util.Map;", "", "@Module", "interface MapModule {", " @Multibinds Map stringMap();", "}"); Source componentFile = CompilerTests.javaSource( "test.TestComponent", "package test;", "", "import com.google.common.util.concurrent.ListenableFuture;", "import dagger.producers.ProductionComponent;", "import java.util.Map;", "", "@ProductionComponent(modules = MapModule.class)", "interface TestComponent {", " ListenableFuture> stringMap();", "}"); CompilerTests.daggerCompiler(mapModuleFile, componentFile) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerTestComponent")); }); } }