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