KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > cmp > GenerateEntityClassesTest


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.test.j2ee.cmp;
21
22 import java.io.File JavaDoc;
23 import java.io.FilenameFilter JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import org.netbeans.api.progress.ProgressHandle;
27 import org.netbeans.api.progress.ProgressHandleFactory;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.SourceGroup;
31 import junit.framework.Test;
32 import junit.textui.TestRunner;
33 import org.netbeans.jmi.javamodel.JavaClass;
34 import org.netbeans.junit.NbTestSuite;
35 import org.netbeans.modules.dbschema.SchemaElement;
36 import org.netbeans.modules.dbschema.SchemaElementUtil;
37 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
38 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
39 import org.netbeans.modules.j2ee.earproject.ui.LogicalViewProvider;
40 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
41 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.DBSchemaTableProvider;
42 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.JavaPersistenceGenerator;
43 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.PersistenceGenerator;
44 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.ProgressPanel;
45 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.SelectedTables;
46 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.TableClosure;
47 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.TableProvider;
48 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
49 import org.netbeans.test.j2ee.multiview.Utils;
50 import org.netbeans.test.j2ee.multiview.DDTestCase;
51 import org.openide.actions.SaveAllAction;
52 import org.openide.cookies.EditCookie;
53 import org.openide.filesystems.FileObject;
54 import org.openide.loaders.DataObject;
55 import org.openide.nodes.Node;
56 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.RelatedCMPHelper;
57 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.RelatedCMPWizard;
58
59 /**
60  *
61  * @author jhorvath
62  */

63 public class GenerateEntityClassesTest extends DDTestCase {
64     public static final String JavaDoc EJB_PROJECT_NAME = "TestGenerateEntity";
65     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
66
67
68     
69     /** Creates a new instance of GenerateEntityClassesTest */
70     public GenerateEntityClassesTest(String JavaDoc testName) {
71         super(testName);
72     }
73     
74     
75     
76     
77     protected void setUp() throws Exception JavaDoc {
78         super.setUp();
79         System.out.println("######## "+getName()+" #######");
80     }
81     
82     protected void tearDown() throws Exception JavaDoc {
83     }
84     
85     public static Test suite() {
86         NbTestSuite suite = new NbTestSuite();
87         suite.addTest(new GenerateEntityClassesTest("testOpenProject"));
88         suite.addTest(new GenerateEntityClassesTest("testGenerateBeans"));
89         
90         return suite;
91     }
92     
93     /** Use for execution inside IDE */
94     public static void main(java.lang.String JavaDoc[] args) {
95         // run only selected test case
96
TestRunner.run(suite());
97     }
98     
99     /*
100      * Method open project
101      *
102      */

103     public void testOpenProject() throws Exception JavaDoc{
104         File JavaDoc projectDir = new File JavaDoc(EJB_PROJECT_PATH);
105         project = (Project)J2eeProjectSupport.openProject(projectDir);
106         assertNotNull("Project is null.", project);
107         Thread.sleep(1000);
108        /*
109         EjbJarProject ejbJarProject = (EjbJarProject)project;
110         ddFo = ejbJarProject.getAPIEjbJar().getDeploymentDescriptor(); // deployment descriptor
111         assertNotNull("ejb-jar.xml FileObject is null.", ddFo);
112         
113         ejbJar = DDProvider.getDefault().getDDRoot(ddFo);
114         
115         ddObj = (EjbJarMultiViewDataObject)DataObject.find(ddFo); //MultiView Editor
116         assertNotNull("MultiViewDO is null.",ddObj);
117         
118         EditCookie edit = (EditCookie)ddObj.getCookie(EditCookie.class);
119         edit.edit();
120         Thread.sleep(1000);
121         Utils.waitForAWTDispatchThread();
122         **/

123     }
124     
125     public void testGenerateBeans() throws Exception JavaDoc{
126         ProgressPanel progressPanel;
127         Thread.sleep(4000);
128         PersistenceGenerator generator = new JavaPersistenceGenerator();
129         FileObject config=project.getProjectDirectory().getFileObject("/src/conf/");
130         
131         RelatedCMPHelper relatedCMPHelper=new RelatedCMPHelper(project, config, generator);
132         FileObject schemaFo=project.getProjectDirectory().getFileObject("/src/conf/testSchema.dbschema");
133         SchemaElement se=SchemaElementUtil.forName(schemaFo);
134         
135         relatedCMPHelper.setTableSource(se, schemaFo);
136         relatedCMPHelper.setPackageName("test");
137         
138         TableProvider tableProvider = new DBSchemaTableProvider(se, generator);
139         TableClosure tableClosure = new TableClosure(tableProvider);
140         tableClosure.setClosureEnabled(false);
141         tableClosure.addAllTables();
142         Set JavaDoc selected = tableClosure.getSelectedTables();
143         System.err.println("*** selected "+selected.size());
144
145         Set JavaDoc available = tableClosure.getAvailableTables();
146         System.err.println("*** available "+available.size());
147         relatedCMPHelper.setTableClosure(tableClosure);
148         
149         SourceGroup sourceGroup = ProjectUtils.getSources(project).getSourceGroups("java")[0];
150         relatedCMPHelper.setLocation(sourceGroup);
151         SelectedTables selectedTables=new SelectedTables(generator, tableClosure, sourceGroup, "test");
152         relatedCMPHelper.setSelectedTables(selectedTables);
153         
154         ProgressHandle handle = ProgressHandleFactory.createHandle("test");
155         progressPanel = new ProgressPanel();
156         handle.start();
157         relatedCMPHelper.buildBeans();
158         generator.generateBeans(progressPanel, relatedCMPHelper, schemaFo, handle, false);
159         Set JavaDoc created = generator.createdObjects();
160         System.err.println("*** created size: "+created.size());
161             
162         
163         handle.finish();
164         Thread.sleep(4000);
165         
166         // save all created files
167
SaveAllAction a = (SaveAllAction)SaveAllAction.get(SaveAllAction.class);
168         a.performAction();
169         
170      
171         org.netbeans.test.j2ee.lib.Utils utils = new org.netbeans.test.j2ee.lib.Utils(this);
172         
173         File JavaDoc dbBeansDir = new File JavaDoc(EJB_PROJECT_PATH + File.separator + "src" + File.separator + "java" + File.separator + "test");
174         String JavaDoc beanFiles[] = dbBeansDir.list(new FilenameFilter JavaDoc() {
175             public boolean accept(File JavaDoc dir, String JavaDoc name) {
176                 return name.endsWith(".java");
177             }
178         });
179         //if (beanFiles.length != 12)
180
// fail("12 generated db bean files are expected in "+dbBeansDir);
181
utils.assertFiles(dbBeansDir, beanFiles, getName()+"_");
182         
183     }
184     
185     protected Node getEjbNode(JavaClass beanClass, String JavaDoc ejbName) {
186         Project[] allProjects = org.netbeans.modules.j2ee.ejbcore.Utils.getCallableEjbProjects(project);
187         for (int i = 0; i < allProjects.length; i++) {
188             LogicalViewProvider lvp =
189                     (LogicalViewProvider) allProjects[i].getLookup().lookup(LogicalViewProvider.class);
190             Node projectView = lvp.createLogicalView();
191             Node [] nodes = projectView.getChildren().getNodes();
192             for (int j = 0; j < nodes.length; j++) {
193                 if ("EJBS".equals(nodes[j].getName())) {
194                     Node [] ejbs = nodes[j].getChildren().getNodes();
195                     for (int k = 0; k < ejbs.length; k++) {
196                         System.out.println("*** "+ejbs[k].getName());
197                         if (ejbName.equals(ejbs[k].getName())) return ejbs[k];
198                     }
199                 }
200             }
201         }
202         return null;
203     }
204     
205     
206     
207 }
208
Popular Tags