001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.wms.test;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.URISyntaxException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.Properties;
025: import java.util.StringTokenizer;
026: import java.util.logging.Level;
027:
028: import org.geotools.data.ows.CRSEnvelope;
029: import org.geotools.data.ows.GetCapabilitiesRequest;
030: import org.geotools.data.ows.Layer;
031: import org.geotools.data.ows.Specification;
032: import org.geotools.data.ows.WMSCapabilities;
033: import org.geotools.data.wms.WMS1_0_0;
034: import org.geotools.data.wms.WMSSpecification;
035: import org.geotools.data.wms.WebMapServer;
036: import org.geotools.data.wms.request.GetMapRequest;
037: import org.geotools.data.wms.xml.WMSSchema;
038: import org.geotools.test.TestData;
039: import org.geotools.wms.v1_1_1.bindings.WMSV1_1_1Configuration;
040: import org.geotools.xml.DocumentFactory;
041: import org.geotools.xml.Parser;
042: import org.geotools.xml.SchemaFactory;
043: import org.geotools.xml.handlers.DocumentHandler;
044: import org.geotools.xml.schema.Schema;
045: import org.xml.sax.SAXException;
046:
047: /**
048: *
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/test/java/org/geotools/data/wms/test/WMS1_0_0_OnlineTest.java $
050: */
051: public class WMS1_0_0_OnlineTest extends ServerTestCase {
052: protected URL server;
053: protected WMSSpecification spec;
054:
055: public WMS1_0_0_OnlineTest() throws Exception {
056: this .spec = new WMS1_0_0();
057: this .server = new URL(
058: "http://www2.demis.nl/mapserver/Request.asp?wmtver=1.0.0&request=getcapabilities");
059: }
060:
061: public void testGetVersion() {
062: assertEquals(spec.getVersion(), "1.0.0");
063: }
064:
065: public void testCreateGetCapabilitiesRequest() throws Exception {
066: GetCapabilitiesRequest request = spec
067: .createGetCapabilitiesRequest(server);
068:
069: Properties properties = new Properties();
070:
071: StringTokenizer tokenizer = new StringTokenizer(request
072: .getFinalURL().getQuery(), "&");
073:
074: while (tokenizer.hasMoreTokens()) {
075: String token = tokenizer.nextToken();
076: String[] param = token.split("=");
077: properties.setProperty(param[0].toUpperCase(), param[1]);
078: }
079:
080: checkProperties(properties);
081: try {
082: WebMapServer wms = new WebMapServer(server);
083: WMSCapabilities capabilities = wms.getCapabilities();
084:
085: assertNotNull(capabilities);
086: } catch (java.net.ConnectException ce) {
087: if (ce.getMessage().indexOf("timed out") > 0) {
088: System.err.println("Unable to test - timed out: " + ce);
089: } else {
090: throw (ce);
091: }
092: }
093: }
094:
095: public void testCreateDescribeLayerRequest() throws Exception {
096: try {
097: spec.createDescribeLayerRequest(null);
098: assertTrue(
099: "Shouldn't be able to create DescribeLayer requests for version 1.0.0",
100: false);
101: } catch (UnsupportedOperationException e) {
102:
103: }
104: }
105:
106: protected void checkProperties(Properties properties) {
107: assertEquals(properties.getProperty("REQUEST"), "capabilities");
108: assertEquals(properties.getProperty("WMTVER"), "1.0.0");
109: }
110:
111: public void testCreateParser() throws Exception {
112: WMSCapabilities capabilities = createCapabilities("1.0.0Capabilities.xml");
113: try {
114: assertEquals(capabilities.getVersion(), "1.0.0");
115: assertEquals(capabilities.getService().getName(), "GetMap");
116: assertEquals(capabilities.getService().getTitle(),
117: "World Map");
118:
119: for (int i = 0; i < capabilities.getService()
120: .getKeywordList().length; i++) {
121: assertEquals(
122: capabilities.getService().getKeywordList()[i],
123: "OpenGIS WMS Web Map Server".split(" ")[i]);
124: }
125:
126: assertEquals(capabilities.getService().getOnlineResource(),
127: new URL("http://www2.demis.nl"));
128: assertEquals(capabilities.getRequest().getGetCapabilities()
129: .getFormats().get(0), "application/vnd.ogc.wms_xml");
130: assertEquals(capabilities.getRequest().getGetFeatureInfo()
131: .getGet(), new URL(
132: "http://www2.demis.nl/wms/wms.asp?wms=WorldMap&"));
133: assertEquals(capabilities.getRequest().getGetMap()
134: .getFormats().size(), 4);
135:
136: assertEquals(capabilities.getLayerList().size(), 21);
137:
138: Layer[] layers = (Layer[]) capabilities.getLayerList()
139: .toArray(
140: new Layer[capabilities.getLayerList()
141: .size()]);
142: assertEquals(layers[0].getTitle(), "World Map");
143: assertEquals(layers[0].getParent(), null);
144: assertTrue(layers[0].getSrs().contains("EPSG:4326")); // case should not matter
145: assertTrue(layers[0].getSrs().contains("EPSG:4327"));
146: assertEquals(layers[1].getTitle(), "Bathymetry");
147: assertEquals(layers[1].getName(), "Bathymetry");
148: assertEquals(layers[20].getTitle(), "Ocean features");
149: assertEquals(layers[20].getName(), "Ocean features");
150: assertEquals(layers[0].getBoundingBoxes().size(), 1);
151:
152: CRSEnvelope bbox = (CRSEnvelope) layers[1]
153: .getBoundingBoxes().get("EPSG:4326");
154: assertNotNull(bbox);
155: } catch (Exception e) {
156: if (e.getMessage().indexOf("timed out") > 0) {
157: System.err.println("Unable to test - timed out: " + e);
158: } else {
159: throw (e);
160: }
161: }
162: }
163:
164: public void testCreateGetMapRequest() throws Exception {
165: try {
166: CustomWMS wms = new CustomWMS(server);
167: WMSCapabilities caps = wms.getCapabilities();
168: GetMapRequest request = wms.createGetMapRequest();
169: request.setFormat("image/jpeg");
170: System.out.println(request.getFinalURL().toExternalForm());
171:
172: assertTrue(request.getFinalURL().toExternalForm().indexOf(
173: "JPEG") >= 0);
174: } catch (java.net.ConnectException ce) {
175: if (ce.getMessage().indexOf("timed out") > 0) {
176: System.err.println("Unable to test - timed out: " + ce);
177: } else {
178: throw (ce);
179: }
180: }
181: }
182:
183: public void testCreateGetFeatureInfoRequest() throws Exception {
184: /* TODO FIX THIS
185: try{
186: URL featureURL = new URL("http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?VERSION=1.1.0&REQUEST=GetCapabilities");
187: WebMapServer wms = getCustomWMS(featureURL);
188: WMSCapabilities caps = wms.getCapabilities();
189: assertNotNull(caps);
190: assertNotNull(caps.getRequest().getGetFeatureInfo());
191:
192: GetMapRequest getMapRequest = wms.createGetMapRequest();
193:
194: List layers = Arrays.asList(WMSUtils.getNamedLayers(caps));
195: List simpleLayers = new ArrayList();
196: Iterator iter = layers.iterator();
197: while (iter.hasNext()) {
198: Layer layer = (Layer) iter.next();
199: SimpleLayer sLayer = new SimpleLayer(layer.getName(), "");
200: simpleLayers.add(sLayer);
201: List styles = layer.getStyles();
202: if (styles.size() == 0) {
203: sLayer.setStyle("");
204: continue;
205: }
206: Random random = new Random();
207: int randomInt = random.nextInt(styles.size());
208: sLayer.setStyle((String) styles.get(randomInt));
209: }
210: getMapRequest.setLayers(simpleLayers);
211:
212: getMapRequest.setSRS("EPSG:4326");
213: getMapRequest.setDimensions("400", "400");
214: getMapRequest.setFormat("image/png");
215:
216: getMapRequest.setBBox("-114.01268,59.4596930,-113.26043,60.0835794");
217: URL url2 = getMapRequest.getFinalURL();
218:
219: GetFeatureInfoRequest request = wms.createGetFeatureInfoRequest(getMapRequest);
220: request.setQueryLayers(WMSUtils.getQueryableLayers(caps));
221: request.setQueryPoint(200, 200);
222: request.setInfoFormat(caps.getRequest().getGetFeatureInfo().getFormatStrings()[0]);
223:
224: System.out.println(request.getFinalURL());
225:
226: GetFeatureInfoResponse response = (GetFeatureInfoResponse) wms.issueRequest(request);
227: System.out.println(response.getContentType());
228: assertTrue( response.getContentType().indexOf("text/plain") != -1 );
229: BufferedReader in = new BufferedReader(new InputStreamReader(response.getInputStream()));
230: String line;
231:
232: boolean textFound = false;
233: while ((line = in.readLine()) != null) {
234: System.out.println(line);
235: if (line.indexOf("Wood Buffalo National Park") != -1) {
236: textFound = true;
237: }
238: }
239: assertTrue(textFound);
240: } catch(java.net.ConnectException ce){
241: if(ce.getMessage().indexOf("timed out")>0){
242: System.err.println("Unable to test - timed out: "+ce);
243: } else{
244: throw(ce);
245: }
246: }
247: */
248: }
249:
250: /**
251: * @param featureURL
252: * @throws IOException
253: * @throws URISyntaxException
254: * @throws SAXException
255: */
256: protected WebMapServer getCustomWMS(URL featureURL)
257: throws SAXException, URISyntaxException, IOException {
258: return new CustomWMS(featureURL);
259: }
260:
261: protected WMSCapabilities createCapabilitiesViaStreamingParser(
262: String capFile) throws Exception {
263: try {
264: File getCaps = TestData.file(this , capFile);
265: URL getCapsURL = getCaps.toURL();
266:
267: Object object = new Parser(new WMSV1_1_1Configuration())
268: .parse(getCapsURL.openStream());
269:
270: Schema schema = WMSSchema.getInstance();
271: SchemaFactory.getInstance(WMSSchema.NAMESPACE);
272:
273: assertTrue("Capabilities failed to parse",
274: object instanceof WMSCapabilities);
275:
276: WMSCapabilities capabilities = (WMSCapabilities) object;
277: return capabilities;
278: } catch (java.net.ConnectException ce) {
279: if (ce.getMessage().indexOf("timed out") > 0) {
280: System.err.println("Unable to test - timed out: " + ce);
281: return null;
282: } else {
283: throw (ce);
284: }
285: }
286: }
287:
288: protected WMSCapabilities createCapabilities(String capFile)
289: throws Exception {
290: try {
291: File getCaps = TestData.file(this , capFile);
292: URL getCapsURL = getCaps.toURL();
293: Map hints = new HashMap();
294: hints.put(DocumentHandler.DEFAULT_NAMESPACE_HINT_KEY,
295: WMSSchema.getInstance());
296: Object object = DocumentFactory.getInstance(getCapsURL
297: .openStream(), hints, Level.WARNING);
298:
299: Schema schema = WMSSchema.getInstance();
300: SchemaFactory.getInstance(WMSSchema.NAMESPACE);
301:
302: assertTrue("Capabilities failed to parse",
303: object instanceof WMSCapabilities);
304:
305: WMSCapabilities capabilities = (WMSCapabilities) object;
306: return capabilities;
307: } catch (java.net.ConnectException ce) {
308: if (ce.getMessage().indexOf("timed out") > 0) {
309: System.err.println("Unable to test - timed out: " + ce);
310: return null;
311: } else {
312: throw (ce);
313: }
314: }
315: }
316:
317: //forces use of 1.0.0 spec
318: private class CustomWMS extends WebMapServer {
319:
320: /**
321: * @param serverURL
322: * @param wait
323: * @throws SAXException
324: * @throws URISyntaxException
325: * @throws IOException
326: */
327: public CustomWMS(URL serverURL) throws SAXException,
328: URISyntaxException, IOException {
329: super (serverURL);
330: // TODO Auto-generated constructor stub
331: }
332:
333: protected void setupSpecifications() {
334: specs = new Specification[1];
335: specs[0] = new WMS1_0_0();
336: }
337: }
338: }
|