1 19 20 package org.netbeans.modules.j2ee.ejbcore.action; 21 22 import com.sun.source.tree.MethodTree; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import javax.lang.model.element.ExecutableElement; 26 import javax.lang.model.element.Modifier; 27 import javax.lang.model.element.TypeElement; 28 import javax.lang.model.util.ElementFilter; 29 import org.netbeans.api.java.source.CompilationController; 30 import org.netbeans.api.java.source.JavaSource; 31 import org.netbeans.api.java.source.WorkingCopy; 32 import org.netbeans.modules.j2ee.common.method.MethodModel; 33 import org.netbeans.modules.j2ee.common.method.MethodModelSupport; 34 import org.netbeans.modules.j2ee.common.source.AbstractTask; 35 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 36 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 37 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 38 import org.netbeans.modules.j2ee.dd.api.ejb.Session; 39 import org.netbeans.modules.j2ee.ejbcore.test.TestBase; 40 import org.openide.filesystems.FileObject; 41 42 46 public class BusinessMethodGeneratorTest extends TestBase { 47 48 public BusinessMethodGeneratorTest(String testName) { 49 super(testName); 50 } 51 52 public void testGenerateEE14() throws IOException { 53 TestModule testModule = ejb14(); 54 55 FileObject beanClass = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRBean.java"); 57 EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor()); 58 Session session = (Session) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.SESSION, Session.EJB_CLASS, "statelesslr.StatelessLRBean"); 59 BusinessMethodGenerator generator = BusinessMethodGenerator.create(session, beanClass); 60 final MethodModel methodModel = MethodModel.create( 61 "businessMethodTest", 62 "java.lang.String", 63 "return null;", 64 Collections.<MethodModel.Variable>emptyList(), 65 Collections.<String >emptyList(), 66 Collections.<Modifier>emptySet() 67 ); 68 generator.generate(methodModel, true, true); 69 70 final boolean[] found = new boolean[] { false }; 72 JavaSource javaSource = JavaSource.forFileObject(beanClass); 73 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 74 public void run(CompilationController controller) throws IOException { 75 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 76 MethodModel ejbClassMethodModel = MethodModel.create( 77 methodModel.getName(), 78 methodModel.getReturnType(), 79 methodModel.getBody(), 80 methodModel.getParameters(), 81 methodModel.getExceptions(), 82 Collections.singleton(Modifier.PUBLIC) 83 ); 84 TypeElement typeElement = controller.getElements().getTypeElement("statelesslr.StatelessLRBean"); 85 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 86 if (MethodModelSupport.isSameMethod(controller, executableElement, ejbClassMethodModel)) { 87 found[0] = true; 88 } 89 } 90 } 91 }, true); 92 assertTrue(found[0]); 93 94 found[0] = false; 96 FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRLocal.java"); 97 javaSource = JavaSource.forFileObject(interfaceFileObject); 98 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 99 public void run(WorkingCopy workingCopy) throws IOException { 100 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 101 TypeElement typeElement = workingCopy.getElements().getTypeElement("statelesslr.StatelessLRLocal"); 102 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 103 if (MethodModelSupport.isSameMethod(workingCopy, executableElement, methodModel)) { 104 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 105 assertNull(methodTree.getBody()); 106 found[0] = true; 107 } 108 } 109 } 110 }); 111 assertTrue(found[0]); 112 113 found[0] = false; 115 interfaceFileObject = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRRemote.java"); 116 javaSource = JavaSource.forFileObject(interfaceFileObject); 117 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 118 public void run(WorkingCopy workingCopy) throws IOException { 119 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 120 MethodModel interfaceMethodModel = MethodModel.create( 121 methodModel.getName(), 122 methodModel.getReturnType(), 123 methodModel.getBody(), 124 methodModel.getParameters(), 125 Collections.singletonList("java.rmi.RemoteException"), 126 methodModel.getModifiers() 127 ); 128 TypeElement typeElement = workingCopy.getElements().getTypeElement("statelesslr.StatelessLRRemote"); 129 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 130 if (MethodModelSupport.isSameMethod(workingCopy, executableElement, interfaceMethodModel)) { 131 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 132 assertNull(methodTree.getBody()); 133 found[0] = true; 134 } 135 } 136 } 137 }); 138 assertTrue(found[0]); 139 140 } 141 142 } 143 | Popular Tags |