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 JDBCResourceTest extends NbTestCase implements WizardConstants{ 53 54 private static String CONNECTION_POOL_NAME = "poolTest"; 55 private static String DATA_RESOURCE_NAME="resourceTest"; 56 57 public JDBCResourceTest(String testName) { 58 59 super(testName); 60 } 61 62 63 public void registerConnectionPool() { 64 try { 65 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 67 ResourceConfigData cpdata = new ResourceConfigData(); 68 cpdata.setProperties(new Vector ()); 70 cpdata.addProperty(__DatabaseVendor, "derby_net"); 71 cpdata.addProperty(__User, "app"); 72 cpdata.addProperty(__Password,"app"); 73 cpdata.addProperty(__ServerName,"localhost"); 74 cpdata.addProperty(__DerbyPortNumber,"1527"); 75 cpdata.addProperty(__DatabaseName,"sample"); 76 cpdata.setString(__Name, CONNECTION_POOL_NAME); 77 cpdata.setString(__ResType, "javax.sql.DataSource"); 78 cpdata.setString(__DatasourceClassname, "org.apache.derby.jdbc.ClientDataSource"); 79 cpdata.setString(__SteadyPoolSize, "8"); 80 cpdata.setString(__MaxPoolSize, "32"); 81 cpdata.setString(__MaxWaitTimeInMillis, "60000"); 82 cpdata.setString(__PoolResizeQuantity, "2"); 83 cpdata.setString(__IdleTimeoutInSeconds, "300"); 84 File fpf = File.createTempFile("falseProject",""); 86 fpf.delete(); 87 FileObject falseProject = FileUtil.createFolder(fpf); 88 falseProject.createFolder("setup"); 89 cpdata.setTargetFileObject(falseProject); 90 cpdata.setTargetFile("poolTest"); 91 ResourceUtils.saveConnPoolDatatoXml(cpdata); 92 SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/poolTest.sun-resource")); 93 Resources res = Util.getResourcesObject(resourceObj); 94 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 95 ResourceUtils.register(res.getJdbcConnectionPool(0), mejb, false); 96 resourceObj.delete(); 97 falseProject.delete(); 98 Util.sleep(5000); 100 String [] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb); 101 for(int i=0;i<connPools.length;i++) { 102 if(connPools[i].equals(CONNECTION_POOL_NAME)) 103 return; 104 } 105 throw new Exception ("Connection Pool hasn't been created !"); 106 } catch(Exception e) { 107 fail(e.getMessage()); 108 } 109 } 110 public void registerDataResource() { 111 try { 112 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 113 ResourceConfigData dsdata = new ResourceConfigData(); 115 ResourceConfigData cpdata = new ResourceConfigData(); 116 dsdata.setString(__JndiName,DATA_RESOURCE_NAME); 118 dsdata.setString(__Enabled, "true"); 119 dsdata.setString(__JdbcObjectType, "user"); 120 dsdata.setString(__PoolName,CONNECTION_POOL_NAME); 121 File fpf = File.createTempFile("falseProject",""); 123 fpf.delete(); 124 FileObject falseProject = FileUtil.createFolder(fpf); 125 falseProject.createFolder("setup"); 126 dsdata.setTargetFileObject(falseProject); 127 dsdata.setTargetFile("resourceTest"); 128 ResourceUtils.saveJDBCResourceDatatoXml(dsdata,cpdata); 129 SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/resourceTest.sun-resource")); 130 Resources res = Util.getResourcesObject(resourceObj); 131 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 132 ResourceUtils.register(res.getJdbcResource(0), mejb, false); 133 resourceObj.delete(); 134 falseProject.delete(); 135 Util.sleep(5000); 137 String [] dataResources =Util.getResourcesNames("getJdbcResource","jndi-name",mejb); 138 for(int i=0;i<dataResources.length;i++) { 139 if(dataResources[i].equals(DATA_RESOURCE_NAME)) 140 return; 141 } 142 throw new Exception ("Data Resource hasn't been created !"); 143 } catch(Exception e) { 144 fail(e.getMessage()); 145 } 146 } 147 148 public void unregisterConnectionPool() { 149 try { 150 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 151 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 152 String [] command = new String [] {"delete-jdbc-connection-pool", "--user", "admin", CONNECTION_POOL_NAME}; 153 Process p=Util.runAsadmin(command); 154 Util.sleep(Util.SLEEP); 155 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 156 String errorMess = error.readLine(); 157 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 158 String output=input.readLine(); 159 if(errorMess!=null) 160 throw new Exception (errorMess+"\n"+output); 161 System.out.println(output); 162 Util.closeProject(Util.WEB_PROJECT_NAME); 163 Util.sleep(5000); 164 String [] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb); 165 for(int i=0;i<connPools.length;i++) { 166 if(connPools[i].equals(CONNECTION_POOL_NAME)) 167 throw new Exception ("Connection Pool hasn't been removed !"); 168 } 169 } catch(Exception e) { 170 fail(e.getMessage()); 171 } 172 } 173 public void unregisterDataResource() { 174 try { 175 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 176 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 177 String [] command = new String [] {"delete-jdbc-resource", "--user", "admin", DATA_RESOURCE_NAME}; 178 Process p=Util.runAsadmin(command); 179 Util.sleep(Util.SLEEP); 180 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 181 String errorMess = error.readLine(); 182 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 183 String output=input.readLine(); 184 if(errorMess!=null) 185 throw new Exception (errorMess+"\n"+output); 186 System.out.println(output); 187 Util.closeProject(Util.WEB_PROJECT_NAME); 188 Util.sleep(5000); 189 String [] dataRes = Util.getResourcesNames("getJdbcResource", "jndi-name", mejb); 190 for(int i=0;i<dataRes.length;i++) { 191 if(dataRes[i].equals(DATA_RESOURCE_NAME)) 192 throw new Exception ("Data Resource hasn't been removed !"); 193 } 194 } catch(Exception e) { 195 fail(e.getMessage()); 196 } 197 } 198 public static NbTestSuite suite() { 199 NbTestSuite suite = new NbTestSuite("JDBCResourceTest"); 200 suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance")); 201 suite.addTest(new StartStopServerTest("startServer")); 202 suite.addTest(new JDBCResourceTest("registerConnectionPool")); 203 suite.addTest(new JDBCResourceTest("registerDataResource")); 204 suite.addTest(new JDBCResourceTest("unregisterDataResource")); 205 suite.addTest(new JDBCResourceTest("unregisterConnectionPool")); 206 suite.addTest(new StartStopServerTest("stopServer")); 207 suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance")); 208 return suite; 209 } 210 } | Popular Tags |