KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > xtests > TestUtil


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 package org.netbeans.modules.j2ee.archive.xtests;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
26 import javax.enterprise.deploy.spi.Target JavaDoc;
27 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.ide.ProjectSupport;
31 import org.netbeans.modules.j2ee.archive.project.ArchiveProjectProperties;
32 import org.netbeans.modules.j2ee.archive.ui.JavaEePlatformUiSupport;
33 import org.netbeans.modules.j2ee.archive.wizard.*;
34 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
35 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
36 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
37 import org.netbeans.spi.project.ui.support.ProjectChooser;
38 import org.openide.WizardDescriptor;
39
40 /**
41  *
42  * @author Michal Mocnak
43  * @author vince kraemer
44  */

45 public class TestUtil {
46     
47     // TEST PROPERTIES
48
public static final String JavaDoc EJB_PROJECT_NAME = "SjsasTestEjb";
49     public static final String JavaDoc WEB_PROJECT_NAME = "SjsasTestWeb";
50     public static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
51     public static final String JavaDoc WEB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEB_PROJECT_NAME;
52     public static final int SLEEP = 10000;
53     
54     // SERVER PROPERTIES FOR TESTS
55
public static final String JavaDoc _SEP = System.getProperty("file.separator");
56     public static final String JavaDoc _DISPLAY_NAME = "Sun Java System Application Server";
57     public static final String JavaDoc _PLATFORM_LOCATION = System.getProperty("sjsas.server.path");
58     public static final String JavaDoc _INSTALL_LOCATION = _PLATFORM_LOCATION+_SEP+"domains";
59     public static final String JavaDoc _DOMAIN = "domain1";
60     public static final String JavaDoc _HOST = "localhost";
61     public static final String JavaDoc _PORT = getPort(new File JavaDoc(_INSTALL_LOCATION+_SEP+_DOMAIN+_SEP+"config"+_SEP+"domain.xml"));
62     public static final String JavaDoc _USER_NAME = "admin";
63     public static final String JavaDoc _PASSWORD = "adminadmin";
64     public static final String JavaDoc _URL = "["+_PLATFORM_LOCATION+"]deployer:Sun:AppServer::"+_HOST+":"+_PORT;
65     
66     // SERVER PROPERTIES FOR APP SERVER REGISTRATION
67
public static final String JavaDoc USER_NAME = "username";
68     public static final String JavaDoc PASSWORD = "password";
69     public static final String JavaDoc HOST = "host";
70     public static final String JavaDoc PORT = "port";
71     public static final String JavaDoc PLATFORM_LOCATION = "platform_location";
72     public static final String JavaDoc INSTALL_LOCATION = "install_location";
73     public static final String JavaDoc DOMAIN = "domain";
74     public static final String JavaDoc TYPE = "type";
75     public static final String JavaDoc PROP_DISPLAY_NAME = "ServInstWizard_displayName";
76     
77     /**
78      * It returns admin port number if the server.
79      */

80     public static String JavaDoc getPort(File JavaDoc domainXml){
81         String JavaDoc adminPort = null;
82         String JavaDoc buffer = null;
83         
84         try {
85             FileReader JavaDoc reader = new FileReader JavaDoc(domainXml);
86             BufferedReader JavaDoc br = new BufferedReader JavaDoc(reader);
87             
88             while((buffer = br.readLine()) != null) {
89                 if(buffer.indexOf("admin-listener") > -1) {
90                     int x = buffer.indexOf(34, buffer.indexOf("port"));
91                     int y = buffer.indexOf(34, ++x);
92                     adminPort = buffer.substring(x, y);
93                     break;
94                 }
95             }
96             
97             br.close();
98             reader.close();
99         } catch (Exception JavaDoc ex) {
100             ex.printStackTrace();
101         }
102         
103         return adminPort;
104     }
105     
106     public static void sleep(int i) {
107         try {
108             Thread.sleep(i);
109         } catch(Exception JavaDoc e) {
110             // Nothing to do
111
}
112     }
113     
114 // public static TargetModuleID deployModule(final ModuleType moduleType, final String modulePath, final String moduleName) throws Exception {
115
// Project project = (Project)openProject(new File(modulePath));
116
// ActionProvider ap = (ActionProvider)project.getLookup().lookup(ActionProvider.class);
117
// J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
118
// final ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL);
119
//
120
// Runnable startCondition = new Runnable() {
121
// public void run() {
122
// while(!si.isConnected()) {
123
// try {
124
// Thread.sleep(5000);
125
// } catch(Exception e) {}
126
// }
127
// }
128
// };
129
//
130
// Runnable deployCondition = new Runnable() {
131
// public void run() {
132
// while(getModuleID(moduleType, moduleName, si) == null) {
133
// try {
134
// Thread.sleep(5000);
135
// } catch(Exception e) {}
136
// }
137
// }
138
// };
139
//
140
// Task t = RequestProcessor.getDefault().create(startCondition);
141
// ap.invokeAction(EjbProjectConstants.COMMAND_REDEPLOY, project.getLookup());
142
// t.run();
143
// if(!t.waitFinished(300000))
144
// throw new Exception("Server start timeout");
145
//
146
// sleep(SLEEP);
147
//
148
// t = RequestProcessor.getDefault().create(deployCondition);
149
// t.run();
150
// if(!t.waitFinished(300000))
151
// throw new Exception("WEB Application deploy timeout");
152
//
153
// sleep(SLEEP);
154
//
155
// closeProject(moduleName);
156
//
157
// sleep(SLEEP);
158
//
159
// return null;
160
// }
161

162 // public static void undeployModule(final ModuleType moduleType, final String modulePath, final String moduleName, final TargetModuleID moduleID) throws Exception {
163
// ServerInstance si = ServerRegistry.getInstance().getServerInstance(_URL);
164
// si.getDeploymentManager().undeploy(new TargetModuleID[] {moduleID});
165
//
166
// sleep(SLEEP);
167
//
168
// if(getModuleID(moduleType, moduleName, si) != null)
169
// throw new Exception("Undeploy failed");
170
// }
171

172 // public static TargetModuleID getModuleID(ModuleType moduleType, String moduleName, ServerInstance si) {
173
// try {
174
// Target target = si.getTargets()[0].getTarget();
175
// TargetModuleID[] modules = si.getDeploymentManager().getRunningModules(moduleType, new Target[] {target});
176
//
177
// for(int i=0;i<modules.length;i++) {
178
// if(modules[i].getModuleID().equals(moduleName))
179
// return modules[i];
180
// }
181
//
182
// return null;
183
// } catch(Exception e) {
184
// return null;
185
// }
186
//
187
// }
188

189 // public static Object openProject(File projectDir) {
190
// return ProjectSupport.openProject(projectDir);
191
// }
192
//
193
// public static void closeProject(String projectName) {
194
// ProjectSupport.closeProject(projectName);
195
// }
196

197     static void createProjectFromArchive(String JavaDoc archiveName, String JavaDoc type, int sleepTime) {
198         WizardDescriptor wizDesc = new WizardDescriptor(new WizardDescriptor.Panel[0]);
199         File JavaDoc oldVal = ProjectChooser.getProjectsFolder();
200         File JavaDoc projDest = new File JavaDoc(System.getProperty("xtest.tmpdir") + File.separator + archiveName);
201         wizDesc.putProperty(DeployableWizardIterator.PROJECT_DIR_PROP, projDest);
202         File JavaDoc archive = new File JavaDoc(System.getProperty("xtest.data")+File.separator + archiveName);
203         wizDesc.putProperty(DeployableWizardIterator.PROJECT_ARCHIVE_PROP, archive);
204                 //FileUtil.toFile(archive));
205
String JavaDoc subprojkey = archiveName;
206         wizDesc.putProperty(DeployableWizardIterator.PROJECT_NAME_PROP,
207                 subprojkey);
208         
209         wizDesc.putProperty(DeployableWizardIterator.PROJECT_TARGET_PROP,
210                 JavaEePlatformUiSupport.getServerInstanceID(TestUtil._URL));
211         wizDesc.putProperty(DeployableWizardIterator.PROJECT_TYPE_PROP,
212                 type);
213         DeployableWizardIterator dwi = new DeployableWizardIterator();
214         dwi.initialize(wizDesc);
215         try {
216             dwi.instantiate();
217         } catch (IOException JavaDoc ioe) {
218             NbTestCase.fail();// fail();
219
}
220         if (null != oldVal) {
221             ProjectChooser.setProjectsFolder(oldVal);
222         }
223         try {
224             Thread.currentThread().sleep(sleepTime);
225         } catch (InterruptedException JavaDoc ex) {
226             ex.printStackTrace();
227         }
228     }
229
230 }
Popular Tags