KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.lang.model.element.AnnotationMirror;
27 import javax.lang.model.element.AnnotationValue;
28 import javax.lang.model.element.ExecutableElement;
29 import javax.lang.model.element.Modifier;
30 import javax.lang.model.element.TypeElement;
31 import javax.lang.model.element.VariableElement;
32 import javax.lang.model.type.DeclaredType;
33 import javax.lang.model.util.ElementFilter;
34 import org.netbeans.api.java.source.ElementHandle;
35 import org.netbeans.api.java.source.JavaSource;
36 import org.netbeans.api.java.source.WorkingCopy;
37 import org.netbeans.modules.j2ee.common.method.MethodModel;
38 import org.netbeans.modules.j2ee.common.method.MethodModelSupport;
39 import org.netbeans.modules.j2ee.common.source.AbstractTask;
40 import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
41 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
42 import org.netbeans.modules.j2ee.ejbcore.test.TestBase;
43 import org.openide.filesystems.FileObject;
44 import org.openide.nodes.AbstractNode;
45 import org.openide.nodes.Children;
46 import org.openide.nodes.Node;
47 import org.openide.util.lookup.Lookups;
48
49 /**
50  *
51  * @author Martin Adamek
52  */

53 public class UseDatabaseGeneratorTest extends TestBase {
54     
55     public UseDatabaseGeneratorTest(String JavaDoc testName) {
56         super(testName);
57     }
58     
59     public void testGenerate() throws IOException JavaDoc {
60         UseDatabaseGenerator generator = new UseDatabaseGenerator();
61         
62         // EJB 2.1 Stateless Session Bean
63
TestModule testModule = ejb14();
64         FileObject beanClass = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRBean.java");
65         Node node = new AbstractNode(Children.LEAF, Lookups.singleton(beanClass));
66         final ElementHandle<TypeElement> elementHandle = _RetoucheUtil.getJavaClassFromNode(node);
67         Datasource datasource = new DatasourceImpl();
68         generator.generate(beanClass, elementHandle, datasource, false, null);
69         
70         JavaSource javaSource = JavaSource.forFileObject(beanClass);
71         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
72             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
73                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
74                 TypeElement typeElement = elementHandle.resolve(workingCopy);
75                 MethodModel methodModel = MethodModel.create(
76                         "getTestJndiName",
77                         javax.sql.DataSource JavaDoc.class.getName(),
78                         null,
79                         Collections.<MethodModel.Variable>emptyList(),
80                         Collections.singletonList(javax.naming.NamingException JavaDoc.class.getName()),
81                         Collections.singleton(Modifier.PRIVATE)
82                         );
83                 assertTrue(containsMethod(workingCopy, methodModel, typeElement));
84             }
85         });
86         
87         // EJB 3.0 Stateless Session Bean
88
testModule = ejb50();
89         beanClass = testModule.getSources()[0].getFileObject("statelesslr/StatelessLRBean.java");
90         node = new AbstractNode(Children.LEAF, Lookups.singleton(beanClass));
91         final ElementHandle<TypeElement> elementHandle2 = _RetoucheUtil.getJavaClassFromNode(node);
92         datasource = new DatasourceImpl();
93         generator.generate(beanClass, elementHandle2, datasource, false, null);
94         
95         javaSource = JavaSource.forFileObject(beanClass);
96         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
97             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
98                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
99                 TypeElement typeElement = elementHandle2.resolve(workingCopy);
100                 //TODO: RETOUCHE not working because of missing annotation based model
101
// checkDatasourceField(workingCopy, typeElement, "testJndiName", "testJndiName");
102
}
103         });
104     }
105     
106     // private helpers =========================================================
107

108     private static boolean containsMethod(WorkingCopy workingCopy, MethodModel methodModel, TypeElement typeElement) {
109         for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
110             if (MethodModelSupport.isSameMethod(workingCopy, executableElement, methodModel)) {
111                 return true;
112             }
113         }
114         return false;
115     }
116     
117     private static void checkDatasourceField(WorkingCopy workingCopy, TypeElement typeElement, String JavaDoc name, String JavaDoc jndiName) {
118         List JavaDoc<VariableElement> elements = ElementFilter.fieldsIn(typeElement.getEnclosedElements());
119         VariableElement variableElement = (VariableElement) elements.get(0);
120         assertTrue(variableElement.getSimpleName().contentEquals(name)); // field name
121
DeclaredType declaredType = (DeclaredType) variableElement.asType();
122         TypeElement returnTypeElement = (TypeElement) declaredType.asElement();
123         assertTrue(returnTypeElement.getQualifiedName().contentEquals("javax.sql.DataSource")); // field type
124
AnnotationMirror annotationMirror = variableElement.getAnnotationMirrors().get(0);
125         DeclaredType annotationDeclaredType = annotationMirror.getAnnotationType();
126         TypeElement annotationTypeElement = (TypeElement) annotationDeclaredType.asElement();
127         assertTrue(annotationTypeElement.getQualifiedName().contentEquals("javax.annotation.Resource")); // annotation type
128
Map.Entry JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> entry = annotationMirror.getElementValues().entrySet().iterator().next();
129         String JavaDoc attributeName = entry.getKey().getSimpleName().toString();
130         String JavaDoc attributeValue = (String JavaDoc) entry.getValue().getValue();
131         assertEquals("name", attributeName); // attributes
132
assertEquals(jndiName, attributeValue);
133     }
134
135     private static class DatasourceImpl implements Datasource {
136         
137         public DatasourceImpl() {}
138         
139         public String JavaDoc getJndiName() { return "testJndiName"; }
140
141         public String JavaDoc getUrl() { return "testUrl"; }
142
143         public String JavaDoc getUsername() { return "testUsername"; }
144
145         public String JavaDoc getPassword() { return "testPassword"; }
146
147         public String JavaDoc getDriverClassName() { return "testDriverClassName"; }
148
149         public String JavaDoc getDisplayName() { return "testDisplayName"; }
150         
151     }
152     
153 }
154
Popular Tags