KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > test > SunConfigurationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * SunConfigurationTest.java
21  *
22  * Created on April 19, 2006, 4:36 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.j2ee.sun.test;
29
30 /**
31  *
32  * @author ak199487
33  */

34
35 import java.io.File JavaDoc;
36 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
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 JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47
48 /**
49  *
50  * @author Peter Williams
51  */

52 public class SunConfigurationTest extends NbTestCase {
53     
54     public SunConfigurationTest(String JavaDoc 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 JavaDoc testProjectName, String JavaDoc testProjectPath, String JavaDoc testConfigPath) {
78         try {
79             Project project = (Project)Util.openProject(new File JavaDoc(testProjectPath));
80             
81             // Test: Verify existence of primary configuration file. (Note this test should not be applied to
82
// trivial JavaEE 5.0 modules which don't have server specific DD files.)
83
J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
84             File JavaDoc 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             // Test: Get deployment configuration object instance of type SunONEDeploymentConfiguration
91
ConfigSupport support = provider.getConfigSupport();
92             support.ensureConfigurationReady();
93             Util.sleep(5000);
94             DeploymentConfigurationProvider dcProvider = (DeploymentConfigurationProvider) support; // Implementation dependency on ConfigSupportImpl in j2eeserver
95
DeploymentConfiguration JavaDoc dcFromDCP = dcProvider.getDeploymentConfiguration();
96             DeploymentConfiguration JavaDoc 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 JavaDoc 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     /** Generic method that performs version changes on the specified configuration file
129      * from the specified test project.
130      */

131     private void changeConfiguration(String JavaDoc testProjectName, String JavaDoc testProjectPath, String JavaDoc testConfigPath) {
132         try {
133             Project project = (Project)Util.openProject(new File JavaDoc(testProjectPath));
134             J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
135             File JavaDoc primaryConfigFile = provider.getDeploymentConfigurationFile(testConfigPath);
136             // Test: Get deployment configuration object instance of type SunONEDeploymentConfiguration
137
ConfigSupport support = provider.getConfigSupport();
138             support.ensureConfigurationReady();
139             Util.sleep(5000);
140             DeploymentConfiguration JavaDoc dcFromCache = SunONEDeploymentConfiguration.getConfiguration(primaryConfigFile);
141             // Test: change version to 8.1
142
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 JavaDoc configBackup= primaryConfigFile.createTempFile("backup",".xml",primaryConfigFile.getParentFile());
151             System.out.println("backup file created is "+configBackup.getName()+"path is "+configBackup.toURL().toString());
152             FileInputStream JavaDoc fin = new FileInputStream JavaDoc(primaryConfigFile);
153             FileOutputStream JavaDoc fout= new FileOutputStream JavaDoc(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             // Test: change version to 9.0
171
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             // Test: diff sun config file against original copy to make sure everything saved properly
180
// -- need to be careful that fields that were dropped due to insufficient version support
181
// remain dropped so either they can't be represented in the first place, or the backup copy
182
// needs to be trimmed accordingly.
183

184             //System.out.println("backup configpath is "+backupConfigPath);
185
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 JavaDoc fIn = new FileInputStream JavaDoc(configBackup);
189                 FileOutputStream JavaDoc fOut= new FileOutputStream JavaDoc(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 JavaDoc e) {
205             e.printStackTrace();
206             fail(e.getMessage());
207         }
208     }
209     
210     
211     public static NbTestSuite suite() {
212         NbTestSuite suite = new NbTestSuite("SunConfigurationTest");
213 // suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
214
// suite.addTest(new StartStopServerTest("startServer"));
215
// suite.addTest(new SunConfigurationTest("loadSunWebConfigVersion"));
216
// suite.addTest(new SunConfigurationTest("loadSunEjbJarConfigVersion"));
217
// suite.addTest(new SunConfigurationTest("loadSunApplicationConfigVersion"));
218
// suite.addTest(new SunConfigurationTest("loadSunAppClientConfigVersion"));
219
// suite.addTest(new SunConfigurationTest("changeSunWebConfigVersion"));
220
// suite.addTest(new SunConfigurationTest("changeSunEjbJarConfigVersion"));
221
// suite.addTest(new SunConfigurationTest("changeSunApplicationConfigVersion"));
222
// suite.addTest(new SunConfigurationTest("changeSunAppClientConfigVersion"));
223
// suite.addTest(new StartStopServerTest("stopServer"));
224
// suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
225
return suite;
226     }
227 }
228
229
230
231
Popular Tags