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 PersistenceResourceTest extends NbTestCase implements WizardConstants{ 53 54 private static String CONNECTION_POOL_NAME = "poolTest"; 55 private static String DATA_RESOURCE_NAME="resourceTest"; 56 private static String PERSISTENCE_RESOURCE_NAME="persistenceTest"; 57 58 59 60 public PersistenceResourceTest(String testName) { 61 super(testName); 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(__DerbyPortNumber, "1527"); 71 cpdata.addProperty(__DerbyDatabaseName, "sample"); 72 cpdata.addProperty(__ServerName, "localhost"); 73 cpdata.addProperty(__User, "app"); 74 cpdata.addProperty(__Password, "APP"); 75 cpdata.setString(__Name, CONNECTION_POOL_NAME); 76 cpdata.setString(__ResType, "javax.sql.DataSource"); 77 cpdata.setString(__IsXA, "false"); 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 registerPersistenceResource() { 149 try { 150 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 151 ResourceConfigData dsdata = new ResourceConfigData(); 153 ResourceConfigData pmdata = new ResourceConfigData(); 154 ResourceConfigData cpdata=new ResourceConfigData(); 155 pmdata.setString(__JndiName,PERSISTENCE_RESOURCE_NAME); 157 pmdata.setString(__Enabled, "true"); 158 pmdata.setString(__FactoryClass, "com.sun.jdo.spi.persistence.support.sqlstore.impl.PersistenceManagerFactoryImpl"); 159 pmdata.setString(__JdbcResourceJndiName,DATA_RESOURCE_NAME); 160 File fpf = File.createTempFile("falseProject",""); 162 fpf.delete(); 163 FileObject falseProject = FileUtil.createFolder(fpf); 164 falseProject.createFolder("setup"); 165 pmdata.setTargetFileObject(falseProject); 166 pmdata.setTargetFile("persistenceTest"); 167 ResourceUtils.savePMFResourceDatatoXml(pmdata,dsdata,cpdata); 168 SunResourceDataObject resourceObj = (SunResourceDataObject)SunResourceDataObject.find(falseProject.getFileObject("setup/persistenceTest.sun-resource")); 169 Resources res = Util.getResourcesObject(resourceObj); 170 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 171 ResourceUtils.register(res.getPersistenceManagerFactoryResource(0), mejb, false); 172 resourceObj.delete(); 173 falseProject.delete(); 174 Util.sleep(5000); 176 String [] perResources =Util.getResourcesNames("getPersistenceManagerFactoryResource","jndi-name",mejb); 177 for(int i=0;i<perResources.length;i++) { 178 if(perResources[i].equals(PERSISTENCE_RESOURCE_NAME)) 179 return; 180 } 181 182 throw new Exception ("Persistence Manager Factory Resource hasn't been created !"); 183 } catch(Exception e) { 184 fail(e.getMessage()); 185 } 186 } 187 public void unregisterConnectionPool() { 188 try { 189 190 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 191 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 192 String [] command = new String [] {"delete-jdbc-connection-pool", "--user", "admin", CONNECTION_POOL_NAME}; 193 Process p=Util.runAsadmin(command); 194 Util.sleep(Util.SLEEP); 195 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 196 String errorMess = error.readLine(); 197 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 198 String output=input.readLine(); 199 if(errorMess!=null) 200 throw new Exception (errorMess+"\n"+output); 201 System.out.println(output); 202 Util.closeProject(Util.WEB_PROJECT_NAME); 203 Util.sleep(5000); 204 String [] connPools = Util.getResourcesNames("getJdbcConnectionPool", "name", mejb); 205 for(int i=0;i<connPools.length;i++) { 206 if(connPools[i].equals(CONNECTION_POOL_NAME)) 207 throw new Exception ("Connection Pool hasn't been removed !"); 208 } 209 } catch(Exception e) { 210 fail(e.getMessage()); 211 } 212 } 213 public void unregisterDataResource() { 214 try { 215 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 216 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 217 String [] command = new String [] {"delete-jdbc-resource", "--user", "admin", DATA_RESOURCE_NAME}; 218 Process p=Util.runAsadmin(command); 219 Util.sleep(Util.SLEEP); 220 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 221 String errorMess = error.readLine(); 222 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 223 String output=input.readLine(); 224 if(errorMess!=null) 225 throw new Exception (errorMess+"\n"+output); 226 System.out.println(output); 227 Util.closeProject(Util.WEB_PROJECT_NAME); 228 Util.sleep(5000); 229 String [] dataRes = Util.getResourcesNames("getJdbcResource", "jndi-name", mejb); 230 for(int i=0;i<dataRes.length;i++) { 231 if(dataRes[i].equals(DATA_RESOURCE_NAME)) 232 throw new Exception ("Data Resource hasn't been removed !"); 233 } 234 } catch(Exception e) { 235 fail(e.getMessage()); 236 } 237 } 238 public void unregisterPersistenceResource() { 239 try { 240 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(Util._URL); 241 ServerInterface mejb = ((SunDeploymentManagerInterface)inst.getDeploymentManager()).getManagement(); 242 String [] command = new String [] {"delete-persistence-resource", "--user", "admin", PERSISTENCE_RESOURCE_NAME}; 243 Process p=Util.runAsadmin(command); 244 Util.sleep(Util.SLEEP); 245 BufferedReader error = new BufferedReader (new InputStreamReader (p.getErrorStream())); 246 String errorMess = error.readLine(); 247 BufferedReader input = new BufferedReader (new InputStreamReader (p.getInputStream())); 248 String output=input.readLine(); 249 if(errorMess!=null) 250 throw new Exception (errorMess+"\n"+output); 251 System.out.println(output); 252 Util.closeProject(Util.WEB_PROJECT_NAME); 253 Util.sleep(5000); 254 String [] perRes = Util.getResourcesNames("getPersistenceManagerFactoryResource", "jndi-name", mejb); 255 for(int i=0;i<perRes.length;i++) { 256 if(perRes[i].equals(PERSISTENCE_RESOURCE_NAME)) 257 throw new Exception ("Persistence Resource hasn't been removed !"); 258 } 259 } catch(Exception e) { 260 fail(e.getMessage()); 261 } 262 } 263 public static NbTestSuite suite() { 264 NbTestSuite suite = new NbTestSuite("PersistenceResourceTest"); 265 suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance")); 266 suite.addTest(new StartStopServerTest("startServer")); 267 suite.addTest(new PersistenceResourceTest("registerConnectionPool")); 268 suite.addTest(new PersistenceResourceTest("registerDataResource")); 269 suite.addTest(new PersistenceResourceTest("registerPersistenceResource")); 270 suite.addTest(new PersistenceResourceTest("unregisterPersistenceResource")); 271 suite.addTest(new PersistenceResourceTest("unregisterDataResource")); 272 suite.addTest(new PersistenceResourceTest("unregisterConnectionPool")); 273 suite.addTest(new StartStopServerTest("stopServer")); 274 suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance")); 275 return suite; 276 } 277 } | Popular Tags |