KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > test > PersistenceResourceTest


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  * PMResourceTest.java
21  *
22  * Created on March 14, 2006, 2:01 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.j2ee.sun.test;
29
30 import java.io.BufferedReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.util.Vector JavaDoc;
34 import org.netbeans.junit.NbTestCase;
35 import org.netbeans.junit.NbTestSuite;
36 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
37 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
38 import org.netbeans.modules.j2ee.sun.api.ServerInterface;
39 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
40 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
41 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
42 import org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader.SunResourceDataObject;
43 import org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.ResourceConfigData;
44 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47
48 /**
49  *
50  * @author Amanpreet Kaur
51  */

52 public class PersistenceResourceTest extends NbTestCase implements WizardConstants{
53     
54     private static String JavaDoc CONNECTION_POOL_NAME = "poolTest";
55     private static String JavaDoc DATA_RESOURCE_NAME="resourceTest";
56     private static String JavaDoc PERSISTENCE_RESOURCE_NAME="persistenceTest";
57     
58     
59     /** Creates a new instance of PersistenceResourcesTest */
60     public PersistenceResourceTest(String JavaDoc testName) {
61         super(testName);
62     }
63     public void registerConnectionPool() {
64         try {
65             //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
66
ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
67             ResourceConfigData cpdata = new ResourceConfigData();
68             //connection pool setting
69
cpdata.setProperties(new Vector JavaDoc());
70             cpdata.addProperty(__DerbyPortNumber, "1527");
71             cpdata.addProperty(__DerbyDatabaseName, "sample");
72             cpdata.addProperty(__ServerName, "localhost");
73             cpdata.addProperty(__User, "app");
74             cpdata.addProperty(__Password, "APP");
75             cpdata.setString(__Name, CONNECTION_POOL_NAME);
76             cpdata.setString(__ResType, "javax.sql.DataSource");
77             cpdata.setString(__IsXA, "false");
78             cpdata.setString(__DatasourceClassname, "org.apache.derby.jdbc.ClientDataSource");
79             cpdata.setString(__SteadyPoolSize, "8");
80             cpdata.setString(__MaxPoolSize, "32");
81             cpdata.setString(__MaxWaitTimeInMillis, "60000");
82             cpdata.setString(__PoolResizeQuantity, "2");
83             cpdata.setString(__IdleTimeoutInSeconds, "300");
84             //cpdata.setTargetFileObject(project.getProjectDirectory());
85
File JavaDoc fpf = File.createTempFile("falseProject","");
86             fpf.delete();
87             FileObject falseProject = FileUtil.createFolder(fpf);
88             falseProject.createFolder("setup");
89             cpdata.setTargetFileObject(falseProject);
90             cpdata.setTargetFile("poolTest");
91             ResourceUtils.saveConnPoolDatatoXml(cpdata);
92             SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/poolTest.sun-resource"));
93             Resources res = Util.getResourcesObject(resourceObj);
94             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
95             ResourceUtils.register(res.getJdbcConnectionPool(0), mejb, false);
96             resourceObj.delete();
97             falseProject.delete();
98             //Util.closeProject(Util.WEB_PROJECT_NAME);
99
Util.sleep(5000);
100             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
101             for(int i=0;i<connPools.length;i++) {
102                 if(connPools[i].equals(CONNECTION_POOL_NAME))
103                     return;
104             }
105             throw new Exception JavaDoc("Connection Pool hasn't been created !");
106         } catch(Exception JavaDoc e) {
107             fail(e.getMessage());
108         }
109     }
110     public void registerDataResource() {
111         try {
112             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
113             //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
114
ResourceConfigData dsdata = new ResourceConfigData();
115             ResourceConfigData cpdata = new ResourceConfigData();
116             //dataresource settings
117
dsdata.setString(__JndiName,DATA_RESOURCE_NAME);
118             dsdata.setString(__Enabled, "true");
119             dsdata.setString(__JdbcObjectType, "user");
120             dsdata.setString(__PoolName,CONNECTION_POOL_NAME);
121             //dsdata.setTargetFileObject(project.getProjectDirectory());
122
File JavaDoc fpf = File.createTempFile("falseProject","");
123             fpf.delete();
124             FileObject falseProject = FileUtil.createFolder(fpf);
125             falseProject.createFolder("setup");
126             dsdata.setTargetFileObject(falseProject);
127             dsdata.setTargetFile("resourceTest");
128             ResourceUtils.saveJDBCResourceDatatoXml(dsdata,cpdata);
129             SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/resourceTest.sun-resource"));
130             Resources res = Util.getResourcesObject(resourceObj);
131             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
132             ResourceUtils.register(res.getJdbcResource(0), mejb, false);
133             resourceObj.delete();
134             falseProject.delete();
135             //Util.closeProject(Util.WEB_PROJECT_NAME);
136
Util.sleep(5000);
137             String JavaDoc[] dataResources =Util.getResourcesNames("getJdbcResource","jndi-name",mejb);
138             for(int i=0;i<dataResources.length;i++) {
139                 if(dataResources[i].equals(DATA_RESOURCE_NAME))
140                     return;
141             }
142             throw new Exception JavaDoc("Data Resource hasn't been created !");
143         } catch(Exception JavaDoc e) {
144             fail(e.getMessage());
145         }
146     }
147     
148     public void registerPersistenceResource() {
149         try {
150             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
151             //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
152
ResourceConfigData dsdata = new ResourceConfigData();
153             ResourceConfigData pmdata = new ResourceConfigData();
154             ResourceConfigData cpdata=new ResourceConfigData();
155             //persistence resource settings
156
pmdata.setString(__JndiName,PERSISTENCE_RESOURCE_NAME);
157             pmdata.setString(__Enabled, "true");
158             pmdata.setString(__FactoryClass, "com.sun.jdo.spi.persistence.support.sqlstore.impl.PersistenceManagerFactoryImpl");
159             pmdata.setString(__JdbcResourceJndiName,DATA_RESOURCE_NAME);
160             //pmdata.setTargetFileObject(project.getProjectDirectory());
161
File JavaDoc fpf = File.createTempFile("falseProject","");
162             fpf.delete();
163             FileObject falseProject = FileUtil.createFolder(fpf);
164             falseProject.createFolder("setup");
165             pmdata.setTargetFileObject(falseProject);
166             pmdata.setTargetFile("persistenceTest");
167             ResourceUtils.savePMFResourceDatatoXml(pmdata,dsdata,cpdata);
168             SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/persistenceTest.sun-resource"));
169             Resources res = Util.getResourcesObject(resourceObj);
170             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
171             ResourceUtils.register(res.getPersistenceManagerFactoryResource(0), mejb, false);
172             resourceObj.delete();
173             falseProject.delete();
174             //Util.closeProject(Util.WEB_PROJECT_NAME);
175
Util.sleep(5000);
176             String JavaDoc[] perResources =Util.getResourcesNames("getPersistenceManagerFactoryResource","jndi-name",mejb);
177             for(int i=0;i<perResources.length;i++) {
178                 if(perResources[i].equals(PERSISTENCE_RESOURCE_NAME))
179                     return;
180             }
181             
182             throw new Exception JavaDoc("Persistence Manager Factory Resource hasn't been created !");
183         } catch(Exception JavaDoc e) {
184             fail(e.getMessage());
185         }
186     }
187     public void unregisterConnectionPool() {
188         try {
189             
190             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
191             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
192             String JavaDoc[] command = new String JavaDoc[] {"delete-jdbc-connection-pool", "--user", "admin", CONNECTION_POOL_NAME};
193             Process JavaDoc p=Util.runAsadmin(command);
194             Util.sleep(Util.SLEEP);
195             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
196             String JavaDoc errorMess = error.readLine();
197             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
198             String JavaDoc output=input.readLine();
199             if(errorMess!=null)
200                 throw new Exception JavaDoc(errorMess+"\n"+output);
201             System.out.println(output);
202             Util.closeProject(Util.WEB_PROJECT_NAME);
203             Util.sleep(5000);
204             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
205             for(int i=0;i<connPools.length;i++) {
206                 if(connPools[i].equals(CONNECTION_POOL_NAME))
207                     throw new Exception JavaDoc("Connection Pool hasn't been removed !");
208             }
209         } catch(Exception JavaDoc e) {
210             fail(e.getMessage());
211         }
212     }
213     public void unregisterDataResource() {
214         try {
215             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
216             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
217             String JavaDoc[] command = new String JavaDoc[] {"delete-jdbc-resource", "--user", "admin", DATA_RESOURCE_NAME};
218             Process JavaDoc p=Util.runAsadmin(command);
219             Util.sleep(Util.SLEEP);
220             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
221             String JavaDoc errorMess = error.readLine();
222             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
223             String JavaDoc output=input.readLine();
224             if(errorMess!=null)
225                 throw new Exception JavaDoc(errorMess+"\n"+output);
226             System.out.println(output);
227             Util.closeProject(Util.WEB_PROJECT_NAME);
228             Util.sleep(5000);
229             String JavaDoc[] dataRes = Util.getResourcesNames("getJdbcResource", "jndi-name", mejb);
230             for(int i=0;i<dataRes.length;i++) {
231                 if(dataRes[i].equals(DATA_RESOURCE_NAME))
232                     throw new Exception JavaDoc("Data Resource hasn't been removed !");
233             }
234         } catch(Exception JavaDoc e) {
235             fail(e.getMessage());
236         }
237     }
238     public void unregisterPersistenceResource() {
239         try {
240             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
241             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
242             String JavaDoc[] command = new String JavaDoc[] {"delete-persistence-resource", "--user", "admin", PERSISTENCE_RESOURCE_NAME};
243             Process JavaDoc p=Util.runAsadmin(command);
244             Util.sleep(Util.SLEEP);
245             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
246             String JavaDoc errorMess = error.readLine();
247             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
248             String JavaDoc output=input.readLine();
249             if(errorMess!=null)
250                 throw new Exception JavaDoc(errorMess+"\n"+output);
251             System.out.println(output);
252             Util.closeProject(Util.WEB_PROJECT_NAME);
253             Util.sleep(5000);
254             String JavaDoc[] perRes = Util.getResourcesNames("getPersistenceManagerFactoryResource", "jndi-name", mejb);
255             for(int i=0;i<perRes.length;i++) {
256                 if(perRes[i].equals(PERSISTENCE_RESOURCE_NAME))
257                     throw new Exception JavaDoc("Persistence Resource hasn't been removed !");
258             }
259         } catch(Exception JavaDoc e) {
260             fail(e.getMessage());
261         }
262     }
263     public static NbTestSuite suite() {
264         NbTestSuite suite = new NbTestSuite("PersistenceResourceTest");
265         suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
266         suite.addTest(new StartStopServerTest("startServer"));
267         suite.addTest(new PersistenceResourceTest("registerConnectionPool"));
268         suite.addTest(new PersistenceResourceTest("registerDataResource"));
269         suite.addTest(new PersistenceResourceTest("registerPersistenceResource"));
270         suite.addTest(new PersistenceResourceTest("unregisterPersistenceResource"));
271         suite.addTest(new PersistenceResourceTest("unregisterDataResource"));
272         suite.addTest(new PersistenceResourceTest("unregisterConnectionPool"));
273         suite.addTest(new StartStopServerTest("stopServer"));
274         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
275         return suite;
276     }
277 }
Popular Tags