1 19 20 package org.netbeans.modules.j2ee.sun.test; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.InputStreamReader ; 25 import java.util.Vector ; 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 47 public class JDBCDefaultConnectionPoolTest extends NbTestCase implements WizardConstants{ 48 49 private static String CONNECTION_POOL_NAME = "poolTest"; 50 51 52 public JDBCDefaultConnectionPoolTest(String testName) { 53 super(testName); 54 } 55 56 public void registerConnectionPool() { 57 try { 58 ResourceConfigData cpdata = new ResourceConfigData(); 61 DatabaseConnection dbconn = ConnectionManager.getDefault().getConnections()[0]; 62 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 63 cpdata.setProperties(new Vector ()); 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 File 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.sleep(5000); 96 String [] 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 ("Connection Pool hasn't been created !"); 102 } catch(Exception 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 [] command = new String [] {"delete-jdbc-connection-pool", 111 "--user", 112 "admin", 113 CONNECTION_POOL_NAME}; 114 Process p=Util.runAsadmin(command); 115 Util.sleep(Util.SLEEP); 116 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 117 String mess = error.readLine(); 118 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 119 String 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 [] 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 ("Connection Pool hasn't been removed !"); 129 } 130 } catch(Exception 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 |