1 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 ; 26 import java.io.InputStream ; 27 import java.util.Collections ; 28 import java.util.List ; 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 56 public class MethodModelSupportTest extends NbTestCase { 57 58 private FileObject testFO; 59 60 public MethodModelSupportTest(String testName) { 61 super(testName); 62 } 63 64 protected void setUp() throws Exception { 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 { 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 { 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 } 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 <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 } 127 128 } 129 } 130 }); 131 } 132 133 public void testGetTypeName() throws Exception { 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 { 140 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 141 Elements elements = controller.getElements(); 142 Types types = controller.getTypes(); 143 144 String typeName = String .class.getName(); 145 String resolvedTypeName = MethodModelSupport.getTypeName(controller, elements.getTypeElement(typeName).asType()); 146 assertEquals(typeName, resolvedTypeName); 147 148 typeName = InputStream .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 .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 { 163 final MethodModel methodModel = MethodModel.create( 164 "method", 165 "void", 166 "{ String name; }", Collections.<MethodModel.Variable>emptyList(), 168 Collections.<String >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 { 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 <? extends StatementTree> statements = methodTree.getBody().getStatements(); 186 assertEquals(1, statements.size()); 187 } 188 }); 189 } 190 191 public void testCreateVariable() throws Exception { 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 { 200 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement(); 201 List <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 { 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 { 220 JavaSource javaSource = JavaSource.forFileObject(javaFile); 221 return javaSource.runModificationTask(taskToTest); 222 } 223 224 } 225 | Popular Tags |