KickJava   Java API By Example, From Geeks To Geeks.

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


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.Query;
40 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod;
41 import org.netbeans.modules.j2ee.ejbcore.test.TestBase;
42 import org.openide.filesystems.FileObject;
43
44 /**
45  *
46  * @author Martin Adamek
47  */

48 public class SelectMethodGeneratorTest extends TestBase {
49     
50     public SelectMethodGeneratorTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     public void testGenerateCmpCardinalityOne() throws IOException JavaDoc {
55         TestModule testModule = ejb14();
56
57         // add create method into local and remote interfaces of CMP Entity EJB
58
FileObject beanClass = testModule.getSources()[0].getFileObject("cmplr/CmpLRBean.java");
59         EjbJar ejbJar = DDProvider.getDefault().getDDRoot(testModule.getDeploymentDescriptor());
60         Entity entity = (Entity) ejbJar.getEnterpriseBeans().findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, "cmplr.CmpLRBean");
61         SelectMethodGenerator generator = SelectMethodGenerator.create(entity, beanClass, testModule.getDeploymentDescriptor());
62         final MethodModel methodModel = MethodModel.create(
63                 "ejbSelectTest",
64                 "int",
65                 "",
66                 Collections.singletonList(MethodModel.Variable.create("java.lang.String", "name")),
67                 Collections.<String JavaDoc>emptyList(),
68                 Collections.<Modifier>emptySet()
69                 );
70         String JavaDoc ejbql = "SELECT COUNT(o) FROM CmpLR o";
71         generator.generate(methodModel, true, true, true, ejbql);
72         
73         // local interface
74
final boolean[] found = new boolean[] { false };
75         JavaSource javaSource = JavaSource.forFileObject(beanClass);
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.CmpLRBean");
80                 for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
81                     if (executableElement.getSimpleName().contentEquals("ejbSelectTest")) {
82                         MethodTree methodTree = workingCopy.getTrees().getTree(executableElement);
83                         assertNull(methodTree.getBody());
84                         assertSame(TypeKind.INT, executableElement.getReturnType().getKind());
85                         TypeElement finderException = workingCopy.getElements().getTypeElement("javax.ejb.FinderException");
86                         assertTrue(executableElement.getThrownTypes().contains(finderException.asType()));
87                         VariableElement parameter = executableElement.getParameters().get(0);
88                         TypeElement stringTypeElement = workingCopy.getElements().getTypeElement(String JavaDoc.class.getName());
89                         assertTrue(workingCopy.getTypes().isSameType(stringTypeElement.asType(), parameter.asType()));
90                         found[0] = true;
91                     }
92                 }
93             }
94         });
95         assertTrue(found[0]);
96         
97         // entry in deployment descriptor
98
boolean queryFound = false;
99         for (Query query : entity.getQuery()) {
100             QueryMethod queryMethod = query.getQueryMethod();
101             if ("ejbSelectTest".equals(queryMethod.getMethodName())) {
102                 assertEquals(ejbql, query.getEjbQl());
103                 queryFound = true;
104             }
105         }
106         assertTrue(queryFound);
107         
108     }
109
110 }
111
Popular Tags