View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.utils;
21  
22  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.lang.reflect.InvocationTargetException;
27  
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  
34  public class AnnotationUtilityTest {
35  
36      @Test
37      public void testIsProperUtilsClass() throws ReflectiveOperationException {
38          try {
39              isUtilsClassHasPrivateConstructor(AnnotationUtility.class, true);
40              Assert.fail("Exception is expected");
41          }
42          catch (InvocationTargetException ex) {
43              assertEquals("Invalid exception message",
44                      "do not instantiate.", ex.getCause().getMessage());
45          }
46      }
47  
48      @Test
49      public void testContainsAnnotationNull() {
50          try {
51              AnnotationUtility.containsAnnotation(null);
52              Assert.fail("IllegalArgumentException is expected");
53          }
54          catch (IllegalArgumentException ex) {
55              assertEquals("Invalid exception message",
56                      "the ast is null", ex.getMessage());
57          }
58      }
59  
60      @Test
61      public void testContainsAnnotationNull2() {
62          try {
63              AnnotationUtility.containsAnnotation(null, "");
64              Assert.fail("IllegalArgumentException is expected");
65          }
66          catch (IllegalArgumentException ex) {
67              assertEquals("Invalid exception message",
68                      "the ast is null", ex.getMessage());
69          }
70      }
71  
72      @Test
73      public void testContainsAnnotationFalse() {
74          final DetailAST ast = new DetailAST();
75          ast.setType(1);
76          Assert.assertFalse("AnnotationUtility should not contain " + ast,
77                  AnnotationUtility.containsAnnotation(ast));
78      }
79  
80      @Test
81      public void testContainsAnnotationFalse2() {
82          final DetailAST ast = new DetailAST();
83          ast.setType(1);
84          final DetailAST ast2 = new DetailAST();
85          ast2.setType(TokenTypes.MODIFIERS);
86          ast.addChild(ast2);
87          Assert.assertFalse("AnnotationUtility should not contain " + ast,
88                  AnnotationUtility.containsAnnotation(ast));
89      }
90  
91      @Test
92      public void testContainsAnnotationTrue() {
93          final DetailAST ast = new DetailAST();
94          ast.setType(1);
95          final DetailAST ast2 = new DetailAST();
96          ast2.setType(TokenTypes.MODIFIERS);
97          ast.addChild(ast2);
98          final DetailAST ast3 = new DetailAST();
99          ast3.setType(TokenTypes.ANNOTATION);
100         ast2.addChild(ast3);
101         assertTrue("AnnotationUtility should contain " + ast,
102                 AnnotationUtility.containsAnnotation(ast));
103     }
104 
105     @Test
106     public void testAnnotationHolderNull() {
107         try {
108             AnnotationUtility.getAnnotationHolder(null);
109             Assert.fail("IllegalArgumentException is expected");
110         }
111         catch (IllegalArgumentException ex) {
112             assertEquals("Invalid exception message",
113                     "the ast is null", ex.getMessage());
114         }
115     }
116 
117     @Test
118     public void testAnnotationNull() {
119         try {
120             AnnotationUtility.getAnnotation(null, null);
121             Assert.fail("IllegalArgumentException is expected");
122         }
123         catch (IllegalArgumentException ex) {
124             assertEquals("Invalid exception message",
125                     "the ast is null", ex.getMessage());
126         }
127     }
128 
129     @Test
130     public void testAnnotationNull2() {
131         try {
132             AnnotationUtility.getAnnotation(new DetailAST(), null);
133             Assert.fail("IllegalArgumentException is expected");
134         }
135         catch (IllegalArgumentException ex) {
136             assertEquals("Invalid exception message",
137                     "the annotation is null", ex.getMessage());
138         }
139     }
140 
141     @Test
142     public void testAnnotationEmpty() {
143         try {
144             AnnotationUtility.getAnnotation(new DetailAST(), "");
145             Assert.fail("IllegalArgumentException is expected");
146         }
147         catch (IllegalArgumentException ex) {
148             assertEquals("Invalid exception message",
149                     "the annotation is empty or spaces", ex.getMessage());
150         }
151     }
152 
153     @Test
154     public void testContainsAnnotationWithNull() {
155         try {
156             AnnotationUtility.getAnnotation(null, "");
157             Assert.fail("IllegalArgumentException is expected");
158         }
159         catch (IllegalArgumentException ex) {
160             assertEquals("Invalid exception message",
161                     "the ast is null", ex.getMessage());
162         }
163     }
164 
165     @Test
166     public void testContainsAnnotation() {
167         final DetailAST astForTest = new DetailAST();
168         astForTest.setType(TokenTypes.PACKAGE_DEF);
169         final DetailAST child = new DetailAST();
170         final DetailAST annotations = new DetailAST();
171         final DetailAST annotation = new DetailAST();
172         final DetailAST annotationNameHolder = new DetailAST();
173         final DetailAST annotationName = new DetailAST();
174         annotations.setType(TokenTypes.ANNOTATIONS);
175         annotation.setType(TokenTypes.ANNOTATION);
176         annotationNameHolder.setType(TokenTypes.AT);
177         annotationName.setText("Annotation");
178 
179         annotationNameHolder.setNextSibling(annotationName);
180         annotation.setFirstChild(annotationNameHolder);
181         annotations.setFirstChild(annotation);
182         child.setNextSibling(annotations);
183         astForTest.setFirstChild(child);
184 
185         assertTrue("Annotation should contain " + astForTest,
186                 AnnotationUtility.containsAnnotation(astForTest, "Annotation"));
187     }
188 
189     @Test
190     public void testContainsAnnotationWithComment() {
191         final DetailAST astForTest = new DetailAST();
192         astForTest.setType(TokenTypes.PACKAGE_DEF);
193         final DetailAST child = new DetailAST();
194         final DetailAST annotations = new DetailAST();
195         final DetailAST annotation = new DetailAST();
196         final DetailAST annotationNameHolder = new DetailAST();
197         final DetailAST annotationName = new DetailAST();
198         final DetailAST comment = new DetailAST();
199         annotations.setType(TokenTypes.ANNOTATIONS);
200         annotation.setType(TokenTypes.ANNOTATION);
201         annotationNameHolder.setType(TokenTypes.AT);
202         comment.setType(TokenTypes.BLOCK_COMMENT_BEGIN);
203         annotationName.setText("Annotation");
204 
205         annotationNameHolder.setNextSibling(annotationName);
206         annotation.setFirstChild(comment);
207         comment.setNextSibling(annotationNameHolder);
208         annotations.setFirstChild(annotation);
209         child.setNextSibling(annotations);
210         astForTest.setFirstChild(child);
211 
212         assertTrue("Annotation should contain " + astForTest,
213                 AnnotationUtility.containsAnnotation(astForTest, "Annotation"));
214     }
215 }