package com.puppycrawl.tools.checkstyle.api; import static org.junit.Assert.*; import org.junit.Test; import antlr.RecognitionException; import antlr.Token; import antlr.TokenStreamException; import com.puppycrawl.tools.checkstyle.TreeWalker; import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaTokenTypes; public class TestAnnotationUtility { private enum Desc{with_annotation, without_annotation}; String fname = "MyTestClass.java"; DetailAST ast1 = createAST(Desc.with_annotation); DetailAST ast2 = createAST(Desc.without_annotation); private DetailAST createAST(Desc desc){ String[] fcontents = null; switch(desc){ case with_annotation: fcontents = new String[]{ "@TestAnnotation", "public class MyTestClass {", "}"}; break; case without_annotation: fcontents = new String[]{"public class MyTestClass {", "}"}; break; } try { return TreeWalker.parse(new FileContents(fname, fcontents)); } catch (RecognitionException e) { return null; } catch (TokenStreamException e) { return null; } } @Test public void testContainsAnnotation(){ assertTrue(AnnotationUtility.containsAnnotation(ast1, "TestAnnotation")); assertFalse(AnnotationUtility.containsAnnotation(ast1, "Not there")); assertTrue(AnnotationUtility.containsAnnotation(ast1)); assertFalse(AnnotationUtility.containsAnnotation(ast2)); try{ AnnotationUtility.containsAnnotation(null); fail(); }catch(NullPointerException npe){} } @Test public void testGetAnnotationHolder(){ assertNotNull(AnnotationUtility.getAnnotationHolder(ast1)); assertNotNull(AnnotationUtility.getAnnotationHolder(ast2)); try{ AnnotationUtility.getAnnotationHolder(null); fail(); }catch(NullPointerException npe){} } @Test public void testGetAnnotation(){ assertNotNull(AnnotationUtility.getAnnotation(ast1, "TestAnnotation")); assertNull(AnnotationUtility.getAnnotation(ast1, "FalseTestAnnotation")); assertNull(AnnotationUtility.getAnnotation(ast2, "TestAnnotation")); try{ AnnotationUtility.getAnnotation(null, "TestAnnotation"); fail(); }catch(NullPointerException npe){} try{ AnnotationUtility.getAnnotation(ast2, null); fail(); }catch(NullPointerException npe){} } @Test public void testAnnotatingWhat(){ try{ AnnotationUtility.annotatingWhat(null); fail(); }catch(NullPointerException npe){} assertNotNull(AnnotationUtility.annotatingWhat(AnnotationUtility.getAnnotation(ast1, "TestAnnotation"))); DetailAST not_an_annotation = new DetailAST(); not_an_annotation.initialize(new Token(TokenTypes.SLIST)); try{ AnnotationUtility.annotatingWhat(not_an_annotation); fail(); }catch(IllegalArgumentException iae){} } @Test public void testIsAnnotatingType(){ DetailAST ann = AnnotationUtility.getAnnotation(ast1, "TestAnnotation"); assertTrue(AnnotationUtility.isAnnotatingType(ann, GeneratedJavaTokenTypes.CLASS_DEF)); assertFalse(AnnotationUtility.isAnnotatingType(ann, GeneratedJavaTokenTypes.ARRAY_DECLARATOR)); try{ AnnotationUtility.isAnnotatingType(null, 0); fail(); }catch(NullPointerException npe){} DetailAST not_an_annotation = new DetailAST(); not_an_annotation.initialize(new Token(TokenTypes.SLIST)); try{ AnnotationUtility.isAnnotatingType(not_an_annotation, 50); fail(); }catch(IllegalArgumentException iae){} } }