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.element.VariableElement; 29 import javax.lang.model.type.TypeKind; 30 import javax.lang.model.util.ElementFilter; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.WorkingCopy; 33 import org.netbeans.modules.j2ee.common.method.MethodModel; 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.Entity; 39 import org.netbeans.modules.j2ee.ejbcore.test.TestBase; 40 import org.openide.filesystems.FileObject; 41 42 46 public class HomeMethodGenerstorTest extends TestBase { 47 48 public HomeMethodGenerstorTest(String testName) { 49 super(testName); 50 } 51 52 public void testGenerate() throws IOException { 53 TestModule testModule = ejb14(); 54 55 FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java"); 57 EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor()); 58 Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean"); 59 HomeMethodGenerator generator = HomeMethodGenerator.create(entity, beanClass); 60 final MethodModel methodModel = MethodModel.create( 61 "homeTest", 62 "void", 63 "", 64 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")), 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.runModificationTask(new AbstractTask<WorkingCopy>() { 74 public void run(WorkingCopy workingCopy) throws IOException { 75 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 76 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRBean"); 77 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 78 if (executableElement.getSimpleName().contentEquals("ejbHomeHomeTest")) { 79 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 80 assertNotNull(methodTree.getBody()); 81 assertTrue(executableElement.getModifiers().contains(Modifier.PUBLIC)); 82 assertFalse(executableElement.getModifiers().contains(Modifier.STATIC)); 83 assertSame(TypeKind.VOID, executableElement.getReturnType().getKind()); 84 VariableElement parameter = executableElement.getParameters().get(0); 85 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 86 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 87 found[0] = true; 88 } 89 } 90 } 91 }); 92 assertTrue(found[0]); 93 94 found[0] = false; 96 FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRLocalHome.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("cmplr.CmpLRLocalHome"); 102 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 103 if (executableElement.getSimpleName().contentEquals("homeTest")) { 104 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 105 assertNull(methodTree.getBody()); 106 TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException"); 107 assertFalse(executableElement.getThrownTypes().contains(remoteException.asType())); 108 assertSame(TypeKind.VOID, executableElement.getReturnType().getKind()); 109 VariableElement parameter = executableElement.getParameters().get(0); 110 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 111 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 112 found[0] = true; 113 } 114 } 115 } 116 }); 117 assertTrue(found[0]); 118 119 found[0] = false; 121 interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRRemoteHome.java"); 122 javaSource = JavaSource.forFileObject(interfaceFileObject); 123 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 124 public void run(WorkingCopy workingCopy) throws IOException { 125 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 126 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemoteHome"); 127 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 128 if (executableElement.getSimpleName().contentEquals("homeTest")) { 129 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 130 assertNull(methodTree.getBody()); 131 TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException"); 132 assertTrue(executableElement.getThrownTypes().contains(remoteException.asType())); 133 assertSame(TypeKind.VOID, executableElement.getReturnType().getKind()); 134 VariableElement parameter = executableElement.getParameters().get(0); 135 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 136 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 137 found[0] = true; 138 } 139 } 140 } 141 }); 142 assertTrue(found[0]); 143 } 144 145 } 146 | Popular Tags |