KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > action > FinderMethodGeneratorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.action;
21
22 import com.sun.source.tree.MethodTree;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
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 /**
44  *
45  * @author Martin Adamek
46  */

47 public class FinderMethodGeneratorTest extends TestBase {
48     
49     public FinderMethodGeneratorTest(String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public void testGenerateCmpCardinalityOne() throws IOException JavaDoc {
54         TestModule testModule = ejb14();
55
56         // add create method into local and remote interfaces of CMP Entity EJB
57
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 JavaDoc>emptyList(),
67                 Collections.<Modifier>emptySet()
68                 );
69         String JavaDoc ejbql = "SELECT OBJECT(o) FROM CmpLR o";
70         generator.generate(methodModel, true, true, true, ejbql);
71         
72         // local interface
73
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 JavaDoc {
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 JavaDoc.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         // remote interface
101
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 JavaDoc {
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 JavaDoc.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         // entry in deployment descriptor
129
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 JavaDoc {
142         TestModule testModule = ejb14();
143
144         // add create method into local and remote interfaces of CMP Entity EJB
145
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 JavaDoc>emptyList(),
155                 Collections.<Modifier>emptySet()
156                 );
157         String JavaDoc ejbql = "SELECT OBJECT(o) FROM CmpLR o";
158         generator.generate(methodModel, true, true, false, ejbql);
159         
160         // local interface
161
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 JavaDoc {
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                         // isSameType should be used here but fails: Collection vs Collection<E>
176
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 JavaDoc.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         // remote interface
190
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 JavaDoc {
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                         // isSameType should be used here but fails: Collection vs Collection<E>
205
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 JavaDoc.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         // entry in deployment descriptor
219
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