• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.android.processor;
18 
19 import static com.google.common.truth.Truth8.assertThat;
20 import static com.google.testing.compile.CompilationSubject.assertThat;
21 import static com.google.testing.compile.Compiler.javac;
22 import static javax.tools.StandardLocation.CLASS_OUTPUT;
23 
24 import com.google.testing.compile.Compilation;
25 import com.google.testing.compile.JavaFileObjects;
26 import javax.tools.JavaFileObject;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 @RunWith(JUnit4.class)
32 public final class AndroidProcessorTest {
33   @Test
generatedProguardFile()34   public void generatedProguardFile() {
35     JavaFileObject module =
36         JavaFileObjects.forSourceLines(
37             "test.TestModule",
38             "package test;",
39             "",
40             "import dagger.android.AndroidInjectionKey;",
41             "import dagger.Module;",
42             "import dagger.multibindings.IntoMap;",
43             "import dagger.Provides;",
44             "",
45             "@Module",
46             "class TestModule {",
47             "  @Provides",
48             "  @IntoMap",
49             "  @AndroidInjectionKey(\"test.TestActivity\")",
50             "  static int i() { ",
51             "    return 1;",
52             "  }",
53             "}");
54     Compilation enabled =
55         javac()
56             .withProcessors(new AndroidProcessor())
57             .withOptions("-Adagger.android.experimentalUseStringKeys=true")
58             .compile(module);
59     assertThat(enabled).succeeded();
60     assertThat(enabled)
61         .generatedFile(CLASS_OUTPUT, "META-INF/proguard/dagger.android.AndroidInjectionKeys");
62 
63     Compilation disabled =
64         javac()
65             .withProcessors(new AndroidProcessor())
66             .withOptions("-Adagger.android.experimentalUseStringKeys=false")
67             .compile(module);
68     assertThat(disabled).succeeded();
69     assertThat(
70             disabled.generatedFile(
71                 CLASS_OUTPUT, "META-INF/proguard/dagger.android.AndroidInjectionKeys"))
72         .isEmpty();
73 
74     Compilation noFlag = javac().withProcessors(new AndroidProcessor()).compile(module);
75     assertThat(noFlag).succeeded();
76     assertThat(
77             noFlag.generatedFile(
78                 CLASS_OUTPUT, "META-INF/proguard/dagger.android.AndroidInjectionKeys"))
79         .isEmpty();
80   }
81 }
82