KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > spi > entitymanagergenerator > EntityManagerGenerationStrategySupportTest


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.persistence.spi.entitymanagergenerator;
21
22 import org.netbeans.modules.j2ee.persistence.action.*;
23 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategySupport;
24 import com.sun.source.tree.ClassTree;
25 import com.sun.source.tree.CompilationUnitTree;
26 import com.sun.source.tree.Tree;
27 import java.io.File JavaDoc;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.TypeElement;
30 import org.netbeans.api.java.source.CancellableTask;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.JavaSource.Phase;
33 import org.netbeans.api.java.source.TreeMaker;
34 import org.netbeans.api.java.source.WorkingCopy;
35 import org.netbeans.jackpot.test.TestUtilities;
36 import org.netbeans.modules.j2ee.persistence.util.AbstractTask;
37 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy;
38 import org.openide.filesystems.FileUtil;
39
40 /**
41  * Tests for the helper methods in EntityManagerGenerationStrategy.
42  *
43  * @author Erno Mononen
44  */

45 public class EntityManagerGenerationStrategySupportTest extends EntityManagerGenerationTestSupport{
46     
47     public EntityManagerGenerationStrategySupportTest(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     public void testGetAnnotationOnClass() throws Exception JavaDoc{
52         
53         final String JavaDoc annotation = "java.lang.Deprecated"; //some annotation
54

55         File JavaDoc testFile = new File JavaDoc(getWorkDir(), "Test.java");
56         
57         TestUtilities.copyStringToFile(testFile,
58                 "package org.netbeans.test;\n\n" +
59                 "import java.util.*;\n\n" +
60                 "@" + annotation + "\n" +
61                 "public class Test {\n" +
62                 "}"
63                 );
64         
65         searchAnnotation(testFile, annotation, true);
66     }
67     
68     public void testGetAnnotationOnField() throws Exception JavaDoc{
69         
70         final String JavaDoc annotation = "java.lang.Deprecated"; //some annotation
71

72         File JavaDoc testFile = new File JavaDoc(getWorkDir(), "Test.java");
73         
74         TestUtilities.copyStringToFile(testFile,
75                 "package org.netbeans.test;\n\n" +
76                 "import java.util.*;\n\n" +
77                 "public class Test {\n" +
78                 "@" + annotation + "\n" +
79                 "Object myField;\n" +
80                 "}"
81                 );
82         
83         searchAnnotation(testFile, annotation, true);
84         
85     }
86     
87     public void testGetAnnotationOnMethod() throws Exception JavaDoc{
88         
89         final String JavaDoc annotation = "java.lang.Deprecated"; //some annotation
90

91         File JavaDoc testFile = new File JavaDoc(getWorkDir(), "Test.java");
92         
93         TestUtilities.copyStringToFile(testFile,
94                 "package org.netbeans.test;\n\n" +
95                 "import java.util.*;\n\n" +
96                 "public class Test {\n" +
97                 "@" + annotation + "\n" +
98                 "Object method(){\n" +
99                 "return null;\n" +
100                 "}\n" +
101                 "}"
102                 );
103         
104         searchAnnotation(testFile, annotation, true);
105         
106     }
107     
108     
109     public void testGetField() throws Exception JavaDoc{
110         
111         final String JavaDoc field = "java.lang.String"; //some field
112

113         File JavaDoc testFile = new File JavaDoc(getWorkDir(), "Test.java");
114         
115         TestUtilities.copyStringToFile(testFile,
116                 "package org.netbeans.test;\n\n" +
117                 "import java.util.*;\n\n" +
118                 "public class Test {\n" +
119                 "private " + field + " myField;\n" +
120                 "}"
121                 );
122         
123         searchField(testFile, field, true);
124         // test for searching a field that does not exist
125
searchField(testFile, "java.lang.Object", false);
126         
127     }
128     
129     private void searchAnnotation(File JavaDoc testFile, final String JavaDoc annotation, final boolean expectSuccess) throws Exception JavaDoc {
130         JavaSource targetSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
131         
132         CancellableTask task = new TaskSupport() {
133             void doAsserts(EntityManagerGenerationStrategySupport strategy) {
134                 Element result = strategy.getAnnotation(annotation);
135                 if (expectSuccess){
136                     assertNotNull(result);
137                     assertTrue(((TypeElement)result).getQualifiedName().contentEquals(annotation));
138                 } else {
139                     assertNull(result);
140                 }
141             }
142         };
143         
144         targetSource.runModificationTask(task);
145     }
146     
147     private void searchField(File JavaDoc testFile, final String JavaDoc field, final boolean expectSuccess) throws Exception JavaDoc {
148         JavaSource targetSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
149         
150         CancellableTask task = new TaskSupport() {
151             void doAsserts(EntityManagerGenerationStrategySupport strategy) {
152                 Element result = strategy.getField(field);
153                 if (expectSuccess){
154                     assertNotNull(result);
155                     assertTrue(((TypeElement)result).getQualifiedName().contentEquals(field));
156                 } else {
157                     assertNull(result);
158                 }
159             }
160         };
161         
162         targetSource.runModificationTask(task);
163     }
164     
165     // a helper class for avoiding some duplicate code
166
private abstract class TaskSupport extends AbstractTask<WorkingCopy> {
167         
168         public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
169             
170             workingCopy.toPhase(Phase.RESOLVED);
171             CompilationUnitTree cut = workingCopy.getCompilationUnit();
172             TreeMaker make = workingCopy.getTreeMaker();
173             
174             for (Tree typeDeclaration : cut.getTypeDecls()){
175                 if (Tree.Kind.CLASS == typeDeclaration.getKind()){
176                     ClassTree clazz = (ClassTree) typeDeclaration;
177                     EntityManagerGenerationStrategySupport strategy =
178                             (EntityManagerGenerationStrategySupport) getStrategy(workingCopy, make, clazz, new GenerationOptions());
179                     doAsserts(strategy);
180                 } else {
181                     fail("No class found"); // should not happen
182
}
183             }
184         }
185         
186         abstract void doAsserts(EntityManagerGenerationStrategySupport strategy);
187         
188     }
189     
190     public static class StubEntityManagerGenerationStrategy extends EntityManagerGenerationStrategySupport{
191         
192         public ClassTree generate() {
193             return null;
194         }
195         
196     }
197
198     protected Class JavaDoc<? extends EntityManagerGenerationStrategy> getStrategyClass() {
199         return StubEntityManagerGenerationStrategy.class;
200     }
201 }
202
Popular Tags