• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.github.javaparser.printer.lexicalpreservation;
2 
3 import com.github.javaparser.ast.Modifier;
4 import com.github.javaparser.ast.body.FieldDeclaration;
5 import com.github.javaparser.ast.body.MethodDeclaration;
6 import com.github.javaparser.ast.body.VariableDeclarator;
7 import com.github.javaparser.ast.expr.Expression;
8 import com.github.javaparser.ast.expr.NameExpr;
9 import com.github.javaparser.ast.expr.NullLiteralExpr;
10 import com.github.javaparser.ast.stmt.ReturnStmt;
11 import com.github.javaparser.ast.type.ArrayType;
12 import com.github.javaparser.ast.type.PrimitiveType;
13 import org.junit.Test;
14 
15 import java.io.IOException;
16 import java.util.EnumSet;
17 
18 /**
19  * These tests are more "high level" than the ones in LexicalPreservingPrinterTest.
20  * The idea is to perform some transformations on the code, print it back and see if the generated code
21  * is the expected one. We do not care about the internal state of LexicalPreservingPrinter, just the visible result.
22  */
23 public class TransformationsTest extends  AbstractLexicalPreservingTest {
24 
25     @Test
unchangedSimpleClasses()26     public void unchangedSimpleClasses() throws IOException {
27         assertUnchanged("Example1");
28         assertUnchanged("Example2");
29     }
30 
31     @Test
unchangedComplexFile()32     public void unchangedComplexFile() throws IOException {
33         assertUnchanged("Example4");
34     }
35 
36     @Test
example1()37     public void example1() throws IOException {
38         considerExample("Example1_original");
39         cu.getClassByName("A").get().getFieldByName("a").get().setModifiers(EnumSet.of(Modifier.STATIC));
40         assertTransformed("Example1", cu);
41     }
42 
43     @Test
example2()44     public void example2() throws IOException {
45         considerExample("Example2_original");
46         cu.getClassByName("A").get().getFieldByName("a").get().getVariable(0).setInitializer("10");
47         assertTransformed("Example2", cu);
48     }
49 
50     @Test
example3()51     public void example3() throws IOException {
52         considerExample("Example3_original");
53         cu.getClassByName("A").get().getFieldByName("a").get().getVariable(0).setInitializer((Expression) null);
54         assertTransformed("Example3", cu);
55     }
56 
57     @Test
example5()58     public void example5() throws IOException {
59         considerExample("Example5_original");
60         cu.getClassByName("A").get().getFieldByName("a").get().getVariable(0).setInitializer(new NullLiteralExpr());
61         assertTransformed("Example5", cu);
62     }
63 
64     @Test
example6()65     public void example6() throws IOException {
66         considerExample("Example6_original");
67         cu.getClassByName("A").get().getFieldByName("a").get().getVariable(0).setName("someOtherName");
68         assertTransformed("Example6", cu);
69     }
70 
71     @Test
example7()72     public void example7() throws IOException {
73         considerExample("Example7_original");
74         cu.getClassByName("A").get().getFieldByName("a").get().getVariable(0).setType(new ArrayType(PrimitiveType.intType()));
75         assertTransformed("Example7", cu);
76     }
77 
78     @Test
example8()79     public void example8() throws IOException {
80         considerExample("Example8_original");
81         FieldDeclaration fd = (FieldDeclaration) cu.getClassByName("A").get().getMember(0).asFieldDeclaration();
82         fd.addVariable(new VariableDeclarator(PrimitiveType.intType(), "b"));
83         assertTransformed("Example8", cu);
84     }
85 
86     @Test
example9()87     public void example9() throws IOException {
88         considerExample("Example9_original");
89         FieldDeclaration fd = (FieldDeclaration) cu.getClassByName("A").get().getMember(0).asFieldDeclaration();
90         fd.addVariable(new VariableDeclarator(new ArrayType(PrimitiveType.intType()), "b"));
91         assertTransformed("Example9", cu);
92     }
93 
94     @Test
example10()95     public void example10() throws IOException {
96         considerExample("Example10_original");
97         cu.getClassByName("A").get().getMembers().remove(0);
98         assertTransformed("Example10", cu);
99     }
100 
101     @Test
exampleParam1()102     public void exampleParam1() throws IOException {
103         considerExample("Example_param1_original");
104         MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
105         md.addParameter("int", "p1");
106         assertTransformed("Example_param1", cu);
107     }
108 
109     @Test
exampleParam2()110     public void exampleParam2() throws IOException {
111         considerExample("Example_param1_original");
112         MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
113         md.addParameter(new ArrayType(PrimitiveType.intType()), "p1");
114         md.addParameter("char", "p2");
115         assertTransformed("Example_param2", cu);
116     }
117 
118     @Test
exampleParam3()119     public void exampleParam3() throws IOException {
120         considerExample("Example_param3_original");
121         MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
122         md.getParameters().remove(0);
123         assertTransformed("Example_param3", cu);
124     }
125 
126     @Test
exampleParam4()127     public void exampleParam4() throws IOException {
128         considerExample("Example_param3_original");
129         MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
130         md.getParameters().remove(1);
131         assertTransformed("Example_param4", cu);
132     }
133 
134     @Test
exampleParam5()135     public void exampleParam5() throws IOException {
136         considerExample("Example_param3_original");
137         MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
138         md.setType(PrimitiveType.intType());
139         assertTransformed("Example_param5b", cu);
140         md.getBody().get().getStatements().add(new ReturnStmt(new NameExpr("p1")));
141         assertTransformed("Example_param5", cu);
142     }
143 }
144