001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.wms.kvp;
006:
007: import java.awt.Color;
008: import java.awt.geom.Point2D;
009: import java.io.IOException;
010: import java.net.URL;
011: import java.util.Calendar;
012: import java.util.HashMap;
013: import java.util.List;
014: import java.util.Map;
015: import java.util.logging.Level;
016:
017: import org.geoserver.data.test.MockData;
018: import org.geoserver.ows.Dispatcher;
019: import org.geoserver.test.ows.KvpRequestReaderTestSupport;
020: import org.geoserver.wms.RemoteOWSTestSupport;
021: import org.geotools.data.DataStore;
022: import org.geotools.data.wfs.WFSDataStoreFactory;
023: import org.geotools.styling.Style;
024: import org.opengis.filter.Id;
025: import org.opengis.filter.PropertyIsEqualTo;
026: import org.vfny.geoserver.config.PaletteManager;
027: import org.vfny.geoserver.global.MapLayerInfo;
028: import org.vfny.geoserver.global.WMS;
029: import org.vfny.geoserver.wms.WmsException;
030: import org.vfny.geoserver.wms.requests.GetMapRequest;
031: import org.vfny.geoserver.wms.servlets.GetMap;
032:
033: public class GetMapKvpRequestReaderTest extends
034: KvpRequestReaderTestSupport {
035: GetMapKvpRequestReader reader;
036: Dispatcher dispatcher;
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040:
041: GetMap getMap = (GetMap) applicationContext
042: .getBean("wmsGetMap");
043: dispatcher = (Dispatcher) applicationContext
044: .getBean("dispatcher");
045: WMS wms = (WMS) applicationContext.getBean("wms");
046: reader = new GetMapKvpRequestReader(getMap, wms);
047: }
048:
049: public void testCreateRequest() throws Exception {
050: GetMapRequest request = (GetMapRequest) reader.createRequest();
051: assertNotNull(request);
052: }
053:
054: public void testReadMandatory() throws Exception {
055: HashMap raw = new HashMap();
056: raw.put("layers", MockData.BASIC_POLYGONS.getPrefix() + ":"
057: + MockData.BASIC_POLYGONS.getLocalPart());
058: raw.put("styles", MockData.BASIC_POLYGONS.getLocalPart());
059: raw.put("format", "image/jpeg");
060: raw.put("srs", "epsg:3003");
061: raw.put("bbox", "-10,-10,10,10");
062: raw.put("height", "600");
063: raw.put("width", "800");
064:
065: GetMapRequest request = (GetMapRequest) reader.createRequest();
066: request = (GetMapRequest) reader.read(request, parseKvp(raw),
067: raw);
068:
069: String layer = MockData.BASIC_POLYGONS.getLocalPart();
070: assertEquals(1, request.getLayers().length);
071: assertTrue(request.getLayers()[0].getName().endsWith(layer));
072:
073: assertEquals(1, request.getStyles().size());
074: assertEquals(getCatalog().getStyle(layer), request.getStyles()
075: .get(0));
076:
077: assertEquals("image/jpeg", request.getFormat());
078: assertEquals(600, request.getHeight());
079: assertEquals(800, request.getWidth());
080:
081: assertNotNull(request.getBbox());
082: assertEquals(-10d, request.getBbox().getMinX(), 0);
083: assertEquals(-10d, request.getBbox().getMinY(), 0);
084: assertEquals(10d, request.getBbox().getMaxX(), 0);
085: assertEquals(10d, request.getBbox().getMaxY(), 0);
086:
087: assertEquals("epsg:3003", request.getSRS());
088: }
089:
090: public void testReadOptional() throws Exception {
091: HashMap kvp = new HashMap();
092: kvp.put("bgcolor", "000000");
093: kvp.put("transparent", "true");
094: kvp.put("tiled", "true");
095: kvp.put("tilesorigin", "1.2,3.4");
096: kvp.put("buffer", "1");
097: kvp.put("palette", "SAFE");
098: kvp.put("time", "2006-02-27T22:08:12Z");
099: kvp.put("elevation", "4");
100:
101: GetMapRequest request = (GetMapRequest) reader.createRequest();
102: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
103: kvp);
104:
105: assertEquals(Color.BLACK, request.getBgColor());
106: assertTrue(request.isTransparent());
107: assertTrue(request.isTiled());
108:
109: assertEquals(new Point2D.Double(1.2, 3.4), request
110: .getTilesOrigin());
111: assertEquals(1, request.getBuffer());
112:
113: assertEquals(PaletteManager.safePalette, request.getPalette()
114: .getIcm());
115: assertEquals(new Integer(4), request.getElevation());
116:
117: Calendar cal = Calendar.getInstance();
118: cal.set(2006, 1, 27, 22, 8, 12);
119: List times = request.getTime();
120: assertEquals(1, request.getTime().size());
121: assertEquals(cal.getTime().toString(), times.get(0).toString());
122: }
123:
124: public void testDefaultStyle() throws Exception {
125: HashMap raw = new HashMap();
126: raw.put("layers", MockData.BASIC_POLYGONS.getPrefix() + ":"
127: + MockData.BASIC_POLYGONS.getLocalPart() + ","
128: + MockData.BUILDINGS.getPrefix() + ":"
129: + MockData.BUILDINGS.getLocalPart());
130: raw.put("styles", ",");
131: raw.put("format", "image/jpeg");
132: raw.put("srs", "epsg:3003");
133: raw.put("bbox", "-10,-10,10,10");
134: raw.put("height", "600");
135: raw.put("width", "800");
136:
137: GetMapRequest request = (GetMapRequest) reader.createRequest();
138: request = (GetMapRequest) reader.read(request, parseKvp(raw),
139: raw);
140: assertEquals(2, request.getStyles().size());
141: }
142:
143: public void testFilter() throws Exception {
144: HashMap kvp = new HashMap();
145: kvp.put("filter", "<Filter><FeatureId id=\"foo\"/></Filter>");
146:
147: GetMapRequest request = (GetMapRequest) reader.createRequest();
148: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
149: kvp);
150:
151: assertNotNull(request.getFilter());
152: assertEquals(1, request.getFilter().size());
153:
154: Id fid = (Id) request.getFilter().get(0);
155: assertEquals(1, fid.getIDs().size());
156:
157: assertEquals("foo", fid.getIDs().iterator().next());
158: }
159:
160: public void testCQLFilter() throws Exception {
161: HashMap kvp = new HashMap();
162: kvp.put("cql_filter", "foo = bar");
163:
164: GetMapRequest request = (GetMapRequest) reader.createRequest();
165: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
166: kvp);
167:
168: assertNotNull(request.getCQLFilter());
169: assertEquals(1, request.getCQLFilter().size());
170:
171: PropertyIsEqualTo filter = (PropertyIsEqualTo) request
172: .getCQLFilter().get(0);
173: }
174:
175: public void testFeatureId() throws Exception {
176: HashMap kvp = new HashMap();
177: kvp.put("featureid", "foo");
178:
179: GetMapRequest request = (GetMapRequest) reader.createRequest();
180: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
181: kvp);
182:
183: assertNotNull(request.getFeatureId());
184: assertEquals(1, request.getFeatureId().size());
185:
186: assertEquals("foo", request.getFeatureId().get(0));
187: }
188:
189: public void testSldNoDefault() throws Exception {
190: // no style name, no default, we should fall back on the server default
191: HashMap kvp = new HashMap();
192: URL url = GetMapKvpRequestReader.class
193: .getResource("BasicPolygonsLibraryNoDefault.sld");
194: kvp.put("sld", url.toString());
195: kvp.put("layers", MockData.BASIC_POLYGONS.getPrefix() + ":"
196: + MockData.BASIC_POLYGONS.getLocalPart());
197:
198: GetMapRequest request = (GetMapRequest) reader.createRequest();
199: reader.setLaxStyleMatchAllowed(false);
200: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
201: kvp);
202:
203: assertNotNull(request.getSld());
204: assertEquals(url, request.getSld());
205: final Style style = (Style) request.getStyles().get(0);
206: assertNotNull(style);
207: assertEquals("BasicPolygons", style.getName());
208: }
209:
210: public void testSldDefault() throws Exception {
211: // no style name, but the sld has a default for that layer
212: HashMap kvp = new HashMap();
213: URL url = GetMapKvpRequestReader.class
214: .getResource("BasicPolygonsLibraryDefault.sld");
215: kvp.put("sld", url.toString());
216: kvp.put("layers", MockData.BASIC_POLYGONS.getPrefix() + ":"
217: + MockData.BASIC_POLYGONS.getLocalPart());
218:
219: GetMapRequest request = (GetMapRequest) reader.createRequest();
220: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
221: kvp);
222:
223: assertNotNull(request.getSld());
224: assertEquals(url, request.getSld());
225: final Style style = (Style) request.getStyles().get(0);
226: assertNotNull(style);
227: assertEquals("TheLibraryModeStyle", style.getName());
228: }
229:
230: public void testSldNamed() throws Exception {
231: // style name matching one in the sld
232: HashMap kvp = new HashMap();
233: URL url = GetMapKvpRequestReader.class
234: .getResource("BasicPolygonsLibraryNoDefault.sld");
235: kvp.put("sld", url.toString());
236: kvp.put("layers", MockData.BASIC_POLYGONS.getPrefix() + ":"
237: + MockData.BASIC_POLYGONS.getLocalPart());
238: kvp.put("styles", "TheLibraryModeStyle");
239:
240: GetMapRequest request = (GetMapRequest) reader.createRequest();
241: request = (GetMapRequest) reader.read(request, parseKvp(kvp),
242: kvp);
243:
244: assertNotNull(request.getSld());
245: assertEquals(url, request.getSld());
246: final Style style = (Style) request.getStyles().get(0);
247: assertNotNull(style);
248: assertEquals("TheLibraryModeStyle", style.getName());
249: }
250:
251: public void testSldFailLookup() throws Exception {
252: // nothing matches the required style name
253: HashMap kvp = new HashMap();
254: URL url = GetMapKvpRequestReader.class
255: .getResource("BasicPolygonsLibraryNoDefault.sld");
256: kvp.put("sld", url.toString());
257: kvp.put("layers", MockData.BASIC_POLYGONS.getPrefix() + ":"
258: + MockData.BASIC_POLYGONS.getLocalPart());
259: kvp.put("styles", "ThisStyleDoesNotExists");
260:
261: GetMapRequest request = (GetMapRequest) reader.createRequest();
262: try {
263: reader.setLaxStyleMatchAllowed(false);
264: request = (GetMapRequest) reader.read(request,
265: parseKvp(kvp), kvp);
266: fail("The style looked up, 'ThisStyleDoesNotExists', should not have been found");
267: } catch (WmsException e) {
268: System.out.println(e);
269: }
270: }
271:
272: /**
273: * One of the cite tests ensures that WMTVER is recognized as VERSION and the server does
274: * not complain
275: * @throws Exception
276: */
277: public void testWmtVer() throws Exception {
278: dispatcher.setCiteCompliant(true);
279: String request = "wms?SERVICE=WMS&&WiDtH=200&FoRmAt=image/png&LaYeRs=cite:Lakes&StYlEs=&BbOx=0,-0.0020,0.0040,0&ReQuEsT=GetMap&HeIgHt=100&SrS=EPSG:4326&WmTvEr=1.1.1";
280: assertEquals("image/png", getAsServletResponse(request)
281: .getContentType());
282: }
283:
284: public void testRemoteWFS() throws Exception {
285: if (!RemoteOWSTestSupport.isRemoteStatesAvailable())
286: return;
287:
288: HashMap raw = new HashMap();
289: raw.put("layers", "topp:states");
290: raw.put("styles", MockData.BASIC_POLYGONS.getLocalPart());
291: raw.put("format", "image/png");
292: raw.put("srs", "epsg:4326");
293: raw.put("bbox", "-100,20,-60,50");
294: raw.put("height", "300");
295: raw.put("width", "300");
296: raw.put("remote_ows_type", "WFS");
297: raw.put("remote_ows_url", RemoteOWSTestSupport.WFS_SERVER_URL);
298:
299: GetMapRequest request = (GetMapRequest) reader.createRequest();
300: request = (GetMapRequest) reader.read(request, parseKvp(raw),
301: raw);
302:
303: assertEquals("WFS", request.getRemoteOwsType()); // TODO: handle case?
304: assertEquals(new URL(
305: "http://sigma.openplans.org:8080/geoserver/wfs?"),
306: request.getRemoteOwsURL());
307: assertEquals(1, request.getLayers().length);
308: assertEquals(MapLayerInfo.TYPE_REMOTE_VECTOR, request
309: .getLayers()[0].getType());
310: assertEquals("topp:states", request.getLayers()[0]
311: .getRemoteFeatureSource().getSchema().getTypeName());
312: }
313:
314: public void testRemoteWFSNoStyle() throws Exception {
315: if (!RemoteOWSTestSupport.isRemoteStatesAvailable())
316: return;
317:
318: HashMap raw = new HashMap();
319: raw.put("layers", "topp:states");
320: raw.put("format", "image/png");
321: raw.put("srs", "epsg:4326");
322: raw.put("bbox", "-100,20,-60,50");
323: raw.put("height", "300");
324: raw.put("width", "300");
325: raw.put("remote_ows_type", "WFS");
326: raw.put("remote_ows_url", RemoteOWSTestSupport.WFS_SERVER_URL);
327:
328: GetMapRequest request = (GetMapRequest) reader.createRequest();
329: try {
330: request = (GetMapRequest) reader.read(request,
331: parseKvp(raw), raw);
332: fail("This should have thrown an exception because of the missing style");
333: } catch (WmsException e) {
334: assertEquals("NoDefaultStyle", e.getCode());
335: }
336: }
337:
338: public void testRemoteWFSInvalidURL() throws Exception {
339: if (!RemoteOWSTestSupport.isRemoteStatesAvailable())
340: return;
341:
342: HashMap raw = new HashMap();
343: raw.put("layers", "topp:states");
344: raw.put("format", "image/png");
345: raw.put("srs", "epsg:4326");
346: raw.put("bbox", "-100,20,-60,50");
347: raw.put("height", "300");
348: raw.put("width", "300");
349: raw.put("remote_ows_type", "WFS");
350: raw.put("remote_ows_url",
351: "http://phantom.openplans.org:8080/crapserver/wfs?");
352:
353: GetMapRequest request = (GetMapRequest) reader.createRequest();
354: try {
355: request = (GetMapRequest) reader.read(request,
356: parseKvp(raw), raw);
357: fail("This should have thrown an exception because of the non existent layer");
358: } catch (WmsException e) {
359: e.printStackTrace();
360: assertEquals("RemoteOWSFailure", e.getCode());
361: }
362: }
363: }
|