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.vfny.geoserver.wms.requests;
006:
007: import com.mockrunner.mock.web.MockHttpServletRequest;
008: import junit.framework.TestCase;
009: import org.geotools.styling.Style;
010: import org.vfny.geoserver.Response;
011: import org.vfny.geoserver.ServiceException;
012: import org.vfny.geoserver.global.Config;
013: import org.vfny.geoserver.global.Data;
014: import org.vfny.geoserver.global.GeoServer;
015: import org.vfny.geoserver.global.WMS;
016: import org.vfny.geoserver.servlets.AbstractService;
017: import org.vfny.geoserver.testdata.MockUtils;
018: import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
019: import org.vfny.geoserver.util.requests.readers.XmlRequestReader;
020: import org.vfny.geoserver.wms.servlets.GetLegendGraphic;
021: import org.vfny.geoserver.wms.servlets.WMService;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: public class GetLegendGraphicKvpReaderTest extends TestCase {
027: /**
028: * request reader to test against, initialized by default with all
029: * parameters from <code>requiredParameters</code> and
030: * <code>optionalParameters</code>
031: */
032: GetLegendGraphicKvpReader requestReader;
033:
034: /** test values for required parameters */
035: Map requiredParameters;
036:
037: /** test values for optional parameters */
038: Map optionalParameters;
039:
040: /** both required and optional parameters joint up */
041: Map allParameters;
042:
043: /** mock request */
044: MockHttpServletRequest httpRequest;
045:
046: /** mock servlet */
047: WMService dummy;
048:
049: /**
050: * Remainder:
051: * <ul>
052: * <li>VERSION/Required
053: * <li>REQUEST/Required
054: * <li>LAYER/Required
055: * <li>FORMAT/Required
056: * <li>STYLE/Optional
057: * <li>FEATURETYPE/Optional
058: * <li>RULE/Optional
059: * <li>SCALE/Optional
060: * <li>SLD/Optional
061: * <li>SLD_BODY/Optional
062: * <li>WIDTH/Optional
063: * <li>HEIGHT/Optional
064: * <li>EXCEPTIONS/Optional
065: * </ul>
066: */
067: protected void setUp() throws Exception {
068: super .setUp();
069: requiredParameters = new HashMap();
070: requiredParameters.put("VERSION", "1.0.0");
071: requiredParameters.put("REQUEST", "GetLegendGraphic");
072: requiredParameters.put("LAYER", "cite:Ponds");
073: requiredParameters.put("FORMAT", "image/png");
074:
075: optionalParameters = new HashMap();
076: optionalParameters.put("STYLE", "Ponds");
077: optionalParameters.put("FEATURETYPE", "fake_not_used");
078: // optionalParameters.put("RULE", "testRule");
079: optionalParameters.put("SCALE", "1000");
080: optionalParameters.put("WIDTH", "120");
081: optionalParameters.put("HEIGHT", "90");
082: // ??optionalParameters.put("EXCEPTIONS", "");
083: allParameters = new HashMap(requiredParameters);
084: allParameters.putAll(optionalParameters);
085:
086: Data data = MockUtils.createTestCiteData(new GeoServer());
087: WMS wms = new WMS(MockUtils.newWmsDto());
088: wms.setData(data);
089:
090: GetLegendGraphic service = new GetLegendGraphic(wms);
091: this .requestReader = new GetLegendGraphicKvpReader(
092: allParameters, service);
093: this .httpRequest = MockUtils
094: .newHttpRequest(allParameters, true);
095:
096: dummy = new WMService("dummy", wms) {
097: protected Response getResponseHandler() {
098: return null;
099: }
100:
101: protected KvpRequestReader getKvpReader(Map params) {
102: return null;
103: }
104:
105: protected XmlRequestReader getXmlRequestReader() {
106: return null;
107: }
108: };
109: }
110:
111: protected void tearDown() throws Exception {
112: super .tearDown();
113: }
114:
115: // disabled this test for the moment as a
116: // fix for http://jira.codehaus.org/browse/GEOS-710
117: // Since at the moment none of the other request do check the version numbers, we
118: // disable this check for the moment, and wait for a proper fix once the
119: // we support more than one version of WMS/WFS specs
120:
121: // public void testVersion() throws Exception {
122: // requiredParameters.put("VERSION", "WRONG");
123: //
124: // this.requestReader =
125: // new GetLegendGraphicKvpReader(requiredParameters, dummy);
126: // try {
127: // requestReader.getRequest(httpRequest);
128: // fail("Expected ServiceException due to wrong VERSION parameter");
129: // } catch (ServiceException e) {
130: // // OK
131: // }
132: // requiredParameters.put("VERSION", "1.0.0");
133: // GetLegendGraphicRequest parsedRequest;
134: // parsedRequest = (GetLegendGraphicRequest) requestReader
135: // .getRequest(httpRequest);
136: // }
137:
138: /**
139: * This test ensures that when a SLD parameter has been passed that refers
140: * to a SLD document with multiple styles, the required one is choosed based
141: * on the LAYER parameter.
142: * <p>
143: * This is the case where a remote SLD document is used in "library" mode.
144: * </p>
145: */
146: public void testRemoteSLDMultipleStyles() throws ServiceException {
147: final URL remoteSldUrl = getClass().getResource(
148: "test-data/MultipleStyles.sld");
149: this .allParameters.put("SLD", remoteSldUrl.toExternalForm());
150:
151: this .allParameters.put("LAYER", "cite:Ponds");
152: this .allParameters.put("STYLE", "Ponds");
153: requestReader = new GetLegendGraphicKvpReader(
154: this .allParameters, dummy);
155:
156: GetLegendGraphicRequest request = (GetLegendGraphicRequest) requestReader
157: .getRequest(httpRequest);
158:
159: //the style names Ponds is declared in third position on the sld doc
160: Style selectedStyle = request.getStyle();
161: assertNotNull(selectedStyle);
162: assertEquals("Ponds", selectedStyle.getName());
163:
164: this .allParameters.put("LAYER", "cite:Lakes");
165: this .allParameters.put("STYLE", "Lakes");
166: requestReader = new GetLegendGraphicKvpReader(
167: this .allParameters, dummy);
168: request = (GetLegendGraphicRequest) requestReader
169: .getRequest(httpRequest);
170:
171: //the style names Ponds is declared in third position on the sld doc
172: selectedStyle = request.getStyle();
173: assertNotNull(selectedStyle);
174: assertEquals("Lakes", selectedStyle.getName());
175: }
176: }
|