• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.common.truth.Truth.assertAbout;
20 import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
21 
22 import com.google.auto.value.processor.AutoAnnotationProcessor;
23 import com.google.common.collect.ImmutableList;
24 import com.google.testing.compile.JavaFileObjects;
25 import java.util.Collection;
26 import javax.tools.JavaFileObject;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.junit.runners.Parameterized.Parameters;
31 
32 @RunWith(Parameterized.class)
33 public class MapKeyProcessorTest {
34   @Parameters(name = "{0}")
parameters()35   public static Collection<Object[]> parameters() {
36     return CompilerMode.TEST_PARAMETERS;
37   }
38 
39   private final CompilerMode compilerMode;
40 
MapKeyProcessorTest(CompilerMode compilerMode)41   public MapKeyProcessorTest(CompilerMode compilerMode) {
42     this.compilerMode = compilerMode;
43   }
44 
45   @Test
mapKeyCreatorFile()46   public void mapKeyCreatorFile() {
47     JavaFileObject enumKeyFile = JavaFileObjects.forSourceLines("test.PathKey",
48         "package test;",
49         "import dagger.MapKey;",
50         "import java.lang.annotation.Retention;",
51         "import static java.lang.annotation.RetentionPolicy.RUNTIME;",
52         "",
53         "@MapKey(unwrapValue = false)",
54         "@Retention(RUNTIME)",
55         "public @interface PathKey {",
56         "  PathEnum value();",
57         "  String relativePath() default \"Defaultpath\";",
58         "}");
59     JavaFileObject pathEnumFile = JavaFileObjects.forSourceLines("test.PathEnum",
60         "package test;",
61         "",
62         "public enum PathEnum {",
63         "    ADMIN,",
64         "    LOGIN;",
65         "}");
66     JavaFileObject generatedKeyCreator =
67         JavaFileObjects.forSourceLines(
68             "test.PathKeyCreator",
69             "package test;",
70             "",
71             GeneratedLines.generatedImports("import com.google.auto.value.AutoAnnotation;"),
72             "",
73             GeneratedLines.generatedAnnotations(),
74             "public final class PathKeyCreator {",
75             "  private PathKeyCreator() {}",
76             "",
77             "  @AutoAnnotation",
78             "  public static PathKey createPathKey(PathEnum value, String relativePath) {",
79             "    return new AutoAnnotation_PathKeyCreator_createPathKey(value, relativePath);",
80             "  }",
81             "}");
82     assertAbout(javaSources())
83         .that(ImmutableList.of(enumKeyFile, pathEnumFile))
84         .withCompilerOptions(compilerMode.javacopts())
85         .processedWith(new ComponentProcessor(), new AutoAnnotationProcessor())
86         .compilesWithoutError()
87         .and()
88         .generatesSources(generatedKeyCreator);
89   }
90 
91   @Test
nestedMapKeyCreatorFile()92   public void nestedMapKeyCreatorFile() {
93     JavaFileObject enumKeyFile = JavaFileObjects.forSourceLines("test.Container",
94         "package test;",
95         "import dagger.MapKey;",
96         "import java.lang.annotation.Retention;",
97         "import static java.lang.annotation.RetentionPolicy.RUNTIME;",
98         "",
99         "public interface Container {",
100         "@MapKey(unwrapValue = false)",
101         "@Retention(RUNTIME)",
102         "public @interface PathKey {",
103         "  PathEnum value();",
104         "  String relativePath() default \"Defaultpath\";",
105         "}",
106         "}");
107     JavaFileObject pathEnumFile = JavaFileObjects.forSourceLines("test.PathEnum",
108         "package test;",
109         "",
110         "public enum PathEnum {",
111         "    ADMIN,",
112         "    LOGIN;",
113         "}");
114     JavaFileObject generatedKeyCreator =
115         JavaFileObjects.forSourceLines(
116             "test.Container_PathKeyCreator",
117             "package test;",
118             "",
119             GeneratedLines.generatedImports("import com.google.auto.value.AutoAnnotation;"),
120             "",
121             GeneratedLines.generatedAnnotations(),
122             "public final class Container_PathKeyCreator {",
123             "  private Container_PathKeyCreator() {}",
124             "",
125             "  @AutoAnnotation",
126             "  public static Container.PathKey createPathKey("
127                 + "PathEnum value, String relativePath) {",
128             "    return new AutoAnnotation_Container_PathKeyCreator_createPathKey(",
129             "        value, relativePath);",
130             "  }",
131             "}");
132     assertAbout(javaSources())
133         .that(ImmutableList.of(enumKeyFile, pathEnumFile))
134         .withCompilerOptions(compilerMode.javacopts())
135         .processedWith(new ComponentProcessor(), new AutoAnnotationProcessor())
136         .compilesWithoutError()
137         .and()
138         .generatesSources(generatedKeyCreator);
139   }
140 }
141