1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 22 package com.github.javaparser.printer.lexicalpreservation.transformations.ast.body; 23 24 import com.github.javaparser.ast.Modifier; 25 import com.github.javaparser.ast.NodeList; 26 import com.github.javaparser.ast.body.AnnotationMemberDeclaration; 27 import com.github.javaparser.ast.expr.IntegerLiteralExpr; 28 import com.github.javaparser.ast.expr.Name; 29 import com.github.javaparser.ast.expr.NormalAnnotationExpr; 30 import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; 31 import org.junit.Test; 32 33 import java.util.EnumSet; 34 35 import static com.github.javaparser.utils.Utils.EOL; 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.assertTrue; 38 39 /** 40 * Transforming AnnotationMemberDeclaration and verifying the LexicalPreservation works as expected. 41 */ 42 public class AnnotationMemberDeclarationTransformationsTest extends AbstractLexicalPreservingTest { 43 44 protected AnnotationMemberDeclaration consider(String code) { 45 considerCode("@interface AD { " + code + " }"); 46 return cu.getAnnotationDeclarationByName("AD").get().getMember(0).asAnnotationMemberDeclaration(); 47 } 48 49 // Name 50 51 @Test 52 public void changingName() { 53 AnnotationMemberDeclaration md = consider("int foo();"); 54 md.setName("bar"); 55 assertTransformedToString("int bar();", md); 56 } 57 58 // Type 59 60 @Test 61 public void changingType() { 62 AnnotationMemberDeclaration md = consider("int foo();"); 63 md.setType("String"); 64 assertTransformedToString("String foo();", md); 65 } 66 67 // Modifiers 68 69 @Test 70 public void addingModifiers() { 71 AnnotationMemberDeclaration md = consider("int foo();"); 72 md.setModifiers(EnumSet.of(Modifier.PUBLIC)); 73 assertTransformedToString("public int foo();", md); 74 } 75 76 @Test 77 public void removingModifiers() { 78 AnnotationMemberDeclaration md = consider("public int foo();"); 79 md.setModifiers(EnumSet.noneOf(Modifier.class)); 80 assertTransformedToString("int foo();", md); 81 } 82 83 @Test 84 public void replacingModifiers() { 85 AnnotationMemberDeclaration md = consider("public int foo();"); 86 md.setModifiers(EnumSet.of(Modifier.PROTECTED)); 87 assertTransformedToString("protected int foo();", md); 88 } 89 90 // Default value 91 92 @Test 93 public void addingDefaultValue() { 94 AnnotationMemberDeclaration md = consider("int foo();"); 95 md.setDefaultValue(new IntegerLiteralExpr("10")); 96 assertTransformedToString("int foo() default 10;", md); 97 } 98 99 @Test 100 public void removingDefaultValue() { 101 AnnotationMemberDeclaration md = consider("int foo() default 10;"); 102 assertEquals(true, md.getDefaultValue().get().remove()); 103 assertTransformedToString("int foo();", md); 104 } 105 106 @Test 107 public void replacingDefaultValue() { 108 AnnotationMemberDeclaration md = consider("int foo() default 10;"); 109 md.setDefaultValue(new IntegerLiteralExpr("11")); 110 assertTransformedToString("int foo() default 11;", md); 111 } 112 113 // Annotations 114 115 @Test 116 public void addingAnnotation() { 117 AnnotationMemberDeclaration it = consider("int foo();"); 118 it.addAnnotation("myAnno"); 119 assertTransformedToString("@myAnno()" + EOL + "int foo();", it); 120 } 121 122 @Test 123 public void addingTwoAnnotations() { 124 AnnotationMemberDeclaration it = consider("int foo();"); 125 it.addAnnotation("myAnno"); 126 it.addAnnotation("myAnno2"); 127 assertTransformedToString("@myAnno()" + EOL + "@myAnno2()" + EOL + "int foo();", it); 128 } 129 130 @Test 131 public void removingAnnotationOnSomeLine() { 132 AnnotationMemberDeclaration it = consider("@myAnno int foo();"); 133 it.getAnnotations().remove(0); 134 assertTransformedToString("int foo();", it); 135 } 136 137 @Test 138 public void removingAnnotationOnPrevLine() { 139 AnnotationMemberDeclaration it = consider("@myAnno" + EOL + "int foo();"); 140 it.getAnnotations().remove(0); 141 assertTransformedToString("int foo();", it); 142 } 143 144 @Test 145 public void replacingAnnotation() { 146 AnnotationMemberDeclaration it = consider("@myAnno int foo();"); 147 it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>())); 148 assertTransformedToString("@myOtherAnno() int foo();", it); 149 } 150 151 // Javadoc 152 153 @Test 154 public void addingJavadoc() { 155 AnnotationMemberDeclaration it = consider("int foo();"); 156 it.setJavadocComment("Cool this annotation!"); 157 assertTransformedToString("@interface AD { /** Cool this annotation!*/" + EOL + 158 "int foo(); }", it.getParentNode().get()); 159 } 160 161 @Test 162 public void removingJavadoc() { 163 AnnotationMemberDeclaration it = consider("/** Cool this annotation!*/ int foo();"); 164 assertTrue(it.getJavadocComment().get().remove()); 165 assertTransformedToString("@interface AD { int foo(); }", it.getParentNode().get()); 166 } 167 168 @Test 169 public void replacingJavadoc() { 170 AnnotationMemberDeclaration it = consider("/** Cool this annotation!*/ int foo();"); 171 it.setJavadocComment("Super extra cool this annotation!!!"); 172 assertTransformedToString("@interface AD { /** Super extra cool this annotation!!!*/ int foo(); }", it.getParentNode().get()); 173 } 174 175 } 176