1 19 27 28 package org.netbeans.modules.j2ee.sun.test; 29 30 34 35 import java.io.File ; 36 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 37 import org.netbeans.api.project.Project; 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.junit.NbTestSuite; 40 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 41 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider.ConfigSupport; 42 import org.netbeans.modules.j2ee.deployment.execution.DeploymentConfigurationProvider; 43 import org.netbeans.modules.j2ee.sun.share.configbean.ASDDVersion; 44 import org.netbeans.modules.j2ee.sun.share.configbean.SunONEDeploymentConfiguration; 45 import java.io.FileInputStream ; 46 import java.io.FileOutputStream ; 47 48 52 public class SunConfigurationTest extends NbTestCase { 53 54 public SunConfigurationTest(String testName) { 55 super(testName); 56 } 57 58 public void loadSunWebConfigVersion() { 59 loadConfiguration(Util.WEB_PROJECT_NAME, Util.WEB_PROJECT_PATH, 60 "WEB-INF/sun-web.xml"); 61 } 62 63 public void loadSunEjbJarConfigVersion() { 64 loadConfiguration(Util.EJB_PROJECT_NAME, Util.EJB_PROJECT_PATH, 65 "sun-ejb-jar.xml"); 66 } 67 68 public void loadSunApplicationConfigVersion() { 69 loadConfiguration(Util.STATEFUL_PROJECT_NAME, Util.STATEFUL_PROJECT_PATH, 70 "sun-application.xml"); 71 } 72 public void loadSunAppClientConfigVersion() { 73 loadConfiguration(Util.CUSTOMER_CLIENT_PROJECT_NAME, Util.CUSTOMER_PROJECT_PATH+Util._SEP+Util.CUSTOMER_CLIENT_PROJECT_NAME, 74 "sun-application-client.xml"); 75 } 76 77 private void loadConfiguration(String testProjectName, String testProjectPath, String testConfigPath) { 78 try { 79 Project project = (Project)Util.openProject(new File (testProjectPath)); 80 81 J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 84 File primaryConfigFile = provider.getDeploymentConfigurationFile(testConfigPath); 85 if(!primaryConfigFile.exists()) { 86 fail("Primary Sun configuration file (" + primaryConfigFile.getName() + ") for project " + 87 testProjectName + " does not exist. (Path = " + primaryConfigFile.getPath() + ")"); 88 } 89 90 ConfigSupport support = provider.getConfigSupport(); 92 support.ensureConfigurationReady(); 93 Util.sleep(5000); 94 DeploymentConfigurationProvider dcProvider = (DeploymentConfigurationProvider) support; DeploymentConfiguration dcFromDCP = dcProvider.getDeploymentConfiguration(); 96 DeploymentConfiguration dcFromCache = SunONEDeploymentConfiguration.getConfiguration(primaryConfigFile); 97 if(dcFromDCP == null) { 98 fail("DeploymentConfiguration instance from DeploymentConfigurationProvider is null."); 99 } else if(dcFromDCP == null) { 100 fail("DeploymentConfiguration instance from SunONEDeploymentConfiguration cache is null."); 101 } else if(dcFromDCP != dcFromCache) { 102 fail("DeploymentConfiguration instance for project does not match cached instance in SunONEDeploymentConfiguration"); 103 } 104 Util.closeProject(testProjectName); 105 Util.sleep(5000); 106 } catch(Exception e) { 107 fail(e.getMessage()); 108 } 109 } 110 111 public void changeSunWebConfigVersion() { 112 changeConfiguration(Util.WEB_PROJECT_NAME, Util.WEB_PROJECT_PATH, 113 "WEB-INF/sun-web.xml"); 114 } 115 116 public void changeSunEjbJarConfigVersion() { 117 changeConfiguration(Util.EJB_PROJECT_NAME, Util.EJB_PROJECT_PATH, 118 "sun-ejb-jar.xml"); 119 } 120 121 public void changeSunApplicationConfigVersion() { 122 loadConfiguration(Util.STATEFUL_PROJECT_NAME, Util.STATEFUL_PROJECT_PATH, "sun-application.xml"); 123 } 124 public void changeSunAppClientConfigVersion() { 125 changeConfiguration(Util.CUSTOMER_CLIENT_PROJECT_NAME, Util.CUSTOMER_PROJECT_PATH+Util._SEP+Util.CUSTOMER_CLIENT_PROJECT_NAME,"sun-application-client.xml"); 126 } 127 128 131 private void changeConfiguration(String testProjectName, String testProjectPath, String testConfigPath) { 132 try { 133 Project project = (Project)Util.openProject(new File (testProjectPath)); 134 J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 135 File primaryConfigFile = provider.getDeploymentConfigurationFile(testConfigPath); 136 ConfigSupport support = provider.getConfigSupport(); 138 support.ensureConfigurationReady(); 139 Util.sleep(5000); 140 DeploymentConfiguration dcFromCache = SunONEDeploymentConfiguration.getConfiguration(primaryConfigFile); 141 SunONEDeploymentConfiguration sunDC = (SunONEDeploymentConfiguration) dcFromCache; 143 ASDDVersion asVersion = sunDC.getAppServerVersion(); 144 System.out.println("Current " + primaryConfigFile.getName() + " version is " + asVersion.toString()); 145 ASDDVersion oldVersion = asVersion; 146 ASDDVersion newVersion = ASDDVersion.SUN_APPSERVER_8_1; 147 if(oldVersion == ASDDVersion.SUN_APPSERVER_8_1) { 148 newVersion = ASDDVersion.SUN_APPSERVER_9_0; 149 } 150 File configBackup= primaryConfigFile.createTempFile("backup",".xml",primaryConfigFile.getParentFile()); 151 System.out.println("backup file created is "+configBackup.getName()+"path is "+configBackup.toURL().toString()); 152 FileInputStream fin = new FileInputStream (primaryConfigFile); 153 FileOutputStream fout= new FileOutputStream (configBackup); 154 int b=fin.read(); 155 while (b!=-1) { 156 fout.write(b); 157 b=fin.read(); 158 } 159 fin.close(); 160 fout.close(); 161 System.out.println("backup file created is "+configBackup.getName()); 162 System.out.println("Changing version to " + newVersion.toString()); 163 sunDC.setAppServerVersion(newVersion); 164 Util.sleep(3000); 165 ASDDVersion currentVersion = sunDC.getAppServerVersion(); 166 if(!newVersion.equals(currentVersion)) { 167 fail("Failed to change configuration version. Current version is still " + currentVersion.toString()); 168 } 169 170 System.out.println("Changing version back to " + oldVersion.toString()); 172 sunDC.setAppServerVersion(oldVersion); 173 Util.sleep(3000); 174 currentVersion = sunDC.getAppServerVersion(); 175 if(!oldVersion.equals(currentVersion)) { 176 fail("Failed to change configuration version back to original. Current version is still " + currentVersion.toString()); 177 } 178 179 184 System.out.println("Comparing " + primaryConfigFile.getName() + " after version changes with backup of original."); 186 if(!Util.compareFile(primaryConfigFile, configBackup)) { 187 System.out.println(primaryConfigFile.getName() + " content altered by version change.\nReplacing the file with backup file"); 188 FileInputStream fIn = new FileInputStream (configBackup); 189 FileOutputStream fOut= new FileOutputStream (primaryConfigFile); 190 b=fIn.read(); 191 while (b!=-1) { 192 fOut.write(b); 193 b=fIn.read(); 194 } 195 fIn.close(); 196 fOut.close(); 197 198 } else { 199 System.out.println(primaryConfigFile.getName() + " matches " + configBackup.getName()); 200 } 201 configBackup.delete(); 202 Util.closeProject(testProjectName); 203 Util.sleep(5000); 204 } catch(Exception e) { 205 e.printStackTrace(); 206 fail(e.getMessage()); 207 } 208 } 209 210 211 public static NbTestSuite suite() { 212 NbTestSuite suite = new NbTestSuite("SunConfigurationTest"); 213 return suite; 226 } 227 } 228 229 230 231 | Popular Tags |