KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejbcore.test.TestBase;
40 import org.openide.filesystems.FileObject;
41
42 /**
43  *
44  * @author Martin Adamek
45  */

46 public class HomeMethodGenerstorTest extends TestBase {
47     
48     public HomeMethodGenerstorTest(String JavaDoc testName) {
49         super(testName);
50     }
51     
52     public void testGenerate() throws IOException JavaDoc {
53         TestModule testModule = ejb14();
54         
55         // add create method into local and remote interfaces of CMP Entity EJB
56
FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java");
57         EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor());
58         Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean");
59         HomeMethodGenerator generator = HomeMethodGenerator.create(entity, beanClass);
60         final MethodModel methodModel = MethodModel.create(
61                 "homeTest",
62                 "void",
63                 "",
64                 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")),
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.runModificationTask(new AbstractTask<WorkingCopy>() {
74             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
75                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
76                 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRBean");
77                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
78                     if (executableElement.getSimpleName().contentEquals("ejbHomeHomeTest")) {
79                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
80                         assertNotNull(methodTree.getBody());
81                         assertTrue(executableElement.getModifiers().contains(Modifier.PUBLIC));
82                         assertFalse(executableElement.getModifiers().contains(Modifier.STATIC));
83                         assertSame(TypeKind.VOID, executableElement.getReturnType().getKind());
84                         VariableElement parameter = executableElement.getParameters().get(0);
85                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
86                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
87                         found[0] = true;
88                     }
89                 }
90             }
91         });
92         assertTrue(found[0]);
93         
94         // local interface
95
found[0] = false;
96         FileObject interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRLocalHome.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("cmplr.CmpLRLocalHome");
102                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
103                     if (executableElement.getSimpleName().contentEquals("homeTest")) {
104                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
105                         assertNull(methodTree.getBody());
106                         TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException");
107                         assertFalse(executableElement.getThrownTypes().contains(remoteException.asType()));
108                         assertSame(TypeKind.VOID, executableElement.getReturnType().getKind());
109                         VariableElement parameter = executableElement.getParameters().get(0);
110                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
111                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
112                         found[0] = true;
113                     }
114                 }
115             }
116         });
117         assertTrue(found[0]);
118         
119         // remote interface
120
found[0] = false;
121         interfaceFileObject = testModule.getSources()[0].getFileObject("cmplr/CmpLRRemoteHome.java");
122         javaSource = JavaSource.forFileObject(interfaceFileObject);
123         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
124             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
125                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
126                 TypeElement typeElement = workingCopy.getElements().getTypeElement("cmplr.CmpLRRemoteHome");
127                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
128                     if (executableElement.getSimpleName().contentEquals("homeTest")) {
129                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
130                         assertNull(methodTree.getBody());
131                         TypeElement remoteException = workingCopy.getElements().getTypeElement("java.rmi.RemoteException");
132                         assertTrue(executableElement.getThrownTypes().contains(remoteException.asType()));
133                         assertSame(TypeKind.VOID, executableElement.getReturnType().getKind());
134                         VariableElement parameter = executableElement.getParameters().get(0);
135                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
136                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
137                         found[0] = true;
138                     }
139                 }
140             }
141         });
142         assertTrue(found[0]);
143     }
144     
145 }
146
Popular Tags