1 19 27 28 package org.netbeans.modules.j2ee.sun.test; 29 30 import java.io.BufferedReader ; 31 import java.io.File ; 32 import java.io.InputStreamReader ; 33 import java.util.Vector ; 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 52 public class JDBCResourceNewCPTest extends NbTestCase implements WizardConstants{ 53 54 private static String CONNECTION_POOL_NAME = "poolTest"; 55 private static String DATA_RESOURCE_NAME="resourceTest"; 56 57 58 59 60 public JDBCResourceNewCPTest(String testName) { 61 62 super(testName); 63 } 64 65 public void registerDataResource() { 66 try { 67 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 68 ResourceConfigData dsdata = new ResourceConfigData(); 70 ResourceConfigData cpdata = new ResourceConfigData(); 71 cpdata.setProperties(new Vector ()); 73 cpdata.addProperty(__DatabaseVendor, "derby_net"); 74 cpdata.addProperty(__User, "app"); 75 cpdata.addProperty(__Password,"app"); 76 cpdata.addProperty(__ServerName,"localhost"); 77 cpdata.addProperty(__DerbyPortNumber,"1527"); 78 cpdata.addProperty(__DatabaseName,"sample"); 79 cpdata.setString(__Name, CONNECTION_POOL_NAME); 80 cpdata.setString(__ResType, "javax.sql.DataSource"); 81 cpdata.setString(__DatasourceClassname, "org.apache.derby.jdbc.ClientDataSource"); 82 cpdata.setString(__SteadyPoolSize, "8"); 83 cpdata.setString(__MaxPoolSize, "32"); 84 cpdata.setString(__MaxWaitTimeInMillis, "60000"); 85 cpdata.setString(__PoolResizeQuantity, "2"); 86 cpdata.setString(__IdleTimeoutInSeconds, "300"); 87 dsdata.setString(__JndiName,DATA_RESOURCE_NAME); 89 dsdata.setString(__Enabled, "true"); 90 dsdata.setString(__JdbcObjectType, "user"); 91 dsdata.setString(__PoolName,CONNECTION_POOL_NAME); 92 File fpf = File.createTempFile("falseProject",""); 94 fpf.delete(); 95 FileObject falseProject = FileUtil.createFolder(fpf); 96 falseProject.createFolder("setup"); 97 dsdata.setTargetFileObject(falseProject); 98 dsdata.setTargetFile("resourceTest"); 99 cpdata.setTargetFileObject(falseProject); 100 cpdata.setTargetFile("poolTest"); 101 ResourceUtils.saveJDBCResourceDatatoXml(dsdata,cpdata); 102 SunResourceDataObject resourceObjds = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/resourceTest.sun-resource")); 103 Resources resds = Util.getResourcesObject(resourceObjds); 104 SunResourceDataObject resourceObjcp = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/poolTest.sun-resource")); 105 Resources rescp = Util.getResourcesObject(resourceObjcp); 106 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 107 ResourceUtils.register(rescp.getJdbcConnectionPool(0), mejb, false); 108 ResourceUtils.register(resds.getJdbcResource(0), mejb, false); 109 resourceObjds.delete(); 110 resourceObjcp.delete(); 111 Util.closeProject(Util.WEB_PROJECT_NAME); 112 Util.sleep(5000); 113 String [] dataResources =Util.getResourcesNames("getJdbcResource","jndi-name",mejb); 114 for(int i=0;i<dataResources.length;i++) { 115 if(dataResources[i].equals(DATA_RESOURCE_NAME)) 116 return; 117 } 118 119 throw new Exception ("Data Resource hasn't been created !"); 120 } catch(Exception e) { 121 fail(e.getMessage()); 122 } 123 } 124 public void unregisterDataResource() { 125 try { 126 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 127 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 128 String [] commandds = new String [] {"delete-jdbc-resource", "--user", "admin", DATA_RESOURCE_NAME}; 129 String [] commandcp = new String [] {"delete-jdbc-connection-pool", "--user", "admin", CONNECTION_POOL_NAME}; 130 Process p=Util.runAsadmin(commandds); 131 Util.sleep(Util.SLEEP); 132 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 133 String errorMess = error.readLine(); 134 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 135 String output=input.readLine(); 136 if(errorMess!=null) 137 throw new Exception (errorMess+"\n"+output); 138 System.out.println(output); 139 Process pc=Util.runAsadmin(commandcp); 140 Util.sleep(Util.SLEEP); 141 BufferedReader errorcp = new BufferedReader (new InputStreamReader (pc.getErrorStream())); 142 errorMess = error.readLine(); 143 BufferedReader inputcp = new BufferedReader (new InputStreamReader (pc.getInputStream())); 144 String outputcp=inputcp.readLine(); 145 if(errorMess!=null) 146 throw new Exception (errorMess+"\n"+outputcp); 147 System.out.println(outputcp); 148 Util.closeProject(Util.WEB_PROJECT_NAME); 149 Util.sleep(5000); 150 String [] dataRes = Util.getResourcesNames("getJdbcResource", "jndi-name", mejb); 151 for(int i=0;i<dataRes.length;i++) { 152 if(dataRes[i].equals(DATA_RESOURCE_NAME)) 153 throw new Exception ("Data Resource hasn't been removed !");} 154 String [] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb); 155 for(int i=0;i<connPools.length;i++) { 156 if(connPools[i].equals(CONNECTION_POOL_NAME)) 157 throw new Exception ("Connection Pool hasn't been removed !"); 158 } 159 } catch(Exception e) { 160 fail(e.getMessage()); 161 } 162 } 163 public static NbTestSuite suite() { 164 NbTestSuite suite = new NbTestSuite("JDBCResourceNewCPTest"); 165 suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance")); 166 suite.addTest(new StartStopServerTest("startServer")); 167 suite.addTest(new JDBCResourceNewCPTest("registerDataResource")); 168 suite.addTest(new JDBCResourceNewCPTest("unregisterDataResource")); 169 suite.addTest(new StartStopServerTest("stopServer")); 170 suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance")); 171 return suite; 172 } 173 } | Popular Tags |