KickJava   Java API By Example, From Geeks To Geeks.

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


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  * WebServiceTest.java
21  *
22  * Created on May 29, 2006, 10:20 AM
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 import java.io.BufferedReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.net.HttpURLConnection JavaDoc;
34 import java.net.Proxy JavaDoc;
35 import java.net.URL JavaDoc;
36 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
37 import javax.enterprise.deploy.spi.Target JavaDoc;
38 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
39 import org.netbeans.api.project.Project;
40 import org.netbeans.junit.NbTestCase;
41 import org.netbeans.junit.NbTestSuite;
42 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
43 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
44 import org.netbeans.spi.project.ActionProvider;
45
46 /**
47  *
48  * @author Amanpreet Kaur
49  */

50 public class WebServiceTest extends NbTestCase{
51     
52     private final int SLEEP = 10000;
53     
54     public WebServiceTest(String JavaDoc testName) {
55         super(testName);
56     }
57      /** calls the deploy api for the CalculatorWSApplication project*/
58     public void deployWebService() {
59         try {
60             Util.deployModule(ModuleType.WAR, Util.WEBSERVICE_PROJECT_PATH, Util.WEBSERVICE_PROJECT_NAME);
61             
62         } catch(Exception JavaDoc e) {
63             fail(e.getMessage());
64         }
65     }
66     /**disables the application by using asadmin command and then checks for it availability in running modules in appserver domain*/
67     public void disableWebService(){
68         try{
69             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
70             String JavaDoc[] command = new String JavaDoc[]{"disable", Util.WEBSERVICE_PROJECT_NAME};
71             Process JavaDoc p=Util.runAsadmin(command);
72             Util.sleep(Util.SLEEP);
73             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
74             String JavaDoc errorMess = error.readLine();
75             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
76             String JavaDoc output=input.readLine();
77             if(errorMess!=null)
78                 throw new Exception JavaDoc(errorMess+"\n"+output);
79             System.out.println(output);
80             if( Util.getModuleID(ModuleType.WAR, Util.WEBSERVICE_PROJECT_NAME,si,true)!=null)
81                 throw new Exception JavaDoc("Disable of web service failed");
82         }catch(Exception JavaDoc e){
83             fail(e.getMessage());
84         }
85         
86     }
87     /**enables the application by using asadmin command and then checks for it availability in running modules in appserver domain*/
88     public void enableWebService(){
89         try{
90             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
91             String JavaDoc[] command = new String JavaDoc[]{"enable", Util.WEBSERVICE_PROJECT_NAME};
92             Process JavaDoc p=Util.runAsadmin(command);
93             Util.sleep(Util.SLEEP);
94             BufferedReader JavaDoc error = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getErrorStream()));
95             String JavaDoc errorMess = error.readLine();
96             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
97             String JavaDoc output=input.readLine();
98             if(errorMess!=null)
99                 throw new Exception JavaDoc(errorMess+"\n"+output);
100             System.out.println(output);
101             if( Util.getModuleID(ModuleType.WAR, Util.WEBSERVICE_PROJECT_NAME,si,true)==null)
102                 throw new Exception JavaDoc("Enable of web service failed");
103             testWebService();
104         }catch(Exception JavaDoc e){
105             fail(e.getMessage());
106         }
107     }
108     /** undeploys the application and checks for its unavailability in running modules of appserver */
109     public void undeployWebService() {
110         try {
111             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
112             TargetModuleID JavaDoc moduleID = Util.getModuleID(ModuleType.WAR, Util.WEBSERVICE_PROJECT_NAME, si,false);
113             
114             if(moduleID == null)
115                 return;
116             
117             Util.undeployModule(ModuleType.WAR, Util.WEBSERVICE_PROJECT_PATH, Util.WEBSERVICE_PROJECT_NAME, moduleID);
118         } catch(Exception JavaDoc e) {
119             fail(e.getMessage());
120         }
121     }
122     /**only runs the verifier, no failures reported- need more work*/
123     public void verifyModule() {
124         try{
125             File JavaDoc f =new File JavaDoc(Util.WEBSERVICE_PROJECT_PATH + Util._SEP + "verifier_results");
126             Project project = (Project)Util.openProject(new File JavaDoc(Util.WEBSERVICE_PROJECT_PATH));
127             ActionProvider ap =(ActionProvider)project.getLookup().lookup(ActionProvider.class);
128             ap.invokeAction("verify", project.getLookup());
129             Util.sleep(10*Util.SLEEP);
130             Util.closeProject(Util.WEBSERVICE_PROJECT_NAME);
131             Util.sleep(Util.SLEEP);
132         } catch(Exception JavaDoc e){
133             fail(e.getMessage());
134         }
135         
136         
137     }
138     /** Tests the web URL and WSDL URL of web service for availability */
139     public void testWebService(){
140         try{
141             ServerInstance si = ServerRegistry.getInstance().getServerInstance(Util._URL);
142             Target JavaDoc target = si.getTargets()[0].getTarget();
143             TargetModuleID JavaDoc[] modules = si.getDeploymentManager().getRunningModules(ModuleType.WAR, new Target JavaDoc[] {target});
144             for(int i=0;i<modules.length;i++){
145                 if(modules[i].getModuleID().equals(Util.WEBSERVICE_PROJECT_NAME)) {
146                     URL JavaDoc url=new URL JavaDoc(modules[i].getWebURL());
147                     HttpURLConnection JavaDoc httpConn = (HttpURLConnection JavaDoc)url.openConnection(Proxy.NO_PROXY);
148                     if( httpConn.getResponseCode()!=200)
149                         throw new Exception JavaDoc("The webService project is not deployed with http error code"+httpConn.getResponseCode());
150                     URL JavaDoc wsdl=new URL JavaDoc(modules[i].getWebURL()+"/CalculatorWSService?wsdl");
151                     HttpURLConnection JavaDoc httpConn_wsdl = (HttpURLConnection JavaDoc)wsdl.openConnection(Proxy.NO_PROXY);
152                     if( httpConn_wsdl.getResponseCode()!=200)
153                         throw new Exception JavaDoc("The WSDL file is not published");
154                     return;
155                 }
156             }
157             throw new Exception JavaDoc("The project is not deployed");
158         }catch(Exception JavaDoc e) {
159             fail(e.getMessage());
160         }
161     }
162     
163     public static NbTestSuite suite() {
164         NbTestSuite suite = new NbTestSuite("WebServiceTest");
165         // TODO : Retouche migration
166
// suite.addTest(new AddRemoveSjsasInstanceTest("addSjsasInstance"));
167
// suite.addTest(new StartStopServerTest("startServer"));
168
// suite.addTest(new WebServiceTest("deployWebService"));
169
// suite.addTest(new WebServiceTest("testWebService"));
170
// //suite.addTest(new WebServiceTest("verifyModule"));
171
// suite.addTest(new WebServiceTest("disableWebService"));
172
// suite.addTest(new WebServiceTest("enableWebService"));
173
// suite.addTest(new StartStopServerTest("restartServer"));
174
// suite.addTest(new WebServiceTest("testWebService"));
175
// suite.addTest(new WebServiceTest("undeployWebService"));
176
// suite.addTest(new StartStopServerTest("stopServer"));
177
// suite.addTest(new AddRemoveSjsasInstanceTest("removeSjsasInstance"));
178
return suite;
179     }
180     
181 }
182
Popular Tags