KickJava   Java API By Example, From Geeks To Geeks.

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


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