KickJava   Java API By Example, From Geeks To Geeks.

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


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

44 public class SunResourcesTest extends NbTestCase implements WizardConstants{
45     
46     private static String JavaDoc CONNECTION_POOL_NAME = "connectionPoolTest";
47     
48     /** Creates a new instance of SunResourcesTest */
49     public SunResourcesTest(String JavaDoc testName) {
50         super(testName);
51     }
52     
53     public void registerConnectionPool() {
54         try {
55             //Project project = (Project)Util.openProject(new File(Util.WEB_PROJECT_PATH));
56
ResourceConfigData cpdata = new ResourceConfigData();
57             DatabaseConnection dbconn = ConnectionManager.getDefault().getConnections()[0];
58             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
59             
60             //connection pool setting
61
cpdata.setProperties(new Vector JavaDoc());
62             cpdata.addProperty(__DerbyPortNumber, "1527");
63             cpdata.addProperty(__DerbyDatabaseName, "sample");
64             cpdata.addProperty(__ServerName, "localhost");
65             cpdata.addProperty(__User, dbconn.getUser());
66             cpdata.addProperty(__Password, dbconn.getPassword());
67             cpdata.setString(__Name, CONNECTION_POOL_NAME);
68             cpdata.setString(__ResType, "javax.sql.DataSource");
69             cpdata.setString(__IsXA, "false");
70             cpdata.setString(__DatasourceClassname, "org.apache.derby.jdbc.ClientDataSource");
71             cpdata.setString(__SteadyPoolSize, "8");
72             cpdata.setString(__MaxPoolSize, "32");
73             cpdata.setString(__MaxWaitTimeInMillis, "60000");
74             cpdata.setString(__PoolResizeQuantity, "2");
75             cpdata.setString(__IdleTimeoutInSeconds, "300");
76             
77 // cpdata.setTargetFileObject(project.getProjectDirectory());
78
File JavaDoc fpf = File.createTempFile("falseProject","");
79             fpf.delete();
80             FileObject falseProject = FileUtil.createFolder(fpf);
81             falseProject.createFolder("setup");
82             cpdata.setTargetFileObject(falseProject);
83             cpdata.setTargetFile("poolTest");
84             
85             ResourceUtils.saveConnPoolDatatoXml(cpdata);
86             
87             SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/poolTest.sun-resource"));
88             Resources res = Util.getResourcesObject(resourceObj);
89             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
90             
91             ResourceUtils.register(res.getJdbcConnectionPool(0), mejb, false);
92             resourceObj.delete();
93             falseProject.delete();
94             //Util.closeProject(Util.WEB_PROJECT_NAME);
95

96             Util.sleep(5000);
97             
98             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
99             
100             for(int i=0;i<connPools.length;i++) {
101                 if(connPools[i].equals(CONNECTION_POOL_NAME))
102                     return;
103             }
104             
105             throw new Exception JavaDoc("Connection Pool hasn't been created !");
106         } catch(Exception JavaDoc e) {
107             fail(e.getMessage());
108         }
109     }
110     
111     public void unregisterConnectionPool() {
112         try {
113             ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL);
114             ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement();
115             
116             String JavaDoc password = System.getProperty("sjsas.server.password");
117             
118             String JavaDoc[] command = new String JavaDoc[] {"delete-jdbc-connection-pool", "--user", "admin", "--password", password, CONNECTION_POOL_NAME};
119             Util.runAsadmin(command);
120             Util.closeProject(Util.WEB_PROJECT_NAME);
121             
122             Util.sleep(5000);
123             
124             String JavaDoc[] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb);
125             
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("SunResourcesTest");
137         suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
138         suite.addTest(new StartStopServerTest("startServer"));
139         suite.addTest(new SunResourcesTest("registerConnectionPool"));
140         suite.addTest(new SunResourcesTest("unregisterConnectionPool"));
141         suite.addTest(new StartStopServerTest("stopServer"));
142         suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
143         return suite;
144     }
145 }
Popular Tags