KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > method > MethodModelSupportTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.common.method;
21
22 import com.sun.source.tree.MethodTree;
23 import com.sun.source.tree.PrimitiveTypeTree;
24 import com.sun.source.tree.StatementTree;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.lang.model.element.ExecutableElement;
30 import javax.lang.model.element.Modifier;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.type.TypeKind;
34 import javax.lang.model.util.ElementFilter;
35 import javax.lang.model.util.Elements;
36 import javax.lang.model.util.Types;
37 import org.netbeans.api.java.source.CancellableTask;
38 import org.netbeans.api.java.source.CompilationController;
39 import org.netbeans.api.java.source.JavaSource;
40 import org.netbeans.api.java.source.ModificationResult;
41 import org.netbeans.api.java.source.WorkingCopy;
42 import org.netbeans.junit.MockServices;
43 import org.netbeans.junit.NbTestCase;
44 import org.netbeans.modules.j2ee.common.source.AbstractTask;
45 import org.netbeans.modules.j2ee.common.source.FakeJavaDataLoaderPool;
46 import org.netbeans.modules.j2ee.common.source.RepositoryImpl;
47 import org.netbeans.modules.j2ee.common.source.SourceUtils;
48 import org.netbeans.modules.j2ee.common.source.TestUtilities;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileUtil;
51
52 /**
53  *
54  * @author Martin Adamek
55  */

56 public class MethodModelSupportTest extends NbTestCase {
57     
58     private FileObject testFO;
59
60     public MethodModelSupportTest(String JavaDoc testName) {
61         super(testName);
62     }
63     
64     protected void setUp() throws Exception JavaDoc {
65         MockServices.setServices(FakeJavaDataLoaderPool.class, RepositoryImpl.class);
66
67         clearWorkDir();
68         FileObject workDir = FileUtil.toFileObject(getWorkDir());
69         testFO = workDir.createData("TestClass.java");
70     }
71     
72     public void testCreateMethodModel() throws Exception JavaDoc {
73         TestUtilities.copyStringToFileObject(testFO,
74                 "package foo;" +
75                 "public class TestClass {" +
76                 " private boolean method1() throws java.io.IOException {" +
77                 " return false;" +
78                 " }" +
79                 " public static void method2(String name, int age, String[] interests) {" +
80                 " return null;" +
81                 " }" +
82                 "}");
83         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
84             public void run(CompilationController controller) throws IOException JavaDoc {
85                 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
86                 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement();
87                 for (ExecutableElement method : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
88                     MethodModel methodModel = MethodModelSupport.createMethodModel(controller, method);
89                     if (method.getSimpleName().contentEquals("method1")) {
90                         assertEquals("boolean", methodModel.getReturnType());
91                         assertEquals("method1", methodModel.getName());
92                         assertTrue(methodModel.getModifiers().size() == 1);
93                         assertTrue(methodModel.getModifiers().contains(Modifier.PRIVATE));
94                         assertTrue(methodModel.getParameters().size() == 0);
95                         assertTrue(methodModel.getExceptions().size() == 1);
96                         assertTrue(methodModel.getExceptions().contains("java.io.IOException"));
97                         //TODO: RETOUCHE test method body, see #90926
98
} else if (method.getSimpleName().contentEquals("method2")) {
99                         assertEquals("void", methodModel.getReturnType());
100                         assertEquals("method2", methodModel.getName());
101                         assertTrue(methodModel.getModifiers().size() == 2);
102                         assertTrue(methodModel.getModifiers().contains(Modifier.PUBLIC));
103                         assertTrue(methodModel.getModifiers().contains(Modifier.STATIC));
104                         MethodModel.Variable nameVariable = null;
105                         MethodModel.Variable ageVariable = null;
106                         MethodModel.Variable interestsVariable = null;
107                         List JavaDoc<MethodModel.Variable> variables = methodModel.getParameters();
108                         assertTrue(variables.size() == 3);
109                         for (MethodModel.Variable variable : variables) {
110                             if ("name".equals(variable.getName())) {
111                                 nameVariable = variable;
112                             } else if ("age".equals(variable.getName())) {
113                                 ageVariable = variable;
114                             } else if ("interests".equals(variable.getName())) {
115                                 interestsVariable = variable;
116                             }
117                         }
118                         assertNotNull(nameVariable);
119                         assertNotNull(ageVariable);
120                         assertNotNull(interestsVariable);
121                         assertEquals("java.lang.String", nameVariable.getType());
122                         assertEquals("int", ageVariable.getType());
123                         assertEquals("java.lang.String[]", interestsVariable.getType());
124                         assertTrue(methodModel.getExceptions().size() == 0);
125                         //TODO: RETOUCHE test method body, see #90926
126
}
127                     
128                 }
129             }
130         });
131     }
132     
133     public void testGetTypeName() throws Exception JavaDoc {
134         TestUtilities.copyStringToFileObject(testFO,
135                 "package foo;" +
136                 "public class TestClass {" +
137                 "}");
138         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
139             public void run(CompilationController controller) throws IOException JavaDoc {
140                 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
141                 Elements elements = controller.getElements();
142                 Types types = controller.getTypes();
143                 
144                 String JavaDoc typeName = String JavaDoc.class.getName();
145                 String JavaDoc resolvedTypeName = MethodModelSupport.getTypeName(controller, elements.getTypeElement(typeName).asType());
146                 assertEquals(typeName, resolvedTypeName);
147                 
148                 typeName = InputStream JavaDoc.class.getName();
149                 resolvedTypeName = MethodModelSupport.getTypeName(controller, elements.getTypeElement(typeName).asType());
150                 assertEquals(typeName, resolvedTypeName);
151                 
152                 resolvedTypeName = MethodModelSupport.getTypeName(controller, types.getPrimitiveType(TypeKind.INT));
153                 assertEquals("int", resolvedTypeName);
154
155                 typeName = String JavaDoc.class.getName();
156                 resolvedTypeName = MethodModelSupport.getTypeName(controller, types.getArrayType(elements.getTypeElement(typeName).asType()));
157                 assertEquals("java.lang.String[]", resolvedTypeName);
158             }
159         });
160     }
161     
162     public void testCreateMethodTree() throws Exception JavaDoc {
163         final MethodModel methodModel = MethodModel.create(
164                 "method",
165                 "void",
166                 "{ String name; }", // for now, Retouche requires those parenthesis (they won't appear in file)
167
Collections.<MethodModel.Variable>emptyList(),
168                 Collections.<String JavaDoc>emptyList(),
169                 Collections.<Modifier>emptySet()
170                 );
171         TestUtilities.copyStringToFileObject(testFO,
172                 "package foo;" +
173                 "public class TestClass {" +
174                 "}");
175         runModificationTask(testFO, new AbstractTask<WorkingCopy>() {
176             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
177                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
178                 MethodTree methodTree = MethodModelSupport.createMethodTree(workingCopy, methodModel);
179                 assertEquals(0, methodTree.getModifiers().getFlags().size());
180                 PrimitiveTypeTree returnTypeTree = (PrimitiveTypeTree) methodTree.getReturnType();
181                 assertTrue(TypeKind.VOID == returnTypeTree.getPrimitiveTypeKind());
182                 assertTrue(methodTree.getName().contentEquals("method"));
183                 assertEquals(0, methodTree.getParameters().size());
184                 assertEquals(0, methodTree.getThrows().size());
185                 List JavaDoc<? extends StatementTree> statements = methodTree.getBody().getStatements();
186                 assertEquals(1, statements.size());
187             }
188         });
189     }
190     
191     public void testCreateVariable() throws Exception JavaDoc {
192         TestUtilities.copyStringToFileObject(testFO,
193                 "package foo;" +
194                 "public class TestClass {" +
195                 " private String name;" +
196                 " private final String address;" +
197                 "}");
198         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
199             public void run(CompilationController controller) throws IOException JavaDoc {
200                 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement();
201                 List JavaDoc<VariableElement> fields = ElementFilter.fieldsIn(typeElement.getEnclosedElements());
202                 MethodModel.Variable nonFinalVariable = MethodModelSupport.createVariable(controller, fields.get(0));
203                 assertEquals("java.lang.String", nonFinalVariable.getType());
204                 assertEquals("name", nonFinalVariable.getName());
205                 assertFalse(nonFinalVariable.getFinalModifier());
206                 MethodModel.Variable finalVariable = MethodModelSupport.createVariable(controller, fields.get(1));
207                 assertEquals("java.lang.String", finalVariable.getType());
208                 assertEquals("address", finalVariable.getName());
209                 assertTrue(finalVariable.getFinalModifier());
210             }
211         });
212     }
213     
214     private static void runUserActionTask(FileObject javaFile, CancellableTask<CompilationController> taskToTest) throws IOException JavaDoc {
215         JavaSource javaSource = JavaSource.forFileObject(javaFile);
216         javaSource.runUserActionTask(taskToTest, true);
217     }
218
219     private static ModificationResult runModificationTask(FileObject javaFile, CancellableTask<WorkingCopy> taskToTest) throws IOException JavaDoc {
220         JavaSource javaSource = JavaSource.forFileObject(javaFile);
221         return javaSource.runModificationTask(taskToTest);
222     }
223
224 }
225
Popular Tags