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.dd.api.ejb.Query; 40 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod; 41 import org.netbeans.modules.j2ee.ejbcore.test.TestBase; 42 import org.openide.filesystems.FileObject; 43 44 48 public class SelectMethodGeneratorTest extends TestBase { 49 50 public SelectMethodGeneratorTest(String testName) { 51 super(testName); 52 } 53 54 public void testGenerateCmpCardinalityOne() throws IOException { 55 TestModule testModule = ejb14(); 56 57 FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java"); 59 EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor()); 60 Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean"); 61 SelectMethodGenerator generator = SelectMethodGenerator.create(entity, beanClass, testModule.getDeploymentDescriptor()); 62 final MethodModel methodModel = MethodModel.create( 63 "ejbSelectTest", 64 "int", 65 "", 66 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")), 67 Collections.<String >emptyList(), 68 Collections.<Modifier>emptySet() 69 ); 70 String ejbql = "SELECT COUNT(o) FROM CmpLR o"; 71 generator.generate(methodModel, true, true, true, ejbql); 72 73 final boolean[] found = new boolean[] { false }; 75 JavaSource javaSource = JavaSource.forFileObject(beanClass); 76 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 77 public void run(WorkingCopy workingCopy) throws IOException { 78 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 79 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRBean"); 80 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 81 if (executableElement.getSimpleName().contentEquals("ejbSelectTest")) { 82 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 83 assertNull(methodTree.getBody()); 84 assertSame(TypeKind.INT, executableElement.getReturnType().getKind()); 85 TypeElement finderException = workingCopy.getElements().getTypeElement("javax.ejb.FinderException"); 86 assertTrue(executableElement.getThrownTypes().contains(finderException.asType())); 87 VariableElement parameter = executableElement.getParameters().get(0); 88 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 89 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 90 found[0] = true; 91 } 92 } 93 } 94 }); 95 assertTrue(found[0]); 96 97 boolean queryFound = false; 99 for (Query query : entity.getQuery()) { 100 QueryMethod queryMethod = query.getQueryMethod(); 101 if ("ejbSelectTest".equals(queryMethod.getMethodName())) { 102 assertEquals(ejbql, query.getEjbQl()); 103 queryFound = true; 104 } 105 } 106 assertTrue(queryFound); 107 108 } 109 110 } 111 | Popular Tags |