KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JDBCResourceTest.java
21  *
22  * Created on March 8, 2006, 10:23 AM
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 JDBCResourceTest extends NbTestCase implements WizardConstants{
53     
54     private static String JavaDoc CONNECTION_POOL_NAME = "poolTest";
55     private static String JavaDoc DATA_RESOURCE_NAME="resourceTest";
56     /** Creates a new instance of JDBCResourcesTest */
57     public JDBCResourceTest(String JavaDoc testName) {
58         
59         super(testName);
60     }
61     
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(__DatabaseVendor, "derby_net");
71             cpdata.addProperty(__User, "app");
72             cpdata.addProperty(__Password,"app");
73             cpdata.addProperty(__ServerName,"localhost");
74             cpdata.addProperty(__DerbyPortNumber,"1527");
75             cpdata.addProperty(__DatabaseName,"sample");
76             cpdata.setString(__Name, CONNECTION_POOL_NAME);
77             cpdata.setString(__ResType, "javax.sql.DataSource");
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             //datasource 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 unregisterConnectionPool() {
149         try {
150             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
151             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
152             String JavaDoc[] command = new String JavaDoc[] {"delete-jdbc-connection-pool", "--user", "admin", CONNECTION_POOL_NAME};
153             Process JavaDoc p=Util.runAsadmin(command);
154             Util.sleep(Util.SLEEP);
155             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
156             String JavaDoc errorMess = error.readLine();
157             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
158             String JavaDoc output=input.readLine();
159             if(errorMess!=null)
160                 throw new Exception JavaDoc(errorMess+"\n"+output);
161             System.out.println(output);
162             Util.closeProject(Util.WEB_PROJECT_NAME);
163             Util.sleep(5000);
164             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
165             for(int i=0;i<connPools.length;i++) {
166                 if(connPools[i].equals(CONNECTION_POOL_NAME))
167                     throw new Exception JavaDoc("Connection Pool hasn't been removed !");
168             }
169         } catch(Exception JavaDoc e) {
170             fail(e.getMessage());
171         }
172     }
173     public void unregisterDataResource() {
174         try {
175             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
176             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
177             String JavaDoc[] command = new String JavaDoc[] {"delete-jdbc-resource", "--user", "admin", DATA_RESOURCE_NAME};
178             Process JavaDoc p=Util.runAsadmin(command);
179             Util.sleep(Util.SLEEP);
180             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
181             String JavaDoc errorMess = error.readLine();
182             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
183             String JavaDoc output=input.readLine();
184             if(errorMess!=null)
185                 throw new Exception JavaDoc(errorMess+"\n"+output);
186             System.out.println(output);
187             Util.closeProject(Util.WEB_PROJECT_NAME);
188             Util.sleep(5000);
189             String JavaDoc[] dataRes = Util.getResourcesNames("getJdbcResource", "jndi-name", mejb);
190             for(int i=0;i<dataRes.length;i++) {
191                 if(dataRes[i].equals(DATA_RESOURCE_NAME))
192                     throw new Exception JavaDoc("Data Resource hasn't been removed !");
193             }
194         } catch(Exception JavaDoc e) {
195             fail(e.getMessage());
196         }
197     }
198     public static NbTestSuite suite() {
199         NbTestSuite suite = new NbTestSuite("JDBCResourceTest");
200         suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
201         suite.addTest(new StartStopServerTest("startServer"));
202         suite.addTest(new JDBCResourceTest("registerConnectionPool"));
203         suite.addTest(new JDBCResourceTest("registerDataResource"));
204         suite.addTest(new JDBCResourceTest("unregisterDataResource"));
205         suite.addTest(new JDBCResourceTest("unregisterConnectionPool"));
206         suite.addTest(new StartStopServerTest("stopServer"));
207         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
208         return suite;
209     }
210 }
Popular Tags