KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ElementFilter;
29 import org.netbeans.api.java.source.CompilationController;
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.method.MethodModelSupport;
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.Session;
39 import org.netbeans.modules.j2ee.ejbcore.test.TestBase;
40 import org.openide.filesystems.FileObject;
41
42 /**
43  *
44  * @author Martin Adamek
45  */

46 public class BusinessMethodGeneratorTest extends TestBase {
47     
48     public BusinessMethodGeneratorTest(String JavaDoc testName) {
49         super(testName);
50     }
51     
52     public void testGenerateEE14() throws IOException JavaDoc {
53         TestModule testModule = ejb14();
54         
55         // add business method into local and remote interfaces of stateless session EJB
56
FileObject beanClass = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRBean.java");
57         EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor());
58         Session session = (Session) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.SESSION, Session.EJB_CLASS, "statelesslr.StatelessLRBean");
59         BusinessMethodGenerator generator = BusinessMethodGenerator.create(session, beanClass);
60         final MethodModel methodModel = MethodModel.create(
61                 "businessMethodTest",
62                 "java.lang.String",
63                 "return null;",
64                 Collections.<MethodModel.Variable>emptyList(),
65                 Collections.<String JavaDoc>emptyList(),
66                 Collections.<Modifier>emptySet()
67                 );
68         generator.generate(methodModel, true, true);
69
70         // ejb class
71
final boolean[] found = new boolean[] { false };
72         JavaSource javaSource = JavaSource.forFileObject(beanClass);
73         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
74             public void run(CompilationController controller) throws IOException JavaDoc {
75                 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
76                 MethodModel ejbClassMethodModel = MethodModel.create(
77                         methodModel.getName(),
78                         methodModel.getReturnType(),
79                         methodModel.getBody(),
80                         methodModel.getParameters(),
81                         methodModel.getExceptions(),
82                         Collections.singleton(Modifier.PUBLIC)
83                         );
84                 TypeElement typeElement = controller.getElements().getTypeElement("statelesslr.StatelessLRBean");
85                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
86                     if (MethodModelSupport.isSameMethod(controller, executableElement, ejbClassMethodModel)) {
87                         found[0] = true;
88                     }
89                 }
90             }
91         }, true);
92         assertTrue(found[0]);
93         
94         // local interface
95
found[0] = false;
96         FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRLocal.java");
97         javaSource = JavaSource.forFileObject(interfaceFileObject);
98         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
99             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
100                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
101                 TypeElement typeElement = workingCopy.getElements().getTypeElement("statelesslr.StatelessLRLocal");
102                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
103                     if (MethodModelSupport.isSameMethod(workingCopy, executableElement, methodModel)) {
104                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
105                         assertNull(methodTree.getBody());
106                         found[0] = true;
107                     }
108                 }
109             }
110         });
111         assertTrue(found[0]);
112         
113         // remote interface
114
found[0] = false;
115         interfaceFileObject = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRRemote.java");
116         javaSource = JavaSource.forFileObject(interfaceFileObject);
117         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
118             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
119                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
120                 MethodModel interfaceMethodModel = MethodModel.create(
121                         methodModel.getName(),
122                         methodModel.getReturnType(),
123                         methodModel.getBody(),
124                         methodModel.getParameters(),
125                         Collections.singletonList("java.rmi.RemoteException"),
126                         methodModel.getModifiers()
127                         );
128                 TypeElement typeElement = workingCopy.getElements().getTypeElement("statelesslr.StatelessLRRemote");
129                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
130                     if (MethodModelSupport.isSameMethod(workingCopy, executableElement, interfaceMethodModel)) {
131                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
132                         assertNull(methodTree.getBody());
133                         found[0] = true;
134                     }
135                 }
136             }
137         });
138         assertTrue(found[0]);
139         
140     }
141     
142 }
143
Popular Tags