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