View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.annotation;
21  
22  import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
23  import static org.truth0.Truth.ASSERT;
24  
25  import javax.tools.JavaFileObject;
26  
27  import org.junit.Test;
28  
29  import com.google.testing.compile.JavaFileObjects;
30  
31  public class AnnotationValidatorTest {
32  
33    private static final String QUALIFIER_WHICH_IS_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION = "package org.grouplens.grapht.annotation;"
34            + "import java.lang.annotation.Documented;"
35            + "import java.lang.annotation.Retention;"
36            + "import java.lang.annotation.RetentionPolicy;"
37            + "import javax.inject.Qualifier;"
38            + "@Documented "
39            + "@Retention(RetentionPolicy.RUNTIME)"
40            + "@Qualifier "
41            + "public @interface QualifierWhichIsDocumentedAndHasRuntimeRetention {}";
42  
43    private static final String QUALIFIER_WHICH_IS_DOCUMENTEND_AND_HAS_NO_RETENTION = "package org.grouplens.grapht.annotation;"
44            + "import java.lang.annotation.Documented;"
45            + "import java.lang.annotation.RetentionPolicy;"
46            + "import javax.inject.Qualifier;"
47            + "@Documented "
48            + "@Qualifier "
49            + "public @interface QualifierWhichIsDocumentedAndHasNoRetention {}";
50  
51    private static final String QUALIFIER_WHICH_IS_NOT_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION = "package org.grouplens.grapht.annotation;"
52            + "import java.lang.annotation.Retention;"
53            + "import java.lang.annotation.RetentionPolicy;"
54            + "import javax.inject.Qualifier;"
55            + "@Retention(RetentionPolicy.RUNTIME)"
56            + "@Qualifier "
57            + "public @interface QualifierWhichIsNotDocumentedAndHasRuntimeRetention {}";
58  
59    private static final String ATTRIBUTE_WHICH_IS_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION = "package org.grouplens.grapht.annotation;"
60            + "import java.lang.annotation.Documented;"
61            + "import java.lang.annotation.Retention;"
62            + "import java.lang.annotation.RetentionPolicy;"
63            + "import org.grouplens.grapht.annotation.Attribute;"
64            + "@Documented "
65            + "@Retention(RetentionPolicy.RUNTIME)"
66            + "@Attribute "
67            + "public @interface AttributeWhichIsDocumentedAndHasRuntimeRetention {}";
68  
69    private static final String ATTRIBUTE_WHICH_IS_DOCUMENTEND_AND_HAS_NO_RETENTION = "package org.grouplens.grapht.annotation;"
70            + "import java.lang.annotation.Documented;"
71            + "import java.lang.annotation.RetentionPolicy;"
72            + "import org.grouplens.grapht.annotation.Attribute;"
73            + "@Documented "
74            + "@Attribute "
75            + "public @interface AttributeWhichIsDocumentedAndHasNoRetention {}";
76  
77    private static final String ATTRIBUTE_WHICH_IS_NOT_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION = "package org.grouplens.grapht.annotation;"
78            + "import java.lang.annotation.Retention;"
79            + "import java.lang.annotation.RetentionPolicy;"
80            + "import org.grouplens.grapht.annotation.Attribute;"
81            + "@Retention(RetentionPolicy.RUNTIME)"
82            + "@Attribute "
83            + "public @interface AttributeWhichIsNotDocumentedAndHasRuntimeRetention {}";
84  
85    private static final String ALIAS_WHICH_IS_QUALIFIED_HAS_RUNTIME_RETENTION_AND_IS_DOCUMENTED = "package org.grouplens.grapht.annotation;"
86        + "import java.lang.annotation.Documented;"
87        + "import java.lang.annotation.Retention;"
88        + "import java.lang.annotation.RetentionPolicy;"
89        + "import javax.inject.Qualifier;"
90        + "import org.grouplens.grapht.annotation.AliasFor;"
91        + "@Documented "
92        + "@Retention(RetentionPolicy.RUNTIME) "
93        + "@Qualifier "
94        + "@AliasFor(Qualifier.class) "
95        + "public @interface AliasWhichIsQualifiedHasRuntimeRetentionAndIsDocumented {}";
96  
97    private static final String ALIAS_WHICH_IS_QUALIFIED_HAS_RUNTIME_RETENTION_IS_DOCUMENTED_AND_HAS_ALLOW_UNQUALIFIED_MATCH = "package org.grouplens.grapht.annotation;"
98        + "import java.lang.annotation.Documented;"
99        + "import java.lang.annotation.Retention;"
100       + "import java.lang.annotation.RetentionPolicy;"
101       + "import javax.inject.Qualifier;"
102       + "import org.grouplens.grapht.annotation.AliasFor;"
103       + "import org.grouplens.grapht.annotation.AllowUnqualifiedMatch;"
104       + "@Documented "
105       + "@Retention(RetentionPolicy.RUNTIME) "
106       + "@Qualifier "
107       + "@AllowUnqualifiedMatch "
108       + "@AliasFor(Qualifier.class) "
109       + "public @interface AliasWhichIsQualifiedHasRuntimeRetentionIsDocumentedAndAllowsUnqualifiedMatch {}";
110 
111   private static final String ALIAS_WHICH_IS_NOT_QUALIFIED_HAS_RUNTIME_RETENTION_AND_IS_DOCUMENTED = "package org.grouplens.grapht.annotation;"
112       + "import java.lang.annotation.Documented;"
113       + "import java.lang.annotation.Retention;"
114       + "import java.lang.annotation.RetentionPolicy;"
115       + "import org.grouplens.grapht.annotation.AliasFor;"
116       + "@Documented "
117       + "@Retention(RetentionPolicy.RUNTIME) "
118       + "@AliasFor(Documented.class) "
119       + "public @interface AliasWhichIsNotQualifiedHasRuntimeRetentionAndIsDocumented {}";
120 
121 
122   @Test
123   public final void whenQualifierAndIsDocumentendAndRetentionIsNotPresentThenShouldNotCompile() {
124     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.QualifierWhichIsDocumentedAndHasNoRetention", QUALIFIER_WHICH_IS_DOCUMENTEND_AND_HAS_NO_RETENTION);
125     ASSERT.about(javaSource())
126           .that(annotation)
127           .processedWith(new AnnotationValidator())
128           .failsToCompile();
129   }
130 
131   @Test
132   public final void whenQualifierAndIsDocumentendAndRetentionIsRuntimeThenShouldCompile() {
133     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.QualifierWhichIsDocumentedAndHasRuntimeRetention", QUALIFIER_WHICH_IS_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION);
134     ASSERT.about(javaSource())
135            .that(annotation)
136            .processedWith(new AnnotationValidator())
137            .compilesWithoutError();
138   }
139 
140   @Test
141   public final void whenQualifierAndIsNotDocumentendAndRetentionIsRuntimeThenShouldCompile() {
142     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.QualifierWhichIsNotDocumentedAndHasRuntimeRetention", QUALIFIER_WHICH_IS_NOT_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION);
143     ASSERT.about(javaSource())
144           .that(annotation)
145           .processedWith(new AnnotationValidator())
146           .compilesWithoutError();
147   }
148 
149   @Test
150   public final void whenAttributeAndIsDocumentendAndRetentionIsNotPresentThenShouldNotCompile() {
151     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.AttributeWhichIsDocumentedAndHasNoRetention", ATTRIBUTE_WHICH_IS_DOCUMENTEND_AND_HAS_NO_RETENTION);
152     ASSERT.about(javaSource())
153           .that(annotation)
154           .processedWith(new AnnotationValidator())
155           .failsToCompile();
156   }
157 
158   @Test
159   public final void whenAttributeAndIsDocumentendAndRetentionIsRuntimeThenShouldCompile() {
160     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.AttributeWhichIsDocumentedAndHasRuntimeRetention", ATTRIBUTE_WHICH_IS_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION);
161     ASSERT.about(javaSource())
162            .that(annotation)
163            .processedWith(new AnnotationValidator())
164            .compilesWithoutError();
165   }
166 
167   @Test
168   public final void whenAttributeAndIsNotDocumentendAndRetentionIsRuntimeThenShouldCompile() {
169     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.AttributeWhichIsNotDocumentedAndHasRuntimeRetention", ATTRIBUTE_WHICH_IS_NOT_DOCUMENTEND_AND_HAS_RUNTIME_RETENTION);
170     ASSERT.about(javaSource())
171           .that(annotation)
172           .processedWith(new AnnotationValidator())
173           .compilesWithoutError();
174   }
175 
176   @Test
177   public final void whenAliasAndIsDocumentendAndRetentionIsRuntimeAndIsQualifiedThenShouldCompile() {
178     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.AliasWhichIsQualifiedHasRuntimeRetentionAndIsDocumented", ALIAS_WHICH_IS_QUALIFIED_HAS_RUNTIME_RETENTION_AND_IS_DOCUMENTED);
179     ASSERT.about(javaSource())
180           .that(annotation)
181           .processedWith(new AnnotationValidator())
182           .compilesWithoutError();
183   }
184 
185   @Test
186   public final void whenAliasAndIsDocumentendAndRetentionIsRuntimeAndIsQualifiedAndAllowsUnqualifiedMatchesThenShouldNotCompile() {
187     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.AliasWhichIsQualifiedHasRuntimeRetentionIsDocumentedAndAllowsUnqualifiedMatch", ALIAS_WHICH_IS_QUALIFIED_HAS_RUNTIME_RETENTION_IS_DOCUMENTED_AND_HAS_ALLOW_UNQUALIFIED_MATCH);
188     ASSERT.about(javaSource())
189           .that(annotation)
190           .processedWith(new AnnotationValidator())
191           .failsToCompile();
192   }
193 
194   @Test
195   public final void whenAliasAndIsDocumentendAndRetentionIsRuntimeAndIsNotQualifiedThenShouldNotCompile() {
196     final JavaFileObject annotation = JavaFileObjects.forSourceString("org.grouplens.grapht.annoation.AliasWhichIsNotQualifiedHasRuntimeRetentionAndIsDocumented", ALIAS_WHICH_IS_NOT_QUALIFIED_HAS_RUNTIME_RETENTION_AND_IS_DOCUMENTED);
197     ASSERT.about(javaSource())
198           .that(annotation)
199           .processedWith(new AnnotationValidator())
200           .failsToCompile();
201   }
202 }