~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/metadata/support/test/unit/src/org/netbeans/modules/j2ee/metadata/model/api/support/annotation/AnnotationScannerTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.metadata.model.api.support.annotation;
 
43
 
 
44
import java.io.PrintStream;
 
45
import java.util.ArrayList;
 
46
import java.util.EnumSet;
 
47
import java.util.HashMap;
 
48
import java.util.HashSet;
 
49
import java.util.List;
 
50
import java.util.Map;
 
51
import java.util.Set;
 
52
import java.util.concurrent.Callable;
 
53
import java.util.logging.Level;
 
54
import javax.lang.model.element.AnnotationMirror;
 
55
import javax.lang.model.element.Element;
 
56
import javax.lang.model.element.ElementKind;
 
57
import javax.lang.model.element.TypeElement;
 
58
import org.netbeans.api.java.source.ClasspathInfo;
 
59
import org.netbeans.api.java.source.ElementHandle;
 
60
import org.netbeans.modules.j2ee.metadata.model.support.TestUtilities;
 
61
import org.netbeans.modules.j2ee.metadata.model.support.PersistenceTestCase;
 
62
import org.netbeans.modules.java.source.usages.RepositoryUpdater;
 
63
import org.openide.util.MapFormat;
 
64
 
 
65
/**
 
66
 *
 
67
 * @author Andrei Badea
 
68
 */
 
69
public class AnnotationScannerTest extends PersistenceTestCase {
 
70
 
 
71
    public AnnotationScannerTest(String testName) {
 
72
        super(testName);
 
73
    }
 
74
 
 
75
    protected Level logLevel() {
 
76
        // enabling logging
 
77
        return Level.INFO;
 
78
    }
 
79
 
 
80
    public PrintStream getLog() {
 
81
        return System.err;
 
82
    }
 
83
 
 
84
    public void testScanTypes() throws Exception {
 
85
        TestUtilities.copyStringToFileObject(srcFO, "Customer.java",
 
86
                "@javax.persistence.Entity()" +
 
87
                "public class Customer { }");
 
88
        TestUtilities.copyStringToFileObject(srcFO, "Foo.java",
 
89
                "public class Foo { }");
 
90
        TestUtilities.copyStringToFileObject(srcFO, "Item.java",
 
91
                "@javax.persistence.Entity()" +
 
92
                "public class Item { }");
 
93
        RepositoryUpdater.getDefault().scheduleCompilationAndWait(srcFO, srcFO).await();
 
94
        ClasspathInfo cpi = ClasspathInfo.create(srcFO);
 
95
        final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
 
96
        final Set<String> types = new HashSet<String>();
 
97
        helper.runJavaSourceTask(new Callable<Void>() {
 
98
            public Void call() throws InterruptedException {
 
99
                helper.getAnnotationScanner().findAnnotations("javax.persistence.Entity", AnnotationScanner.TYPE_KINDS, new AnnotationHandler() {
 
100
                    public void handleAnnotation(TypeElement type, Element element, AnnotationMirror annotation) {
 
101
                        types.add(type.getQualifiedName().toString());
 
102
                    }
 
103
                });
 
104
                return null;
 
105
            }
 
106
        });
 
107
        assertEquals(2, types.size());
 
108
        assertTrue(types.contains("Customer"));
 
109
        assertTrue(types.contains("Item"));
 
110
    }
 
111
 
 
112
    public void testPerformance() throws Exception {
 
113
        // Logger.getLogger(AnnotationScanner.class.getName()).setLevel(Level.FINEST);
 
114
        final int ENTITY_COUNT = 500;
 
115
        String template = TestUtilities.copyStreamToString(getClass().getResourceAsStream("Table.javax"));
 
116
        Map<String, String> args = new HashMap<String, String>();
 
117
        MapFormat format = new MapFormat(args);
 
118
        format.setLeftBrace("__");
 
119
        format.setRightBrace("__");
 
120
        for (int i = 0; i < ENTITY_COUNT; i++) {
 
121
            String name = "Table" + i;
 
122
            args.put("NUM", Integer.toString(i));
 
123
            args.put("NAME", name);
 
124
            String contents = format.format(template);
 
125
            TestUtilities.copyStringToFileObject(srcFO, name + ".java", contents);
 
126
        }
 
127
        long startTime = System.nanoTime();
 
128
        RepositoryUpdater.getDefault().scheduleCompilationAndWait(srcFO, srcFO).await();
 
129
        long compilationDoneTime = System.nanoTime();
 
130
        ClasspathInfo cpi = ClasspathInfo.create(srcFO);
 
131
        final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
 
132
        final int[] entityCount = { 0 };
 
133
        final long initialScanDoneTime[] = { 0L };
 
134
        final List<ElementHandle<TypeElement>> typeHandles  = new ArrayList<ElementHandle<TypeElement>>();
 
135
        helper.runJavaSourceTask(new Callable<Void>() {
 
136
            public Void call() throws InterruptedException {
 
137
                helper.getAnnotationScanner().findAnnotations("javax.persistence.Entity", AnnotationScanner.TYPE_KINDS, new AnnotationHandler() {
 
138
                    public void handleAnnotation(TypeElement type, Element element, AnnotationMirror annotation) {
 
139
                        typeHandles.add(ElementHandle.create(type));
 
140
                        entityCount[0]++;
 
141
                    }
 
142
                });
 
143
                initialScanDoneTime[0] = System.nanoTime();
 
144
                return null;
 
145
            }
 
146
        });
 
147
        assertEquals(ENTITY_COUNT, entityCount[0]);
 
148
        System.out.println("Compilation time (ms): " + (compilationDoneTime - startTime) / 1e6);
 
149
        System.out.println("Initial annotation scan time (ms): " + (initialScanDoneTime[0] - compilationDoneTime) / 1e6);
 
150
    }
 
151
    
 
152
    private ClasspathInfo createClasspathInfoForScanningAnnotations() throws Exception {
 
153
        TestUtilities.copyStringToFileObject(srcFO, "foo/MyClass.java",
 
154
                "package foo;" +
 
155
                "" +
 
156
                "import javax.annotation.Resource;" +
 
157
                "import javax.sql.DataSource;" +
 
158
                "" +
 
159
                "@Resource(name=\"myClass\")" +
 
160
                "public class MyClass {" +
 
161
                "   @Resource" +
 
162
                "   private DataSource myDS;" +
 
163
                "}");
 
164
        TestUtilities.copyStringToFileObject(srcFO, "foo/YourClass.java",
 
165
                "package foo;" +
 
166
                "" +
 
167
                "import javax.annotation.Resource;" +
 
168
                "import javax.sql.DataSource;" +
 
169
                "" +
 
170
                "public class YourClass {" +
 
171
                "   @Resource" +
 
172
                "   private void setYourDataSource(DataSource ds) {" +
 
173
                "   }" +
 
174
                "}");
 
175
        RepositoryUpdater.getDefault().scheduleCompilationAndWait(srcFO, srcFO).await();
 
176
        return ClasspathInfo.create(srcFO);
 
177
    }
 
178
    
 
179
    public void testScanAnnotationsOnClassesMethodsFields() throws Exception {
 
180
        final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
 
181
        final Set<String> elements = new HashSet<String>();
 
182
        
 
183
        helper.runJavaSourceTask(new Callable<Void>() {
 
184
            public Void call() throws InterruptedException{
 
185
                helper.getAnnotationScanner().findAnnotations(
 
186
                        "javax.annotation.Resource",
 
187
                        EnumSet.of(ElementKind.CLASS, ElementKind.METHOD, ElementKind.FIELD),
 
188
                        new AnnotationHandler() {
 
189
                            public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
 
190
                                elements.add(element.getSimpleName().toString());
 
191
                            }
 
192
                        });
 
193
                return null;
 
194
            }
 
195
        });
 
196
        assertEquals(3, elements.size());
 
197
        assertTrue(elements.contains("MyClass"));
 
198
        assertTrue(elements.contains("myDS"));
 
199
        assertTrue(elements.contains("setYourDataSource"));
 
200
    }
 
201
    
 
202
    public void testScanAnnotationsOnMethodsFields() throws Exception {
 
203
        final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
 
204
        final Set<String> elements = new HashSet<String>();
 
205
        
 
206
        helper.runJavaSourceTask(new Callable<Void>() {
 
207
            public Void call() throws InterruptedException {
 
208
                helper.getAnnotationScanner().findAnnotations(
 
209
                        "javax.annotation.Resource",
 
210
                        EnumSet.of(ElementKind.METHOD, ElementKind.FIELD),
 
211
                        new AnnotationHandler() {
 
212
                            public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
 
213
                                elements.add(element.getSimpleName().toString());
 
214
                            }
 
215
                        });
 
216
                return null;
 
217
            }
 
218
        });
 
219
        assertEquals(2, elements.size());
 
220
        assertTrue(elements.contains("myDS"));
 
221
        assertTrue(elements.contains("setYourDataSource"));
 
222
    }
 
223
    
 
224
    public void testScanAnnotationsOnClasses() throws Exception {
 
225
        final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
 
226
        final Set<String> elements = new HashSet<String>();
 
227
        
 
228
        helper.runJavaSourceTask(new Callable<Void>() {
 
229
            public Void call() throws InterruptedException {
 
230
                helper.getAnnotationScanner().findAnnotations(
 
231
                        "javax.annotation.Resource",
 
232
                        EnumSet.of(ElementKind.CLASS),
 
233
                        new AnnotationHandler() {
 
234
                            public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
 
235
                                assertSame(typeElement, element);
 
236
                                elements.add(element.getSimpleName().toString());
 
237
                            }
 
238
                        });
 
239
                return null;
 
240
            }
 
241
        });
 
242
        assertEquals(1, elements.size());
 
243
        assertTrue(elements.contains("MyClass"));
 
244
    }
 
245
    
 
246
    public void testScanAnnotationsOnMethods() throws Exception {
 
247
        final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
 
248
        final Set<String> elements = new HashSet<String>();
 
249
        
 
250
        helper.runJavaSourceTask(new Callable<Void>() {
 
251
            public Void call() throws InterruptedException {
 
252
                helper.getAnnotationScanner().findAnnotations(
 
253
                        "javax.annotation.Resource",
 
254
                        EnumSet.of(ElementKind.METHOD),
 
255
                        new AnnotationHandler() {
 
256
                            public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
 
257
                                assertEquals("foo.YourClass", typeElement.getQualifiedName().toString());
 
258
                                elements.add(element.getSimpleName().toString());
 
259
                            }
 
260
                        });
 
261
                return null;
 
262
            }
 
263
        });
 
264
        assertEquals(1, elements.size());
 
265
        assertTrue(elements.contains("setYourDataSource"));
 
266
    }
 
267
    
 
268
    public void testScanAnnotationsOnFields() throws Exception {
 
269
        final AnnotationModelHelper helper = AnnotationModelHelper.create(createClasspathInfoForScanningAnnotations());
 
270
        final Set<String> elements = new HashSet<String>();
 
271
        
 
272
        helper.runJavaSourceTask(new Callable<Void>() {
 
273
            public Void call() throws InterruptedException {
 
274
                helper.getAnnotationScanner().findAnnotations(
 
275
                        "javax.annotation.Resource",
 
276
                        EnumSet.of(ElementKind.FIELD),
 
277
                        new AnnotationHandler() {
 
278
                            public void handleAnnotation(TypeElement typeElement, Element element, AnnotationMirror annotationMirror) {
 
279
                                assertEquals("foo.MyClass", typeElement.getQualifiedName().toString());
 
280
                                elements.add(element.getSimpleName().toString());
 
281
                            }
 
282
                        });
 
283
                return null;
 
284
            }
 
285
        });
 
286
        assertEquals(1, elements.size());
 
287
        assertTrue(elements.contains("myDS"));
 
288
    }
 
289
}