KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.Session;
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 CreateMethodGeneratorTest extends TestBase {
48     
49     public CreateMethodGeneratorTest(String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public void testGenerateSession() throws IOException JavaDoc {
54         TestModule testModule = ejb14();
55         
56         // add create method into local and remote interfaces of Stateful session EJB
57
FileObject beanClass = testModule.getSources()[0].getFileObject("statefullr/StatefulLRBean.java");
58         EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor());
59         Session session = (Session) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.SESSION, Session.EJB_CLASS, "statefullr.StatefulLRBean");
60         CreateMethodGenerator generator = CreateMethodGenerator.create(session, beanClass);
61         final MethodModel methodModel = MethodModel.create(
62                 "createTest",
63                 "void",
64                 "",
65                 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")),
66                 Collections.<String JavaDoc>emptyList(),
67                 Collections.<Modifier>emptySet()
68                 );
69         generator.generate(methodModel, true, true);
70
71         // ejb class, EJB 2.1 spec 7.11.3
72
final boolean[] found = new boolean[] { false };
73         JavaSource javaSource = JavaSource.forFileObject(beanClass);
74         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
75             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
76                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
77                 TypeElement typeElement = workingCopy.getElements().getTypeElement("statefullr.StatefulLRBean");
78                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
79                     if (executableElement.getSimpleName().contentEquals("ejbCreateTest")) {
80                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
81                         assertNotNull(methodTree.getBody());
82                         assertTrue(executableElement.getModifiers().contains(Modifier.PUBLIC));
83                         assertFalse(executableElement.getModifiers().contains(Modifier.FINAL));
84                         assertFalse(executableElement.getModifiers().contains(Modifier.STATIC));
85                         assertSame(TypeKind.VOID, executableElement.getReturnType().getKind());
86                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
87                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
88                         VariableElement parameter = executableElement.getParameters().get(0);
89                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
90                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
91                         found[0] = true;
92                     }
93                 }
94             }
95         });
96         assertTrue(found[0]);
97         
98         // local interface, EJB 2.1 spec 7.11.8
99
found[0] = false;
100         FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("statefullr/StatefulLRLocalHome.java");
101         javaSource = JavaSource.forFileObject(interfaceFileObject);
102         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
103             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
104                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
105                 TypeElement typeElement = workingCopy.getElements().getTypeElement("statefullr.StatefulLRLocalHome");
106                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
107                     if (executableElement.getSimpleName().contentEquals("createTest")) {
108                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
109                         assertNull(methodTree.getBody());
110                         TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException");
111                         assertFalse(executableElement.getThrownTypes().contains(remoteException.asType()));
112                         TypeElement localTypeElement = workingCopy.getElements().getTypeElement("statefullr.StatefulLRLocal");
113                         assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), localTypeElement.asType()));
114                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
115                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
116                         VariableElement parameter = executableElement.getParameters().get(0);
117                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
118                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
119                         found[0] = true;
120                     }
121                 }
122             }
123         });
124         assertTrue(found[0]);
125         
126         // remote interface, EJB 2.1 spec 7.11.6
127
found[0] = false;
128         interfaceFileObject = testModule.getSources()[0].getFileObject("statefullr/StatefulLRRemoteHome.java");
129         javaSource = JavaSource.forFileObject(interfaceFileObject);
130         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
131             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
132                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
133                 TypeElement typeElement = workingCopy.getElements().getTypeElement("statefullr.StatefulLRRemoteHome");
134                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
135                     if (executableElement.getSimpleName().contentEquals("createTest")) {
136                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
137                         assertNull(methodTree.getBody());
138                         TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException");
139                         assertTrue(executableElement.getThrownTypes().contains(remoteException.asType()));
140                         TypeElement remoteTypeElement = workingCopy.getElements().getTypeElement("statefullr.StatefulLRRemote");
141                         assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), remoteTypeElement.asType()));
142                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
143                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
144                         VariableElement parameter = executableElement.getParameters().get(0);
145                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
146                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
147                         found[0] = true;
148                     }
149                 }
150             }
151         });
152         assertTrue(found[0]);
153         
154     }
155     
156     public void testGenerateEntity() throws IOException JavaDoc {
157         TestModule testModule = ejb14();
158         
159         // add create method into local and remote interfaces of CMP Entity EJB
160
FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java");
161         EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor());
162         Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean");
163         CreateMethodGenerator generator = CreateMethodGenerator.create(entity, beanClass);
164         final MethodModel methodModel = MethodModel.create(
165                 "createTest",
166                 "void",
167                 "",
168                 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")),
169                 Collections.<String JavaDoc>emptyList(),
170                 Collections.<Modifier>emptySet()
171                 );
172         generator.generate(methodModel, true, true);
173
174         // ejb class, EJB 2.1 spec 10.6.4, 10.6.5
175
final boolean[] found = new boolean[] { false, false }; // ejbCreate, ejbPostCreate
176
JavaSource javaSource = JavaSource.forFileObject(beanClass);
177         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
178             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
179                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
180                 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRBean");
181                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
182                     if (executableElement.getSimpleName().contentEquals("ejbCreateTest")) {
183                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
184                         assertNotNull(methodTree.getBody());
185                         assertTrue(executableElement.getModifiers().contains(Modifier.PUBLIC));
186                         assertFalse(executableElement.getModifiers().contains(Modifier.FINAL));
187                         assertFalse(executableElement.getModifiers().contains(Modifier.STATIC));
188                         TypeElement longTypeElement = workingCopy.getElements().getTypeElement("java.lang.Long");
189                         assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), longTypeElement.asType()));
190                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
191                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
192                         VariableElement parameter = executableElement.getParameters().get(0);
193                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
194                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
195                         found[0] = true;
196                     }
197                     if (executableElement.getSimpleName().contentEquals("ejbPostCreateTest")) {
198                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
199                         assertNotNull(methodTree.getBody());
200                         assertTrue(executableElement.getModifiers().contains(Modifier.PUBLIC));
201                         assertFalse(executableElement.getModifiers().contains(Modifier.FINAL));
202                         assertFalse(executableElement.getModifiers().contains(Modifier.STATIC));
203                         assertSame(TypeKind.VOID, executableElement.getReturnType().getKind());
204                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
205                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
206                         VariableElement parameter = executableElement.getParameters().get(0);
207                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
208                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
209                         found[1] = true;
210                     }
211                 }
212             }
213         });
214         assertTrue(found[0]);
215         assertTrue(found[1]);
216         
217         // local interface, EJB 2.1 spec 10.6.12
218
found[0] = false;
219         FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRLocalHome.java");
220         javaSource = JavaSource.forFileObject(interfaceFileObject);
221         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
222             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
223                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
224                 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRLocalHome");
225                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
226                     if (executableElement.getSimpleName().contentEquals("createTest")) {
227                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
228                         assertNull(methodTree.getBody());
229                         TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException");
230                         assertFalse(executableElement.getThrownTypes().contains(remoteException.asType()));
231                         TypeElement localTypeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRLocal");
232                         assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), localTypeElement.asType()));
233                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
234                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
235                         VariableElement parameter = executableElement.getParameters().get(0);
236                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
237                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
238                         found[0] = true;
239                     }
240                 }
241             }
242         });
243         assertTrue(found[0]);
244         
245         // remote interface, EJB 2.1 spec 10.6.10
246
found[0] = false;
247         interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRRemoteHome.java");
248         javaSource = JavaSource.forFileObject(interfaceFileObject);
249         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
250             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
251                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
252                 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemoteHome");
253                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
254                     if (executableElement.getSimpleName().contentEquals("createTest")) {
255                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
256                         assertNull(methodTree.getBody());
257                         TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException");
258                         assertTrue(executableElement.getThrownTypes().contains(remoteException.asType()));
259                         TypeElement remoteTypeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemote");
260                         assertTrue(workingCopy.getTypes().isSameType(executableElement.getReturnType(), remoteTypeElement.asType()));
261                         TypeElement createException = workingCopy.getElements().getTypeElement("javax.ejb.CreateException");
262                         assertTrue(executableElement.getThrownTypes().contains(createException.asType()));
263                         VariableElement parameter = executableElement.getParameters().get(0);
264                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
265                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
266                         found[0] = true;
267                     }
268                 }
269             }
270         });
271         assertTrue(found[0]);
272         
273     }
274
275 }
276
Popular Tags