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.util.ElementFilter; 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.source.AbstractTask; 34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider; 35 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar; 36 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans; 37 import org.netbeans.modules.j2ee.dd.api.ejb.Entity; 38 import org.netbeans.modules.j2ee.dd.api.ejb.Query; 39 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod; 40 import org.netbeans.modules.j2ee.ejbcore.test.TestBase; 41 import org.openide.filesystems.FileObject; 42 43 47 public class FinderMethodGeneratorTest extends TestBase { 48 49 public FinderMethodGeneratorTest(String testName) { 50 super(testName); 51 } 52 53 public void testGenerateCmpCardinalityOne() throws IOException { 54 TestModule testModule = ejb14(); 55 56 FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java"); 58 EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor()); 59 Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean"); 60 FinderMethodGenerator generator = FinderMethodGenerator.create(entity, beanClass, testModule.getDeploymentDescriptor()); 61 final MethodModel methodModel = MethodModel.create( 62 "findTestOne", 63 "void", 64 "", 65 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")), 66 Collections.<String >emptyList(), 67 Collections.<Modifier>emptySet() 68 ); 69 String ejbql = "SELECT OBJECT(o) FROM CmpLR o"; 70 generator.generate(methodModel, true, true, true, ejbql); 71 72 final boolean[] found = new boolean[] { false }; 74 FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRLocalHome.java"); 75 JavaSource javaSource = JavaSource.forFileObject(interfaceFileObject); 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.CmpLRLocalHome"); 80 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 81 if (executableElement.getSimpleName().contentEquals("findTestOne")) { 82 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 83 assertNull(methodTree.getBody()); 84 TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException"); 85 assertFalse(executableElement.getThrownTypes().contains(remoteException.asType())); 86 TypeElement localTypeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRLocal"); 87 assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), localTypeElement.asType())); 88 TypeElement finderException = workingCopy.getElements().getTypeElement("javax.ejb.FinderException"); 89 assertTrue(executableElement.getThrownTypes().contains(finderException.asType())); 90 VariableElement parameter = executableElement.getParameters().get(0); 91 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 92 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 93 found[0] = true; 94 } 95 } 96 } 97 }); 98 assertTrue(found[0]); 99 100 found[0] = false; 102 interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRRemoteHome.java"); 103 javaSource = JavaSource.forFileObject(interfaceFileObject); 104 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 105 public void run(WorkingCopy workingCopy) throws IOException { 106 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 107 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemoteHome"); 108 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 109 if (executableElement.getSimpleName().contentEquals("findTestOne")) { 110 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 111 assertNull(methodTree.getBody()); 112 TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException"); 113 assertTrue(executableElement.getThrownTypes().contains(remoteException.asType())); 114 TypeElement remoteTypeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemote"); 115 assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), remoteTypeElement.asType())); 116 TypeElement finderException = workingCopy.getElements().getTypeElement("javax.ejb.FinderException"); 117 assertTrue(executableElement.getThrownTypes().contains(finderException.asType())); 118 VariableElement parameter = executableElement.getParameters().get(0); 119 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 120 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 121 found[0] = true; 122 } 123 } 124 } 125 }); 126 assertTrue(found[0]); 127 128 boolean queryFound = false; 130 for (Query query : entity.getQuery()) { 131 QueryMethod queryMethod = query.getQueryMethod(); 132 if ("findTestOne".equals(queryMethod.getMethodName())) { 133 assertEquals(ejbql, query.getEjbQl()); 134 queryFound = true; 135 } 136 } 137 assertTrue(queryFound); 138 139 } 140 141 public void testGenerateCmpCardinalityMany() throws IOException { 142 TestModule testModule = ejb14(); 143 144 FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java"); 146 EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor()); 147 Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean"); 148 FinderMethodGenerator generator = FinderMethodGenerator.create(entity, beanClass, testModule.getDeploymentDescriptor()); 149 final MethodModel methodModel = MethodModel.create( 150 "findTestMany", 151 "void", 152 "", 153 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")), 154 Collections.<String >emptyList(), 155 Collections.<Modifier>emptySet() 156 ); 157 String ejbql = "SELECT OBJECT(o) FROM CmpLR o"; 158 generator.generate(methodModel, true, true, false, ejbql); 159 160 final boolean[] found = new boolean[] { false }; 162 FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRLocalHome.java"); 163 JavaSource javaSource = JavaSource.forFileObject(interfaceFileObject); 164 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 165 public void run(WorkingCopy workingCopy) throws IOException { 166 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 167 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRLocalHome"); 168 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 169 if (executableElement.getSimpleName().contentEquals("findTestMany")) { 170 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 171 assertNull(methodTree.getBody()); 172 TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException"); 173 assertFalse(executableElement.getThrownTypes().contains(remoteException.asType())); 174 TypeElement collectionTypeElement = workingCopy.getElements().getTypeElement("java.util.Collection"); 175 assertTrue(workingCopy.getTypes().isAssignable(executableElement.getReturnType(), collectionTypeElement.asType())); 177 TypeElement finderException = workingCopy.getElements().getTypeElement("javax.ejb.FinderException"); 178 assertTrue(executableElement.getThrownTypes().contains(finderException.asType())); 179 VariableElement parameter = executableElement.getParameters().get(0); 180 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 181 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 182 found[0] = true; 183 } 184 } 185 } 186 }); 187 assertTrue(found[0]); 188 189 found[0] = false; 191 interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRRemoteHome.java"); 192 javaSource = JavaSource.forFileObject(interfaceFileObject); 193 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 194 public void run(WorkingCopy workingCopy) throws IOException { 195 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 196 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemoteHome"); 197 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) { 198 if (executableElement.getSimpleName().contentEquals("findTestMany")) { 199 MethodTree methodTree = workingCopy.getTrees().getTree(executableElement); 200 assertNull(methodTree.getBody()); 201 TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException"); 202 assertTrue(executableElement.getThrownTypes().contains(remoteException.asType())); 203 TypeElement collectionTypeElement = workingCopy.getElements().getTypeElement("java.util.Collection"); 204 assertTrue(workingCopy.getTypes().isAssignable(executableElement.getReturnType(), collectionTypeElement.asType())); 206 TypeElement finderException = workingCopy.getElements().getTypeElement("javax.ejb.FinderException"); 207 assertTrue(executableElement.getThrownTypes().contains(finderException.asType())); 208 VariableElement parameter = executableElement.getParameters().get(0); 209 TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String .class.getName()); 210 assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType())); 211 found[0] = true; 212 } 213 } 214 } 215 }); 216 assertTrue(found[0]); 217 218 boolean queryFound = false; 220 for (Query query : entity.getQuery()) { 221 QueryMethod queryMethod = query.getQueryMethod(); 222 if ("findTestMany".equals(queryMethod.getMethodName())) { 223 assertEquals(ejbql, query.getEjbQl()); 224 queryFound = true; 225 } 226 } 227 assertTrue(queryFound); 228 229 } 230 231 } 232 | Popular Tags |