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.responses;
006:
007: import org.geotools.feature.FeatureType;
008: import org.geotools.filter.FilterFactory;
009: import org.geotools.filter.FilterFactoryFinder;
010: import org.geotools.styling.FeatureTypeStyle;
011: import org.geotools.styling.Fill;
012: import org.geotools.styling.Rule;
013: import org.geotools.styling.Style;
014: import org.geotools.styling.StyleFactory;
015: import org.geotools.styling.StyleFactoryFinder;
016: import org.geotools.styling.Symbolizer;
017: import org.vfny.geoserver.ServiceException;
018: import org.vfny.geoserver.global.WMS;
019: import org.vfny.geoserver.testdata.AbstractCiteDataTest;
020: import org.vfny.geoserver.testdata.MockUtils;
021: import org.vfny.geoserver.wms.requests.GetLegendGraphicRequest;
022: import org.vfny.geoserver.wms.servlets.GetLegendGraphic;
023: import java.awt.image.BufferedImage;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.util.HashMap;
027: import java.util.logging.Logger;
028:
029: /**
030: * Tets the functioning of the abstract legend producer for raster formats,
031: * which relies on Geotools' StyledShapePainter.
032: *
033: * @author Gabriel Roldan, Axios Engineering
034: * @version $Id: DefaultRasterLegendProducerTest.java 7746 2007-11-13 15:38:35Z aaime $
035: */
036: public class DefaultRasterLegendProducerTest extends
037: AbstractCiteDataTest {
038: /** DOCUMENT ME! */
039: private static final Logger LOGGER = org.geotools.util.logging.Logging
040: .getLogger(DefaultRasterLegendProducerTest.class
041: .getPackage().getName());
042:
043: /** DOCUMENT ME! */
044: private DefaultRasterLegendProducer legendProducer;
045: GetLegendGraphic service;
046:
047: /**
048: * DOCUMENT ME!
049: *
050: * @param args DOCUMENT ME!
051: */
052: public static void main(String[] args) {
053: junit.textui.TestRunner
054: .run(DefaultRasterLegendProducerTest.class);
055: }
056:
057: /**
058: * DOCUMENT ME!
059: *
060: * @throws Exception DOCUMENT ME!
061: * @throws UnsupportedOperationException DOCUMENT ME!
062: */
063: public void setUp() throws Exception {
064: super .setUp();
065: this .legendProducer = new DefaultRasterLegendProducer() {
066: public void writeTo(OutputStream out)
067: throws ServiceException, IOException {
068: throw new UnsupportedOperationException();
069: }
070:
071: public String getContentType()
072: throws java.lang.IllegalStateException {
073: throw new UnsupportedOperationException();
074: }
075: };
076:
077: WMS wms = new WMS(MockUtils.newWmsDto());
078: service = new GetLegendGraphic(wms);
079: }
080:
081: /**
082: * DOCUMENT ME!
083: *
084: * @throws Exception DOCUMENT ME!
085: */
086: public void tearDown() throws Exception {
087: this .legendProducer = null;
088: super .tearDown();
089: }
090:
091: /**
092: * Tests that a legend is produced for the explicitly specified rule, when
093: * the FeatureTypeStyle has more than one rule, and one of them is
094: * requested by the RULE parameter.
095: *
096: * @throws Exception DOCUMENT ME!
097: */
098: public void testUserSpecifiedRule() throws Exception {
099: //load a style with 3 rules
100: Style multipleRulesStyle = getDefaultStyle(ROAD_SEGMENTS_TYPE);
101: Rule rule = multipleRulesStyle.getFeatureTypeStyles()[0]
102: .getRules()[0];
103: LOGGER.info("testing single rule " + rule.getName()
104: + " from style " + multipleRulesStyle.getName());
105:
106: GetLegendGraphicRequest req = new GetLegendGraphicRequest(
107: service);
108: req.setLayer(getCiteDataStore().getSchema(ROAD_SEGMENTS_TYPE));
109: req.setStyle(multipleRulesStyle);
110: req.setRule(rule);
111: req.setLegendOptions(new HashMap());
112:
113: final int HEIGHT_HINT = 30;
114: req.setHeight(HEIGHT_HINT);
115:
116: //use default values for the rest of parameters
117: this .legendProducer.produceLegendGraphic(req);
118:
119: BufferedImage legend = this .legendProducer.getLegendGraphic();
120:
121: //was the legend painted?
122: super .assertNotBlank("testUserSpecifiedRule", legend,
123: DefaultRasterLegendProducer.BG_COLOR);
124:
125: //was created only one rule?
126: String errMsg = "expected just one legend of height "
127: + HEIGHT_HINT + ", for the rule " + rule.getName();
128: int resultLegendCount = legend.getHeight() / HEIGHT_HINT;
129: assertEquals(errMsg, 1, resultLegendCount);
130: }
131:
132: // /**
133: // * Tests that scale denominator is respected when passed as part of the
134: // * request parameters
135: // *
136: // * @throws Exception DOCUMENT ME!
137: // */
138: // public void testRespectsScale() throws Exception {
139: // Style style = createSampleStyleWithScale();
140: //
141: // GetLegendGraphicRequest req = new GetLegendGraphicRequest();
142: // req.setLayer(getCiteDataStore().getSchema(BUILDINGS_TYPE));
143: // req.setStyle(style);
144: //
145: // final int HEIGHT_HINT = 30;
146: // req.setHeight(HEIGHT_HINT);
147: //
148: // //use default values for the rest of parameters
149: // this.legendProducer.produceLegendGraphic(req);
150: //
151: // BufferedImage legend = this.legendProducer.getLegendGraphic();
152: // assertEquals("Expected two symbols since no scale was set yet",
153: // 2 * HEIGHT_HINT, legend.getHeight());
154: //
155: // req.setScale(1500);
156: // this.legendProducer.produceLegendGraphic(req);
157: // legend = this.legendProducer.getLegendGraphic();
158: // assertEquals("Expected only one symbol", HEIGHT_HINT, legend.getHeight());
159: // }
160:
161: /**
162: * Creates a Style with two rules: the first with a polygon symbolizer with
163: * all red fill for scale up to 1:1000, the second with a polygon
164: * symbolizer with an all blue fill for scale up to 1:5000.
165: *
166: * @return
167: */
168: private Style createSampleStyleWithScale() {
169: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
170: StyleFactory sf = StyleFactoryFinder.createStyleFactory();
171: Style s = sf.createStyle();
172:
173: Rule rule1_1000 = sf.createRule();
174: Fill redFill = sf.createFill(ff
175: .createLiteralExpression("0xFF0000"));
176: Symbolizer redSym = sf.createPolygonSymbolizer(null, redFill,
177: null);
178: rule1_1000.setSymbolizers(new Symbolizer[] { redSym });
179: rule1_1000.setMaxScaleDenominator(1000);
180:
181: Rule rule1_5000 = sf.createRule();
182: Fill blueFill = sf.createFill(ff
183: .createLiteralExpression("0x0000FF"));
184: Symbolizer blueSym = sf.createPolygonSymbolizer(null, blueFill,
185: null);
186: rule1_5000.setSymbolizers(new Symbolizer[] { blueSym });
187: rule1_5000.setMinScaleDenominator(1000);
188: rule1_5000.setMaxScaleDenominator(5000);
189:
190: Rule[] rules = new Rule[] { rule1_1000, rule1_5000 };
191: FeatureTypeStyle fts = sf.createFeatureTypeStyle(rules);
192:
193: s.setFeatureTypeStyles(new FeatureTypeStyle[] { fts });
194:
195: return s;
196: }
197:
198: /**
199: * Tests the legend production for the default style of the given cite type
200: * name, as defined in AbstractCiteDataTest.
201: *
202: * <p>
203: * The number of rules the default style for the given cite type name is
204: * expected at the <code>ruleCount</code> value. It is used to assert that
205: * the generated legend graphic has as many stacked graphics as rules.
206: * </p>
207: *
208: * @param citeTypeName
209: * @param ruleCount the pre-known number of rules the default style for the
210: * given cite type has.
211: *
212: * @return the legend graphic produced by DefaultRasterLegendProducer
213: *
214: * @throws Exception if something goes wrong getting the cite test data for
215: * <code>citeTypeName</code>, getting its default test style, or
216: * asking the producer to generate the legend
217: */
218: private BufferedImage testProduceLegendGraphic(String citeTypeName,
219: int ruleCount) throws Exception {
220: FeatureType layer = getCiteDataStore().getSchema(citeTypeName);
221: GetLegendGraphicRequest req = new GetLegendGraphicRequest(
222: service);
223: req.setLayer(layer);
224: req.setStyle(getDefaultStyle(citeTypeName));
225:
226: final int HEIGHT_HINT = 30;
227: req.setHeight(HEIGHT_HINT);
228: req.setWidth(30);
229:
230: this .legendProducer.produceLegendGraphic(req);
231:
232: BufferedImage legend = this .legendProducer.getLegendGraphic();
233: showImage("legend", 1000, legend);
234:
235: String errMsg = citeTypeName
236: + ": number of rules and number of legend graphics don't match";
237: assertEquals(errMsg, HEIGHT_HINT * ruleCount, legend
238: .getHeight());
239:
240: return legend;
241: }
242: }
|