KickJava   Java API By Example, From Geeks To Geeks.

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


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.modules.j2ee.sun.test;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.netbeans.api.db.explorer.ConnectionManager;
27 import org.netbeans.api.db.explorer.DatabaseConnection;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
32 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
33 import org.netbeans.modules.j2ee.sun.api.ServerInterface;
34 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
35 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources;
36 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ResourceUtils;
37 import org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader.SunResourceDataObject;
38 import org.netbeans.modules.j2ee.sun.ide.sunresources.wizards.ResourceConfigData;
39 import org.netbeans.modules.j2ee.sun.sunresources.beans.WizardConstants;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42
43 /**
44  *
45  * @author Amanpreet Kaur
46  */

47 public class JDBCDefaultConnectionPoolTest extends NbTestCase implements WizardConstants{
48     
49     private static String JavaDoc CONNECTION_POOL_NAME = "poolTest";
50     
51     /** Creates a new instance of JDBCDefaultConnectionPoolResourcesTest */
52     public JDBCDefaultConnectionPoolTest(String JavaDoc testName) {
53         super(testName);
54     }
55     
56     public void registerConnectionPool() {
57         try {
58             // todo : retouche migration
59
//Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
60
ResourceConfigData cpdata = new ResourceConfigData();
61             DatabaseConnection dbconn = ConnectionManager.getDefault().getConnections()[0];
62             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
63             //connection pool setting
64
cpdata.setProperties(new Vector JavaDoc());
65             cpdata.addProperty(__User, dbconn.getUser());
66             cpdata.addProperty(__Password,dbconn.getPassword());
67             cpdata.addProperty(__Url,dbconn.getDatabaseURL());
68             cpdata.addProperty(__DatabaseName,"sample");
69             cpdata.addProperty(__ServerName,"localhost");
70             cpdata.setString(__Name, CONNECTION_POOL_NAME);
71             cpdata.setString(__ResType, "javax.sql.DataSource");
72             cpdata.setString(__IsXA, "false");
73             cpdata.setString(__DatasourceClassname, "org.apache.derby.jdbc.ClientDataSource");
74             cpdata.setString(__SteadyPoolSize, "8");
75             cpdata.setString(__MaxPoolSize, "32");
76             cpdata.setString(__MaxWaitTimeInMillis, "60000");
77             cpdata.setString(__PoolResizeQuantity, "2");
78             cpdata.setString(__IdleTimeoutInSeconds, "300");
79             // todo : retouche migration
80
//cpdata.setTargetFileObject(project.getProjectDirectory());
81
File JavaDoc fpf = File.createTempFile("falseProject","");
82             fpf.delete();
83             FileObject falseProject = FileUtil.createFolder(fpf);
84             falseProject.createFolder("setup");
85             cpdata.setTargetFileObject(falseProject);
86             cpdata.setTargetFile("poolTest");
87             ResourceUtils.saveConnPoolDatatoXml(cpdata);
88             SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/poolTest.sun-resource"));
89             Resources res = Util.getResourcesObject(resourceObj);
90             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
91             ResourceUtils.register(res.getJdbcConnectionPool(0), mejb, false);
92             resourceObj.delete();
93             falseProject.delete();
94             //Util.closeProject(Util.WEB_PROJECT_NAME);
95
Util.sleep(5000);
96             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
97             for(int i=0;i<connPools.length;i++) {
98                 if(connPools[i].equals(CONNECTION_POOL_NAME))
99                     return;
100             }
101             throw new Exception JavaDoc("Connection Pool hasn't been created !");
102         } catch(Exception JavaDoc e) {
103             fail(e.getMessage());
104         }
105     }
106     public void unregisterConnectionPool() {
107         try {
108             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
109             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
110             String JavaDoc[] command = new String JavaDoc[] {"delete-jdbc-connection-pool",
111                 "--user",
112                 "admin",
113                 CONNECTION_POOL_NAME};
114             Process JavaDoc p=Util.runAsadmin(command);
115             Util.sleep(Util.SLEEP);
116             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
117             String JavaDoc mess = error.readLine();
118             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
119             String JavaDoc output=input.readLine();
120             if(mess!=null)
121                 fail(mess+" \n "+output);
122             System.out.println(output);
123             Util.closeProject(Util.WEB_PROJECT_NAME);
124             Util.sleep(5000);
125             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
126             for(int i=0;i<connPools.length;i++) {
127                 if(connPools[i].equals(CONNECTION_POOL_NAME))
128                     throw new Exception JavaDoc("Connection Pool hasn't been removed !");
129             }
130         } catch(Exception JavaDoc e) {
131             fail(e.getMessage());
132         }
133     }
134     
135     public static NbTestSuite suite() {
136         NbTestSuite suite = new NbTestSuite("JDBCDefaultConnectionPoolResourcesTest");
137         suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
138         suite.addTest(new StartStopServerTest("startServer"));
139         suite.addTest(new JDBCDefaultConnectionPoolTest("registerConnectionPool"));
140         suite.addTest(new JDBCDefaultConnectionPoolTest("unregisterConnectionPool"));
141         suite.addTest(new StartStopServerTest("stopServer"));
142         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
143         return suite;
144     }
145 }
146
Popular Tags