KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > action > EntityManagerGenerationTestSupport


1 /*
2  * ContainerManagedJTANonInjectableInWebTest.java
3  * JUnit based test
4  *
5  * Created on 1 November 2006, 16:00
6  */

7
8 package org.netbeans.modules.j2ee.persistence.action;
9
10 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy;
11 import junit.framework.*;
12 import com.sun.source.tree.ClassTree;
13 import com.sun.source.tree.CompilationUnitTree;
14 import com.sun.source.tree.Tree;
15 import java.io.IOException JavaDoc;
16 import org.netbeans.api.java.source.CancellableTask;
17 import org.netbeans.api.java.source.JavaSource;
18 import org.netbeans.api.java.source.JavaSource.Phase;
19 import org.netbeans.api.java.source.TreeMaker;
20 import org.netbeans.api.java.source.WorkingCopy;
21 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
22 import org.netbeans.modules.j2ee.persistence.sourcetestsupport.SourceTestSupport;
23 import org.openide.filesystems.FileObject;
24
25 /**
26  *
27  * @author Erno Mononen
28  */

29 public abstract class EntityManagerGenerationTestSupport extends SourceTestSupport{
30     
31     public EntityManagerGenerationTestSupport(String JavaDoc testName){
32         super(testName);
33     }
34     
35     
36     protected FileObject generate(FileObject targetFo, final GenerationOptions options) throws IOException JavaDoc{
37         
38         JavaSource targetSource = JavaSource.forFileObject(targetFo);
39         
40         CancellableTask task = new CancellableTask<WorkingCopy>() {
41             
42             public void cancel() {
43             }
44             
45             public void run(WorkingCopy workingCopy) throws Exception JavaDoc {
46                 
47                 workingCopy.toPhase(Phase.RESOLVED);
48                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
49                 TreeMaker make = workingCopy.getTreeMaker();
50                 
51                 for (Tree typeDeclaration : cut.getTypeDecls()){
52                     if (Tree.Kind.CLASS == typeDeclaration.getKind()){
53                         ClassTree clazz = (ClassTree) typeDeclaration;
54                         ClassTree modifiedClazz = getStrategy(workingCopy, make, clazz, options).generate();
55                         workingCopy.rewrite(clazz, modifiedClazz);
56                     }
57                 }
58                 
59             }
60         };
61         targetSource.runModificationTask(task).commit();
62         
63         return targetFo;
64         
65     }
66
67     /**
68      * @return a persistence unit with name "MyPersistenceUnit".
69      */

70     protected PersistenceUnit getPersistenceUnit(){
71         PersistenceUnit punit = new PersistenceUnit();
72         punit.setName("MyPersistenceUnit");
73         return punit;
74     }
75     
76     protected EntityManagerGenerationStrategy getStrategy(WorkingCopy workingCopy, TreeMaker make, ClassTree clazz, GenerationOptions options){
77         
78         EntityManagerGenerationStrategy result = null;
79         
80         try{
81             result = getStrategyClass().newInstance();
82             result.setClassTree(clazz);
83             result.setWorkingCopy(workingCopy);
84             result.setGenerationOptions(options);
85             result.setTreeMaker(make);
86             result.setPersistenceUnit(getPersistenceUnit());
87         } catch (IllegalAccessException JavaDoc iae){
88             throw new RuntimeException JavaDoc(iae);
89         } catch (InstantiationException JavaDoc ie){
90             throw new RuntimeException JavaDoc(ie);
91         }
92         
93         return result;
94     }
95
96     
97     protected abstract Class JavaDoc<? extends EntityManagerGenerationStrategy> getStrategyClass();
98
99     
100 }
101
102
Popular Tags