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.awt.image.BufferedImage;
019: import java.net.URL;
020: import java.util.Arrays;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Random;
024: import java.util.Set;
025:
026: import javax.imageio.ImageIO;
027:
028: import org.geotools.data.ows.Layer;
029: import org.geotools.data.ows.WMSCapabilities;
030: import org.geotools.data.wms.WMSUtils;
031: import org.geotools.data.wms.WebMapServer;
032: import org.geotools.data.wms.request.GetMapRequest;
033: import org.geotools.data.wms.response.GetMapResponse;
034: import org.geotools.geometry.GeneralEnvelope;
035: import org.geotools.ows.ServiceException;
036: import org.geotools.referencing.CRS;
037: import org.opengis.layer.Style;
038: import org.opengis.referencing.crs.CoordinateReferenceSystem;
039:
040: /**
041: * @author Richard Gould
042: *
043: * TODO To change the template for this generated type comment go to
044: * Window - Preferences - Java - Code Style - Code Templates
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/test/java/org/geotools/data/wms/test/WebMapServerOnlineTest.java $
046: */
047: public class WebMapServerOnlineTest extends ServerTestCase {
048: URL serverURL;
049: URL brokenURL;
050: private URL featureURL;
051:
052: /*
053: * @see TestCase#setUp()
054: */
055: protected void setUp() throws Exception {
056: super .setUp();
057: // serverURL = new URL("http://demo.cubewerx.com/demo/cubeserv/cubeserv.cgi?CONFIG=main&SERVICE=WMS&?VERSION=1.3.0&REQUEST=GetCapabilities");
058: serverURL = new URL(
059: "http://terraservice.net/ogccapabilities.ashx?version=1.1.1&request=GetCapabilties");
060: featureURL = new URL(
061: "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap?VERSION=1.1.0&REQUEST=GetCapabilities");
062: brokenURL = new URL("http://afjklda.com");
063: }
064:
065: /*
066: * Class under test for void WebMapServer(URL)
067: */
068: public void testWebMapServerURL() throws Exception {
069: WebMapServer wms = new WebMapServer(serverURL);
070:
071: assertNotNull(wms.getCapabilities());
072: }
073:
074: public void testGetCapabilities() throws Exception {
075: WebMapServer wms = new WebMapServer(serverURL);
076:
077: assertNotNull(wms.getCapabilities());
078: }
079:
080: public void testIssueGetMapRequest() throws Exception {
081: WebMapServer wms = new WebMapServer(serverURL);
082:
083: WMSCapabilities capabilities = wms.getCapabilities();
084:
085: GetMapRequest request = wms.createGetMapRequest();
086:
087: //request.setVersion("1.1.1");
088:
089: Layer[] layers = WMSUtils.getNamedLayers(capabilities);
090: Iterator iter = Arrays.asList(layers).iterator();
091: int count = -1;
092: while (iter.hasNext()) {
093:
094: Layer layer = (Layer) iter.next();
095: count++;
096: if (count >= 5) {
097: break;
098: }
099:
100: List styles = layer.getStyles();
101:
102: if (styles.size() == 0) {
103: request.addLayer(layer);
104: continue;
105: }
106:
107: Random random = new Random();
108: int randomInt = random.nextInt(styles.size());
109:
110: request.addLayer(layer, (Style) styles.get(randomInt));
111: }
112:
113: Set srss = WMSUtils.getSRSs(capabilities);
114: request.setSRS((String) srss.iterator().next());
115: request.setDimensions("400", "400");
116:
117: String format = "image/gif";
118: List formats = wms.getCapabilities().getRequest().getGetMap()
119: .getFormats();
120: if (!formats.contains("image/gif")) {
121: format = (String) formats.get(0);
122: }
123: request.setFormat(format);
124:
125: request.setBBox("366800,2170400,816000,2460400");
126:
127: //System.out.println(request.getFinalURL());
128: GetMapResponse response = (GetMapResponse) wms
129: .issueRequest(request);
130:
131: assertEquals(response.getContentType(), format);
132: //System.out.println("Content Type: " + response.getContentType());
133:
134: BufferedImage image = ImageIO.read(response.getInputStream());
135: assertEquals(image.getHeight(), 400);
136: }
137:
138: public void testIssueGetFeatureInfoRequest() throws Exception {
139: /* TODO fix this
140:
141: // http://dev1.dmsolutions.ca/cgi-bin/mswms_gmap?LAYERS=DEMO&FORMAT=image/png&TRANSPARENT=TRUE&HEIGHT=213&REQUEST=GetMap&BBOX=-172.367,35.667300000000004,-11.562400000000014,83.8293&WIDTH=710&STYLES=&SRS=EPSG:4326&VERSION=1.1.1
142:
143: WebMapServer wms = new WebMapServer(featureURL);
144: WMSCapabilities capabilities = wms.getCapabilities();
145:
146: assertNotNull(capabilities);
147:
148: GetMapRequest getMapRequest = wms.createGetMapRequest();
149:
150: List layers = Arrays.asList(WMSUtils.getNamedLayers(capabilities));
151: List simpleLayers = new ArrayList();
152: Iterator iter = layers.iterator();
153: while (iter.hasNext()) {
154: Layer layer = (Layer) iter.next();
155: SimpleLayer sLayer = new SimpleLayer(layer.getName(), "");
156: simpleLayers.add(sLayer);
157: List styles = layer.getStyles();
158: if (styles.size() == 0) {
159: sLayer.setStyle("");
160: continue;
161: }
162: Random random = new Random();
163: int randomInt = random.nextInt(styles.size());
164: sLayer.setStyle((String) styles.get(randomInt));
165: }
166: getMapRequest.setLayers(simpleLayers);
167:
168: getMapRequest.setSRS("EPSG:4326");
169: getMapRequest.setDimensions("400", "400");
170: getMapRequest.setFormat("image/png");
171:
172: getMapRequest.setBBox("-114.01268,59.4596930,-113.26043,60.0835794");
173: URL url2 = getMapRequest.getFinalURL();
174:
175: GetFeatureInfoRequest request = wms.createGetFeatureInfoRequest(getMapRequest);
176: request.setQueryLayers(WMSUtils.getQueryableLayers(capabilities));
177: request.setQueryPoint(200, 200);
178: request.setInfoFormat("text/html");
179:
180: System.out.println(request.getFinalURL());
181:
182: GetFeatureInfoResponse response = (GetFeatureInfoResponse) wms.issueRequest(request);
183: System.out.println(response.getContentType());
184: assertTrue( response.getContentType().indexOf("text/html") != -1 );
185: BufferedReader in = new BufferedReader(new InputStreamReader(response.getInputStream()));
186: String line;
187:
188: boolean textFound = false;
189: while ((line = in.readLine()) != null) {
190: System.out.println(line);
191: if (line.indexOf("Wood Buffalo National Park") != -1) {
192: textFound = true;
193: }
194: }
195: assertTrue(textFound);
196: */
197: }
198:
199: public void testGetEnvelope() throws Exception {
200: WebMapServer wms = new WebMapServer(featureURL);
201:
202: WMSCapabilities caps = wms.getCapabilities();
203:
204: Layer layer = (Layer) caps.getLayerList().get(1);
205: CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
206:
207: GeneralEnvelope envelope = wms.getEnvelope(layer, crs);
208:
209: // minx="-172.367" miny="35.6673" maxx="-11.5624" maxy="83.8293" />
210: assertEquals(envelope.getMinimum(0), -172.367, 0.0);
211: assertEquals(envelope.getMinimum(1), 35.6673, 0.0);
212: assertEquals(envelope.getMaximum(0), -11.5624, 0.0);
213: assertEquals(envelope.getMaximum(1), 83.8293, 0.0);
214:
215: crs = CRS.decode("EPSG:42304");
216: envelope = wms.getEnvelope(layer, crs);
217:
218: // minx="-2.2e+06" miny="-712631" maxx="3.0728e+06" maxy="3.84e+06" />
219: assertEquals(envelope.getMinimum(0), -2.2e+06, 0.0);
220: assertEquals(envelope.getMinimum(1), -712631, 0.0);
221: assertEquals(envelope.getMaximum(0), 3.0728e+06, 0.0);
222: assertEquals(envelope.getMaximum(1), 3.84e+06, 0.0);
223:
224: layer = (Layer) caps.getLayerList().get(2);
225: crs = CRS.decode("EPSG:4326");
226:
227: envelope = wms.getEnvelope(layer, crs);
228:
229: // minx="-178.838" miny="31.8844" maxx="179.94" maxy="89.8254" />
230: assertEquals(envelope.getMinimum(0), -178.838, 0.0);
231: assertEquals(envelope.getMinimum(1), 31.8844, 0.0);
232: assertEquals(envelope.getMaximum(0), 179.94, 0.0);
233: assertEquals(envelope.getMaximum(1), 89.8254, 0.0);
234: }
235:
236: public void testServiceExceptions() throws Exception {
237: WebMapServer wms = new WebMapServer(featureURL);
238: GetMapRequest request = wms.createGetMapRequest();
239: request.addLayer("NoLayer", "NoStyle");
240: try {
241: //System.out.println(request.getFinalURL());
242: GetMapResponse response = wms.issueRequest(request);
243: assertTrue(false);
244: } catch (ServiceException e) {
245: //e.printStackTrace();
246: }
247: }
248: }
|