1 19 20 package org.netbeans.modules.j2ee.ejbcore.action; 21 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import java.util.Map ; 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 53 public class UseDatabaseGeneratorTest extends TestBase { 54 55 public UseDatabaseGeneratorTest(String testName) { 56 super(testName); 57 } 58 59 public void testGenerate() throws IOException { 60 UseDatabaseGenerator generator = new UseDatabaseGenerator(); 61 62 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 { 73 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 74 TypeElement typeElement = elementHandle.resolve(workingCopy); 75 MethodModel methodModel = MethodModel.create( 76 "getTestJndiName", 77 javax.sql.DataSource .class.getName(), 78 null, 79 Collections.<MethodModel.Variable>emptyList(), 80 Collections.singletonList(javax.naming.NamingException .class.getName()), 81 Collections.singleton(Modifier.PRIVATE) 82 ); 83 assertTrue(containsMethod(workingCopy, methodModel, typeElement)); 84 } 85 }); 86 87 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 { 98 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 99 TypeElement typeElement = elementHandle2.resolve(workingCopy); 100 } 103 }); 104 } 105 106 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 name, String jndiName) { 118 List <VariableElement> elements = ElementFilter.fieldsIn(typeElement.getEnclosedElements()); 119 VariableElement variableElement = (VariableElement) elements.get(0); 120 assertTrue(variableElement.getSimpleName().contentEquals(name)); DeclaredType declaredType = (DeclaredType) variableElement.asType(); 122 TypeElement returnTypeElement = (TypeElement) declaredType.asElement(); 123 assertTrue(returnTypeElement.getQualifiedName().contentEquals("javax.sql.DataSource")); 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")); Map.Entry <? extends ExecutableElement, ? extends AnnotationValue> entry = annotationMirror.getElementValues().entrySet().iterator().next(); 129 String attributeName = entry.getKey().getSimpleName().toString(); 130 String attributeValue = (String ) entry.getValue().getValue(); 131 assertEquals("name", attributeName); assertEquals(jndiName, attributeValue); 133 } 134 135 private static class DatasourceImpl implements Datasource { 136 137 public DatasourceImpl() {} 138 139 public String getJndiName() { return "testJndiName"; } 140 141 public String getUrl() { return "testUrl"; } 142 143 public String getUsername() { return "testUsername"; } 144 145 public String getPassword() { return "testPassword"; } 146 147 public String getDriverClassName() { return "testDriverClassName"; } 148 149 public String getDisplayName() { return "testDisplayName"; } 150 151 } 152 153 } 154 | Popular Tags |