1 19 20 package org.netbeans.modules.j2ee.ejbcore; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.Map ; 27 import javax.lang.model.element.AnnotationMirror; 28 import javax.lang.model.element.AnnotationValue; 29 import javax.lang.model.element.ExecutableElement; 30 import javax.lang.model.element.Modifier; 31 import javax.lang.model.element.TypeElement; 32 import javax.lang.model.element.VariableElement; 33 import javax.lang.model.type.DeclaredType; 34 import javax.lang.model.util.ElementFilter; 35 import org.netbeans.api.java.source.CompilationController; 36 import org.netbeans.api.java.source.ElementHandle; 37 import org.netbeans.api.java.source.JavaSource; 38 import org.netbeans.modules.j2ee.common.source.AbstractTask; 39 import org.netbeans.modules.j2ee.common.source.SourceUtils; 40 import org.netbeans.modules.j2ee.ejbcore.test.TestBase; 41 import org.netbeans.modules.j2ee.ejbcore.test.TestUtilities; 42 import org.openide.filesystems.FileObject; 43 44 48 public class _RetoucheUtilTest extends TestBase{ 49 50 public _RetoucheUtilTest(String testName) { 51 super(testName); 52 } 53 54 public void testGenerateInjectedField() throws IOException { 55 TestUtilities.copyStringToFileObject(testFO, 59 "package foo;" + 60 "public class TestClass {" + 61 "}"); 62 _RetoucheUtil.generateAnnotatedField( 63 testFO, 64 "foo.TestClass", 65 "javax.annotation.Resource", 66 "myResource", 67 "javax.sql.DataSource", 68 Collections.singletonMap("name", "MyJndiName"), 69 false); 70 testAddedField(testFO, false); 71 72 TestUtilities.copyStringToFileObject(testFO, 76 "package foo;" + 77 "public class TestClass {" + 78 "}"); 79 _RetoucheUtil.generateAnnotatedField( 80 testFO, 81 "foo.TestClass", 82 "javax.annotation.Resource", 83 "myResource", 84 "javax.sql.DataSource", 85 Collections.singletonMap("name", "MyJndiName"), 86 true); 87 testAddedField(testFO, true); 88 } 89 90 public void testIsInterface() throws IOException { 91 TestUtilities.copyStringToFileObject(testFO, 92 "package foo;" + 93 "public class TestClass {" + 94 "}"); 95 final List <ElementHandle<TypeElement>> result1 = new ArrayList <ElementHandle<TypeElement>>(1); 96 JavaSource javaSource = JavaSource.forFileObject(testFO); 97 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 98 public void run(CompilationController controller) throws IOException { 99 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 100 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement(); 101 result1.add(ElementHandle.create(typeElement)); 102 } 103 }, true); 104 assertFalse(_RetoucheUtil.isInterface(testFO, result1.get(0))); 105 106 TestUtilities.copyStringToFileObject(testFO, 107 "package foo;" + 108 "public interface TestClass {" + 109 "}"); 110 final List <ElementHandle<TypeElement>> result2 = new ArrayList <ElementHandle<TypeElement>>(1); 111 javaSource = JavaSource.forFileObject(testFO); 112 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 113 public void run(CompilationController controller) throws IOException { 114 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 115 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement(); 116 result2.add(ElementHandle.create(typeElement)); 117 } 118 }, true); 119 assertTrue(_RetoucheUtil.isInterface(testFO, result2.get(0))); 120 } 121 122 private void testAddedField(FileObject fileObject, final boolean isStatic) throws IOException { 123 JavaSource javaSource = JavaSource.forFileObject(fileObject); 124 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 125 public void run(CompilationController controller) throws IOException { 126 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 127 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement(); 128 List <VariableElement> elements = ElementFilter.fieldsIn(typeElement.getEnclosedElements()); 129 VariableElement variableElement = (VariableElement) elements.get(0); 130 assertEquals(isStatic, variableElement.getModifiers().contains(Modifier.STATIC)); 131 assertTrue(variableElement.getSimpleName().contentEquals("myResource")); DeclaredType declaredType = (DeclaredType) variableElement.asType(); 133 TypeElement returnTypeElement = (TypeElement) declaredType.asElement(); 134 assertTrue(returnTypeElement.getQualifiedName().contentEquals("javax.sql.DataSource")); AnnotationMirror annotationMirror = variableElement.getAnnotationMirrors().get(0); 136 DeclaredType annotationDeclaredType = annotationMirror.getAnnotationType(); 137 TypeElement annotationTypeElement = (TypeElement) annotationDeclaredType.asElement(); 138 assertTrue(annotationTypeElement.getQualifiedName().contentEquals("javax.annotation.Resource")); Map.Entry <? extends ExecutableElement, ? extends AnnotationValue> entry = annotationMirror.getElementValues().entrySet().iterator().next(); 140 String attributeName = entry.getKey().getSimpleName().toString(); 141 String attributeValue = (String ) entry.getValue().getValue(); 142 assertEquals("name", attributeName); assertEquals("MyJndiName", attributeValue); 144 } 145 }, true); 146 } 147 148 } 149 | Popular Tags |