1 /*
2  * Copyright (C) 2018 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.TestUtils.endsWithMessage;
20 import static javax.tools.Diagnostic.Kind.ERROR;
21 
22 import androidx.room.compiler.processing.util.Source;
23 import com.google.common.collect.ImmutableMap;
24 import dagger.spi.model.BindingGraph;
25 import dagger.spi.model.BindingGraphPlugin;
26 import dagger.spi.model.DiagnosticReporter;
27 import dagger.testing.compile.CompilerTests;
28 import java.util.regex.Pattern;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 
33 /** Tests for -Adagger.pluginsVisitFullBindingGraph. */
34 @RunWith(JUnit4.class)
35 public final class PluginsVisitFullBindingGraphTest {
36   private static final Source MODULE_WITHOUT_ERRORS =
37       CompilerTests.javaSource(
38           "test.ModuleWithoutErrors",
39           "package test;",
40           "",
41           "import dagger.Binds;",
42           "import dagger.Module;",
43           "",
44           "@Module",
45           "interface ModuleWithoutErrors {",
46           "  @Binds Object object(String string);",
47           "}");
48 
49   private static final Source MODULE_WITH_ERRORS =
50       CompilerTests.javaSource(
51           "test.ModuleWithErrors",
52           "package test;",
53           "",
54           "import dagger.Binds;",
55           "import dagger.Module;",
56           "",
57           "@Module",
58           "interface ModuleWithErrors {",
59           "  @Binds Object object1(String string);",
60           "  @Binds Object object2(Long l);",
61           "}");
62 
63   private static final Pattern PLUGIN_ERROR_MESSAGE =
64       endsWithMessage(
65           "[dagger.internal.codegen.PluginsVisitFullBindingGraphTest.ErrorPlugin] Error!");
66 
67   @Test
testNoFlags()68   public void testNoFlags() {
69     CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
70         .withBindingGraphPlugins(ErrorPlugin::new)
71         .compile(subject -> subject.hasErrorCount(0));
72   }
73 
74   @Test
testWithVisitPlugins()75   public void testWithVisitPlugins() {
76     CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
77         .withProcessingOptions(ImmutableMap.of("dagger.pluginsVisitFullBindingGraphs", "Enabled"))
78         .withBindingGraphPlugins(ErrorPlugin::new)
79         .compile(
80             subject -> {
81               subject.hasErrorCount(1);
82               subject
83                   .hasErrorContainingMatch(PLUGIN_ERROR_MESSAGE.toString())
84                   .onSource(MODULE_WITH_ERRORS)
85                   .onLineContaining("interface ModuleWithErrors");
86             });
87   }
88 
89   @Test
testWithValidationNone()90   public void testWithValidationNone() {
91     CompilerTests.daggerCompiler(MODULE_WITHOUT_ERRORS)
92         .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "NONE"))
93         .withBindingGraphPlugins(ErrorPlugin::new)
94         .compile(subject -> subject.hasErrorCount(0));
95   }
96 
97   @Test
testWithValidationError()98   public void testWithValidationError() {
99     // Test that pluginsVisitFullBindingGraph is enabled with fullBindingGraphValidation.
100     CompilerTests.daggerCompiler(MODULE_WITHOUT_ERRORS)
101         .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
102         .withBindingGraphPlugins(ErrorPlugin::new)
103         .compile(
104             subject -> {
105               subject.hasErrorCount(1);
106               subject
107                   .hasErrorContainingMatch(PLUGIN_ERROR_MESSAGE.toString())
108                   .onSource(MODULE_WITHOUT_ERRORS)
109                   .onLineContaining("interface ModuleWithoutErrors");
110             });
111   }
112 
113   @Test
testWithValidationErrorAndVisitPlugins()114   public void testWithValidationErrorAndVisitPlugins() {
115     CompilerTests.daggerCompiler(MODULE_WITHOUT_ERRORS)
116         .withProcessingOptions(
117             ImmutableMap.of(
118                 "dagger.fullBindingGraphValidation", "ERROR",
119                 "dagger.pluginsVisitFullBindingGraphs", "Enabled"))
120         .withBindingGraphPlugins(ErrorPlugin::new)
121         .compile(
122             subject -> {
123               subject.hasErrorCount(1);
124               subject
125                   .hasErrorContainingMatch(PLUGIN_ERROR_MESSAGE.toString())
126                   .onSource(MODULE_WITHOUT_ERRORS)
127                   .onLineContaining("interface ModuleWithoutErrors");
128             });
129   }
130 
131   /** A test plugin that just reports each component with the given {@link Diagnostic.Kind}. */
132   private static final class ErrorPlugin implements BindingGraphPlugin {
133     @Override
visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)134     public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
135       diagnosticReporter.reportComponent(ERROR, bindingGraph.rootComponentNode(), "Error!");
136     }
137   }
138 }
139