KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > _RetoucheUtilTest


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;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
45  *
46  * @author Martin Adamek
47  */

48 public class _RetoucheUtilTest extends TestBase{
49     
50     public _RetoucheUtilTest(String JavaDoc testName) {
51         super(testName);
52     }
53
54     public void testGenerateInjectedField() throws IOException JavaDoc {
55         // creates and tests following field:
56
// @javax.annotation.Resource(name="MyJndiName")
57
// javax.sql.DataSource myResource;
58
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         // creates and tests following field:
73
// @javax.annotation.Resource(name="MyJndiName")
74
// static javax.sql.DataSource myResource;
75
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 JavaDoc {
91         TestUtilities.copyStringToFileObject(testFO,
92                 "package foo;" +
93                 "public class TestClass {" +
94                 "}");
95         final List JavaDoc<ElementHandle<TypeElement>> result1 = new ArrayList JavaDoc<ElementHandle<TypeElement>>(1);
96         JavaSource javaSource = JavaSource.forFileObject(testFO);
97         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
98             public void run(CompilationController controller) throws IOException JavaDoc {
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 JavaDoc<ElementHandle<TypeElement>> result2 = new ArrayList JavaDoc<ElementHandle<TypeElement>>(1);
111         javaSource = JavaSource.forFileObject(testFO);
112         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
113             public void run(CompilationController controller) throws IOException JavaDoc {
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 JavaDoc {
123         JavaSource javaSource = JavaSource.forFileObject(fileObject);
124         javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
125             public void run(CompilationController controller) throws IOException JavaDoc {
126                 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
127                 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement();
128                 List JavaDoc<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")); // field name
132
DeclaredType declaredType = (DeclaredType) variableElement.asType();
133                 TypeElement returnTypeElement = (TypeElement) declaredType.asElement();
134                 assertTrue(returnTypeElement.getQualifiedName().contentEquals("javax.sql.DataSource")); // field type
135
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")); // annotation type
139
Map.Entry JavaDoc<? extends ExecutableElement, ? extends AnnotationValue> entry = annotationMirror.getElementValues().entrySet().iterator().next();
140                 String JavaDoc attributeName = entry.getKey().getSimpleName().toString();
141                 String JavaDoc attributeValue = (String JavaDoc) entry.getValue().getValue();
142                 assertEquals("name", attributeName); // attributes
143
assertEquals("MyJndiName", attributeValue);
144             }
145         }, true);
146     }
147
148 }
149
Popular Tags