KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import junit.framework.Test;
25 import junit.textui.TestRunner;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
30 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
31 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
32 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
33 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
34 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
35 import org.netbeans.modules.j2ee.ddloaders.multiview.CmpFieldHelper;
36 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject;
37 import org.netbeans.modules.j2ee.ddloaders.multiview.EntityHelper;
38 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
39 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
40 import org.netbeans.test.j2ee.lib.Utils;
41 import org.netbeans.test.j2ee.refactoring.Utility;
42 import org.openide.filesystems.FileObject;
43 import org.openide.loaders.DataObject;
44 import org.openide.cookies.EditorCookie;
45
46 import javax.swing.*;
47
48
49 /**
50  *
51  * @author blaha
52  */

53 public class EjbCMPRefactorTest extends NbTestCase {
54     public static final String JavaDoc EJB_PROJECT_NAME = "TestCMP";
55     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
56
57     private static Project project;
58     private static EjbJarMultiViewDataObject ddo;
59     private static Entity bean;
60
61     public EjbCMPRefactorTest(String JavaDoc testName) {
62         super(testName);
63     }
64
65     protected void setUp() throws Exception JavaDoc {
66         super.setUp();
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70     }
71
72     public static Test suite() {
73         NbTestSuite suite = new NbTestSuite(EjbCMPRefactorTest.class);
74         return suite;
75     }
76
77     /** Use for execution inside IDE */
78     public static void main(java.lang.String JavaDoc[] args) {
79         // run only selected test case
80
TestRunner.run(suite());
81     }
82
83     public void testOpenProject() throws Exception JavaDoc{
84         File JavaDoc projectDir = new File JavaDoc(EJB_PROJECT_PATH);
85         project = (Project)J2eeProjectSupport.openProject(projectDir);
86
87         EjbJarProject ejbJarProject = (EjbJarProject)project;
88         EjbJar ejb = ejbJarProject.getAPIEjbJar();
89         FileObject ddFo = ejb.getDeploymentDescriptor();
90         ddo = (EjbJarMultiViewDataObject)DataObject.find(ddFo);
91         EditorCookie editorCookie = (EditorCookie) ddo.getCookie(EditorCookie.class);
92         assertNotNull(editorCookie);
93         editorCookie.open();
94
95         // find Entity bean in project
96
EnterpriseBeans beans = DDProvider.getDefault().
97                 getDDRoot(ddFo).getEnterpriseBeans();
98         bean = (Entity)beans.findBeanByName(EnterpriseBeans.ENTITY, Ejb.EJB_NAME, "CustomerBean");
99     }
100
101     /*
102     * Rename PK CMP field
103     */

104     public void testRenameCMPFromHelper() throws Exception JavaDoc{
105         CmpField cmpField = null;
106
107         // find correct CMP field, I want PK
108
CmpField[] cmpFieldsArray = bean.getCmpField();
109         for(int i = 0; i < cmpFieldsArray.length; i++){
110             if(cmpFieldsArray[i].getFieldName().equals("id"))
111                 cmpField = cmpFieldsArray[i];
112         }
113         EntityHelper entityHelper = new EntityHelper((EjbJarMultiViewDataObject)ddo, bean);
114         CmpFieldHelper cmpHelper = new CmpFieldHelper(entityHelper, cmpField);
115         cmpHelper.setFieldName("newId"); //change CMP field name
116
Utility.waitForAWTDispatchThread();
117         assertTrue(Utility.saveDD(ddo));
118         checkFiles(getName());
119     }
120
121     /*
122     * Change PK CMP type
123     */

124     public void testChangeCMPTypeFromHelper() throws Exception JavaDoc{
125         CmpField cmpField = null;
126
127         // find correct CMP field, I want PK
128
CmpField[] cmpFieldsArray = bean.getCmpField();
129         for(int i = 0; i < cmpFieldsArray.length; i++){
130             if(cmpFieldsArray[i].getFieldName().equals("newId"))
131                 cmpField = cmpFieldsArray[i];
132         }
133
134         EntityHelper entityHelper = new EntityHelper((EjbJarMultiViewDataObject)ddo, bean);
135         CmpFieldHelper cmpHelper = new CmpFieldHelper(entityHelper, cmpField);
136         cmpHelper.setType("java.lang.String"); //change CMP's type
137
Utility.waitForAWTDispatchThread();
138         assertTrue(Utility.saveDD(ddo));
139         checkFiles(getName());
140     }
141
142     private void checkFiles(String JavaDoc methodName) throws IOException JavaDoc{
143         Utils utils = new Utils(this);
144         String JavaDoc ddNames[] = { "ejb-jar.xml", "sun-cmp-mappings.xml"};
145         utils.assertFiles(new File JavaDoc(EJB_PROJECT_PATH + File.separator + "src" + File.separator + "conf"), ddNames, methodName+"_");
146
147         String JavaDoc classNames[] = { "CustomerBean.java", "CustomerLocalBusiness.java"};
148         utils.assertFiles(new File JavaDoc(EJB_PROJECT_PATH + File.separator + "src" + File.separator + "java" + File.separator + "cmp"), classNames, methodName+"_");
149 }
150 }
151
Popular Tags