KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > clients > webservices > A_WebServicesEndpoint


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_WebServicesEndpoint.java,v 1.7 2005/06/02 11:05:41 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.examples.clients.webservices;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.wsdl.Definition;
33 import javax.wsdl.Port;
34 import javax.wsdl.Service;
35 import javax.wsdl.extensions.soap.SOAPAddress;
36 import javax.wsdl.factory.WSDLFactory;
37 import javax.wsdl.xml.WSDLReader;
38 import javax.xml.namespace.QName JavaDoc;
39
40 import com.meterware.httpunit.WebForm;
41 import com.meterware.httpunit.WebLink;
42 import com.meterware.httpunit.WebResponse;
43
44 /**
45  * Define a class to test the webServices examples wsWarExample
46  * Test WebServices deployment OK and test Call on deployed WebService
47  * @author Guillaume Sauthier
48  */

49 public abstract class A_WebServicesEndpoint extends A_WebServices {
50
51     /**
52      * property name => file1 WSDLHandler is mandatory for this test !
53      */

54     private static final String JavaDoc JPROP_WSDL_LOCATION = "jonas.service.publish.file.directory";
55
56     public A_WebServicesEndpoint(String JavaDoc s, String JavaDoc urlPrefix) {
57         super(s, urlPrefix);
58     }
59
60     /**
61      * Check if Axis is Running
62      * @param idServicesLink indicate where is the link to /services in the homepage
63      * @param portName the name of the port we want to check
64      * @throws Exception if an error occurs
65      */

66     public void checkAxisServicesAccessible(int idServicesLink, String JavaDoc context, String JavaDoc portName) throws Exception JavaDoc {
67
68         WebResponse wr = wc.getResponse(url + "index.html");
69         WebLink services = wr.getLinks()[idServicesLink];
70
71         // click the link
72
WebResponse swr = services.click();
73
74         // traverse the links and find the interresting one.
75
boolean found = false;
76         for (int i = 0; i < swr.getLinks().length; i++) {
77             String JavaDoc ws_url = swr.getLinks()[i].getURLString();
78             if (ws_url.equalsIgnoreCase(url + portName + "/" + portName + "?jwsdl"))
79                 found = true;
80         }
81         if (!found)
82             fail("endpoint '" + portName + "' not found in /" + context);
83
84     }
85
86     /**
87      * Check if WSDL is generated
88      * @param idWSDLServiceLink indicate where we can find the link
89      * to WSDL of the port we want to test. (first, second, ...)
90      * @throws Exception if an error occurs
91      */

92     public void checkAxisWSDL(String JavaDoc path) throws Exception JavaDoc {
93
94         WebResponse wr = wc.getResponse(url + path);
95         assertEquals("Must be an XML mime type", "text/xml", wr.getContentType());
96
97     }
98
99     /**
100      * Check if deployed WebService is running
101      * @param formPage the filename of the webpage containing the form
102      * @param endpoint the URL where local service can be found
103      * @param name the name to set as parameter
104      * @param pageTitle title of the returned page
105      * @throws Exception if an error occurs
106      */

107     public void checkDeployedWebService(String JavaDoc formPage,
108                                         String JavaDoc endpoint,
109                                         String JavaDoc name,
110                                         String JavaDoc pageTitle) throws Exception JavaDoc {
111
112         // Get the page containing the form
113
WebResponse wr = wc.getResponse(url + formPage);
114
115         // get the form named "prepare"
116
WebForm form = wr.getFormWithName("prepare");
117         // set all parameters
118
form.setParameter("endpoint", endpoint);
119         form.setParameter("name", name);
120         // submit
121
WebResponse wr2 = form.submit();
122
123         String JavaDoc text = wr2.getText();
124
125         String JavaDoc valTitle = "<title>" + pageTitle + "</title>";
126         String JavaDoc valURL = "Working with URL : <i>" + endpoint + "</i><br/>";
127         String JavaDoc valHello = "<b>result of sayHello(name) method :</b><i>Hello " + name + "</i><br/>";
128         String JavaDoc valQuotes = "<b>result of getCotes() method :</b><i>12</i><br/>";
129         boolean testOK = false;
130
131         if ((text.indexOf(valTitle) != -1)
132             && (text.indexOf(valURL) != -1)
133             && (text.indexOf(valHello) != -1)
134             && (text.indexOf(valQuotes) != -1)) {
135             testOK = true;
136         }
137
138         if (!testOK) {
139             fail("Call fails");
140         }
141
142     }
143
144
145     /**
146      * Check if WSDL has been published.
147      * @param wsdlFilename the filename of the WSDL file
148      * @param tns targetNameSpace of the Definition
149      * @param serviceName the localpart of the Service QName
150      * @param portName the name of the WSDL' Port
151      * @param endpoint the expected URL
152      * @throws Exception if an error occurs
153      */

154     public void checkWSDLPublication(String JavaDoc wsdlFilename,
155                                      String JavaDoc tns,
156                                      String JavaDoc serviceName,
157                                      String JavaDoc portName,
158                                      String JavaDoc endpoint) throws Exception JavaDoc {
159
160         String JavaDoc jonasbase = System.getProperty("jonas.base");
161         Properties JavaDoc props = new Properties JavaDoc();
162         props.load(new FileInputStream JavaDoc(new File JavaDoc(jonasbase, "conf/file1.properties")));
163         String JavaDoc wsdlLoc = props.getProperty(JPROP_WSDL_LOCATION);
164
165         File JavaDoc wsdl = new File JavaDoc(wsdlLoc, wsdlFilename);
166
167         assertTrue("WSDL is not created", wsdl.exists());
168         assertTrue("WSDL is not a file", wsdl.isFile());
169
170         WSDLFactory f = WSDLFactory.newInstance();
171         WSDLReader r = f.newWSDLReader();
172
173         r.setFeature("javax.wsdl.verbose", true);
174         r.setFeature("javax.wsdl.importDocuments", false);
175
176         Definition def = r.readWSDL(wsdl.getPath());
177         Service s = def.getService(new QName JavaDoc(tns, serviceName));
178         Port p = s.getPort(portName);
179         SOAPAddress sa = (SOAPAddress) p.getExtensibilityElements().get(0);
180
181         assertEquals("URL has not been updated", endpoint, sa.getLocationURI());
182
183     }
184
185
186 }
187
Popular Tags