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; 23 24 import com.github.javaparser.ast.ArrayCreationLevel; 25 import com.github.javaparser.ast.NodeList; 26 import com.github.javaparser.ast.expr.ArrayCreationExpr; 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 com.github.javaparser.utils.Utils; 32 import org.junit.Test; 33 34 import java.io.IOException; 35 36 /** 37 * Transforming ArrayCreationLevel and verifying the LexicalPreservation works as expected. 38 */ 39 public class ArrayCreationLevelTransformationsTest extends AbstractLexicalPreservingTest { 40 41 protected ArrayCreationLevel consider(String code) { 42 considerExpression("new int" + code); 43 ArrayCreationExpr arrayCreationExpr = expression.asArrayCreationExpr(); 44 return arrayCreationExpr.getLevels().get(0); 45 } 46 47 // Dimension 48 49 @Test 50 public void addingDimension() throws IOException { 51 ArrayCreationLevel it = consider("[]"); 52 it.setDimension(new IntegerLiteralExpr("10")); 53 assertTransformedToString("[10]", it); 54 } 55 56 @Test 57 public void removingDimension() throws IOException { 58 ArrayCreationLevel it = consider("[10]"); 59 it.removeDimension(); 60 assertTransformedToString("[]", it); 61 } 62 63 @Test 64 public void replacingDimension() throws IOException { 65 ArrayCreationLevel it = consider("[10]"); 66 it.setDimension(new IntegerLiteralExpr("12")); 67 assertTransformedToString("[12]", it); 68 } 69 70 // Annotations 71 72 @Test 73 public void addingAnnotation() throws IOException { 74 ArrayCreationLevel it = consider("[]"); 75 it.addAnnotation("myAnno"); 76 assertTransformedToString("@myAnno()"+ Utils.EOL+"[]", it); 77 } 78 79 @Test 80 public void removingAnnotation() throws IOException { 81 ArrayCreationLevel it = consider("@myAnno []"); 82 it.getAnnotations().remove(0); 83 assertTransformedToString("[]", it); 84 } 85 86 @Test 87 public void replacingAnnotation() throws IOException { 88 ArrayCreationLevel it = consider("@myAnno []"); 89 it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>())); 90 assertTransformedToString("@myOtherAnno() []", it); 91 } 92 93 } 94