001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * WebServiceTest.java
043: *
044: * Created on May 29, 2006, 10:20 AM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.j2ee.sun.test;
051:
052: import java.io.BufferedReader;
053: import java.io.File;
054: import java.io.InputStreamReader;
055: import java.net.HttpURLConnection;
056: import java.net.Proxy;
057: import java.net.URL;
058: import javax.enterprise.deploy.shared.ModuleType;
059: import javax.enterprise.deploy.spi.Target;
060: import javax.enterprise.deploy.spi.TargetModuleID;
061: import org.netbeans.api.project.Project;
062: import org.netbeans.junit.NbTestCase;
063: import org.netbeans.junit.NbTestSuite;
064: import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
065: import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
066: import org.netbeans.spi.project.ActionProvider;
067:
068: /**
069: *
070: * @author Amanpreet Kaur
071: */
072: public class WebServiceTest extends NbTestCase {
073:
074: private final int SLEEP = 10000;
075:
076: public WebServiceTest(String testName) {
077: super (testName);
078: }
079:
080: /** calls the deploy api for the CalculatorWSApplication project*/
081: public void deployWebService() {
082: try {
083: Util.deployModule(ModuleType.WAR,
084: Util.WEBSERVICE_PROJECT_PATH,
085: Util.WEBSERVICE_PROJECT_NAME);
086:
087: } catch (Exception e) {
088: fail(e.getMessage());
089: }
090: }
091:
092: /**disables the application by using asadmin command and then checks for it availability in running modules in appserver domain*/
093: public void disableWebService() {
094: try {
095: ServerInstance si = ServerRegistry.getInstance()
096: .getServerInstance(Util._URL);
097: String[] command = new String[] { "disable",
098: Util.WEBSERVICE_PROJECT_NAME };
099: Process p = Util.runAsadmin(command);
100: Util.sleep(Util.SLEEP);
101: BufferedReader error = new BufferedReader(
102: new InputStreamReader(p.getErrorStream()));
103: String errorMess = error.readLine();
104: BufferedReader input = new BufferedReader(
105: new InputStreamReader(p.getInputStream()));
106: String output = input.readLine();
107: if (errorMess != null)
108: throw new Exception(errorMess + "\n" + output);
109: System.out.println(output);
110: if (Util.getModuleID(ModuleType.WAR,
111: Util.WEBSERVICE_PROJECT_NAME, si, true) != null)
112: throw new Exception("Disable of web service failed");
113: } catch (Exception e) {
114: fail(e.getMessage());
115: }
116:
117: }
118:
119: /**enables the application by using asadmin command and then checks for it availability in running modules in appserver domain*/
120: public void enableWebService() {
121: try {
122: ServerInstance si = ServerRegistry.getInstance()
123: .getServerInstance(Util._URL);
124: String[] command = new String[] { "enable",
125: Util.WEBSERVICE_PROJECT_NAME };
126: Process p = Util.runAsadmin(command);
127: Util.sleep(Util.SLEEP);
128: BufferedReader error = new BufferedReader(
129: new InputStreamReader(p.getErrorStream()));
130: String errorMess = error.readLine();
131: BufferedReader input = new BufferedReader(
132: new InputStreamReader(p.getInputStream()));
133: String output = input.readLine();
134: if (errorMess != null)
135: throw new Exception(errorMess + "\n" + output);
136: System.out.println(output);
137: if (Util.getModuleID(ModuleType.WAR,
138: Util.WEBSERVICE_PROJECT_NAME, si, true) == null)
139: throw new Exception("Enable of web service failed");
140: testWebService();
141: } catch (Exception e) {
142: fail(e.getMessage());
143: }
144: }
145:
146: /** undeploys the application and checks for its unavailability in running modules of appserver */
147: public void undeployWebService() {
148: try {
149: ServerInstance si = ServerRegistry.getInstance()
150: .getServerInstance(Util._URL);
151: TargetModuleID moduleID = Util.getModuleID(ModuleType.WAR,
152: Util.WEBSERVICE_PROJECT_NAME, si, false);
153:
154: if (moduleID == null)
155: return;
156:
157: Util.undeployModule(ModuleType.WAR,
158: Util.WEBSERVICE_PROJECT_PATH,
159: Util.WEBSERVICE_PROJECT_NAME, moduleID);
160: } catch (Exception e) {
161: fail(e.getMessage());
162: }
163: }
164:
165: /**only runs the verifier, no failures reported- need more work*/
166: public void verifyModule() {
167: try {
168: File f = new File(Util.WEBSERVICE_PROJECT_PATH + Util._SEP
169: + "verifier_results");
170: Project project = (Project) Util.openProject(new File(
171: Util.WEBSERVICE_PROJECT_PATH));
172: ActionProvider ap = (ActionProvider) project.getLookup()
173: .lookup(ActionProvider.class);
174: ap.invokeAction("verify", project.getLookup());
175: Util.sleep(10 * Util.SLEEP);
176: Util.closeProject(Util.WEBSERVICE_PROJECT_NAME);
177: Util.sleep(Util.SLEEP);
178: } catch (Exception e) {
179: fail(e.getMessage());
180: }
181:
182: }
183:
184: /** Tests the web URL and WSDL URL of web service for availability */
185: public void testWebService() {
186: try {
187: ServerInstance si = ServerRegistry.getInstance()
188: .getServerInstance(Util._URL);
189: Target target = si.getTargets()[0].getTarget();
190: TargetModuleID[] modules = si.getDeploymentManager()
191: .getRunningModules(ModuleType.WAR,
192: new Target[] { target });
193: for (int i = 0; i < modules.length; i++) {
194: if (modules[i].getModuleID().equals(
195: Util.WEBSERVICE_PROJECT_NAME)) {
196: URL url = new URL(modules[i].getWebURL());
197: HttpURLConnection httpConn = (HttpURLConnection) url
198: .openConnection(Proxy.NO_PROXY);
199: if (httpConn.getResponseCode() != 200)
200: throw new Exception(
201: "The webService project is not deployed with http error code"
202: + httpConn.getResponseCode());
203: URL wsdl = new URL(modules[i].getWebURL()
204: + "/CalculatorWSService?wsdl");
205: HttpURLConnection httpConn_wsdl = (HttpURLConnection) wsdl
206: .openConnection(Proxy.NO_PROXY);
207: if (httpConn_wsdl.getResponseCode() != 200)
208: throw new Exception(
209: "The WSDL file is not published");
210: return;
211: }
212: }
213: throw new Exception("The project is not deployed");
214: } catch (Exception e) {
215: fail(e.getMessage());
216: }
217: }
218:
219: public static NbTestSuite suite() {
220: NbTestSuite suite = new NbTestSuite("WebServiceTest");
221: // TODO : Retouche migration
222: // suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
223: // suite.addTest(new StartStopServerTest("startServer"));
224: // suite.addTest(new WebServiceTest("deployWebService"));
225: // suite.addTest(new WebServiceTest("testWebService"));
226: // //suite.addTest(new WebServiceTest("verifyModule"));
227: // suite.addTest(new WebServiceTest("disableWebService"));
228: // suite.addTest(new WebServiceTest("enableWebService"));
229: // suite.addTest(new StartStopServerTest("restartServer"));
230: // suite.addTest(new WebServiceTest("testWebService"));
231: // suite.addTest(new WebServiceTest("undeployWebService"));
232: // suite.addTest(new StartStopServerTest("stopServer"));
233: // suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
234: return suite;
235: }
236:
237: }
|