001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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.styling;
017:
018: import java.util.Set;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.geotools.factory.CommonFactoryFinder;
025: import org.geotools.feature.AttributeType;
026: import org.geotools.feature.AttributeTypeFactory;
027: import org.geotools.feature.FeatureType;
028: import org.geotools.feature.FeatureTypeFactory;
029: import org.geotools.filter.FilterFactoryFinder;
030: import org.geotools.filter.IllegalFilterException;
031: import org.opengis.filter.Filter;
032: import org.opengis.filter.FilterFactory;
033: import org.opengis.filter.expression.Expression;
034:
035: import com.vividsolutions.jts.geom.LineString;
036:
037: /**
038: * Tests style cloning
039: *
040: * @author Sean Geoghegan
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/styling/StyleAttributeExtractorTest.java $
042: */
043: public class StyleAttributeExtractorTest extends TestCase {
044: private StyleFactory styleFactory;
045: private FilterFactory filterFactory;
046: private FeatureType testSchema = null;
047:
048: /**
049: * Constructor for StyleCloneTest.
050: *
051: * @param arg0
052: */
053: public StyleAttributeExtractorTest(String arg0) {
054: super (arg0);
055: }
056:
057: /*
058: * @see TestCase#setUp()
059: */
060: protected void setUp() throws Exception {
061: styleFactory = StyleFactoryFinder.createStyleFactory();
062: filterFactory = CommonFactoryFinder.getFilterFactory(null);
063:
064: // Create the schema attributes
065: AttributeType geometryAttribute = AttributeTypeFactory
066: .newAttributeType("testGeometry", LineString.class);
067:
068: AttributeType booleanAttribute = AttributeTypeFactory
069: .newAttributeType("testBoolean", Boolean.class);
070:
071: AttributeType charAttribute = AttributeTypeFactory
072: .newAttributeType("testCharacter", Character.class);
073: AttributeType byteAttribute = AttributeTypeFactory
074: .newAttributeType("testByte", Byte.class);
075: AttributeType shortAttribute = AttributeTypeFactory
076: .newAttributeType("testShort", Short.class);
077: AttributeType intAttribute = AttributeTypeFactory
078: .newAttributeType("testInteger", Integer.class);
079: AttributeType longAttribute = AttributeTypeFactory
080: .newAttributeType("testLong", Long.class);
081: AttributeType floatAttribute = AttributeTypeFactory
082: .newAttributeType("testFloat", Float.class);
083: AttributeType doubleAttribute = AttributeTypeFactory
084: .newAttributeType("testDouble", Double.class);
085: AttributeType stringAttribute = AttributeTypeFactory
086: .newAttributeType("testString", String.class);
087: AttributeType stringAttribute2 = AttributeTypeFactory
088: .newAttributeType("testString2", String.class);
089:
090: // Builds the schema
091: FeatureTypeFactory feaTypeFactory = FeatureTypeFactory
092: .newInstance("test");
093: feaTypeFactory.addType(geometryAttribute);
094:
095: feaTypeFactory.addType(booleanAttribute);
096:
097: feaTypeFactory.addType(charAttribute);
098:
099: feaTypeFactory.addType(byteAttribute);
100:
101: feaTypeFactory.addType(shortAttribute);
102:
103: //LOGGER.finer("added short to feature type");
104: feaTypeFactory.addType(intAttribute);
105:
106: //LOGGER.finer("added int to feature type");
107: feaTypeFactory.addType(longAttribute);
108:
109: //LOGGER.finer("added long to feature type");
110: feaTypeFactory.addType(floatAttribute);
111:
112: //LOGGER.finer("added float to feature type");
113: feaTypeFactory.addType(doubleAttribute);
114:
115: //LOGGER.finer("added double to feature type");
116: feaTypeFactory.addType(stringAttribute);
117: feaTypeFactory.addType(stringAttribute2);
118:
119: testSchema = feaTypeFactory.getFeatureType();
120: }
121:
122: /*
123: * @see TestCase#tearDown()
124: */
125: protected void tearDown() throws Exception {
126: styleFactory = null;
127: }
128:
129: private void assertAttributeName(Style s, String name) {
130: assertAttributeName(s, new String[] { name });
131: }
132:
133: private void assertAttributeName(Style style, String[] names) {
134: StyleAttributeExtractor sae = new StyleAttributeExtractor();
135: style.accept(sae);
136:
137: Set attNames = sae.getAttributeNameSet();
138:
139: assertNotNull(attNames);
140: assertEquals(names.length, attNames.size());
141:
142: for (int i = 0; i < names.length; i++) {
143: assertTrue(attNames.contains(names[i]));
144: }
145: }
146:
147: private Style createStyle() {
148: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
149: Rule rule1 = styleFactory.createRule();
150: fts.addRule(rule1);
151:
152: Rule rule2 = styleFactory.createRule();
153: fts.addRule(rule2);
154: fts.setFeatureTypeName("feature-type-1");
155:
156: FeatureTypeStyle fts2 = styleFactory.createFeatureTypeStyle();
157: fts2.setFeatureTypeName("feature-type-2");
158:
159: Style style = styleFactory.getDefaultStyle();
160: style.addFeatureTypeStyle(fts);
161: style.addFeatureTypeStyle(fts2);
162:
163: return style;
164: }
165:
166: public void testStyle() throws Exception {
167: Style s = createStyle();
168: assertAttributeName(s, new String[0]);
169: }
170:
171: public void testRule() throws Exception {
172: Symbolizer symb1 = styleFactory.createLineSymbolizer(
173: styleFactory.getDefaultStroke(), "geometry");
174: Symbolizer symb2 = styleFactory.createPolygonSymbolizer(
175: styleFactory.getDefaultStroke(), styleFactory
176: .getDefaultFill(), "shape");
177: Rule rule = styleFactory.createRule();
178: rule.setSymbolizers(new Symbolizer[] { symb1, symb2 });
179:
180: Style s = createStyle();
181: s.getFeatureTypeStyles()[0].addRule(rule);
182: assertAttributeName(s, new String[] { "geometry", "shape" });
183:
184: Filter f = filterFactory.equals(filterFactory
185: .property("testLong"), filterFactory.literal(10.0));
186: rule.setFilter(f);
187:
188: assertAttributeName(s, new String[] { "geometry", "shape",
189: "testLong" });
190: }
191:
192: public void testPointSymbolizer() throws Exception {
193: PointSymbolizer pointSymb = styleFactory
194: .createPointSymbolizer();
195: ExternalGraphic eg = styleFactory.createExternalGraphic(
196: "www.test.com", "image/png");
197: Mark mark = styleFactory.createMark();
198: Stroke stroke = styleFactory.getDefaultStroke();
199: stroke.setWidth(filterFactory.property("testInteger"));
200: mark.setStroke(stroke);
201: mark.setWellKnownName(filterFactory.property("testString"));
202:
203: Expression opacity = filterFactory.property("testLong");
204: Expression rotation = filterFactory.property("testDouble");
205: Expression size = filterFactory.property("testFloat");
206: Graphic g = styleFactory.createGraphic(
207: new ExternalGraphic[] { eg }, new Mark[] { mark },
208: null, opacity, rotation, size);
209: pointSymb.setGraphic(g);
210:
211: Style s = createStyle();
212: s.getFeatureTypeStyles()[0].getRules()[0]
213: .setSymbolizers(new Symbolizer[] { pointSymb });
214: assertAttributeName(s, new String[] { "testInteger",
215: "testLong", "testDouble", "testFloat", "testString" });
216:
217: pointSymb.setGeometryPropertyName("testGeometry");
218: assertAttributeName(s, new String[] { "testInteger",
219: "testLong", "testDouble", "testFloat", "testString",
220: "testGeometry" });
221: }
222:
223: public void testTextSymbolizer() throws Exception {
224: TextSymbolizer textSymb = styleFactory.createTextSymbolizer();
225: Expression offset = filterFactory.property("testInteger");
226: Expression label = filterFactory.property("testString");
227: textSymb.setLabelPlacement(styleFactory
228: .createLinePlacement(offset));
229: textSymb.setLabel(label);
230:
231: Style s = createStyle();
232: s.getFeatureTypeStyles()[0].getRules()[0]
233: .setSymbolizers(new Symbolizer[] { textSymb });
234: assertAttributeName(s, new String[] { "testInteger",
235: "testString" });
236:
237: Expression ancX = filterFactory.property("testFloat");
238: Expression ancY = filterFactory.property("testDouble");
239: AnchorPoint ancPoint = styleFactory.createAnchorPoint(ancX,
240: ancY);
241: LabelPlacement placement = styleFactory.createPointPlacement(
242: ancPoint, null, null);
243: textSymb.setLabelPlacement(placement);
244:
245: assertAttributeName(s, new String[] { "testFloat",
246: "testDouble", "testString" });
247: }
248:
249: public void testFont() throws Exception {
250: Font font = styleFactory.createFont(filterFactory
251: .property("testString"), filterFactory
252: .property("testString2"), filterFactory
253: .property("testLong"), filterFactory
254: .property("testBoolean"));
255:
256: TextSymbolizer textSymb = styleFactory.createTextSymbolizer();
257: Expression offset = filterFactory.property("testFloat");
258: Expression label = filterFactory.property("testByte");
259: textSymb.setLabelPlacement(styleFactory
260: .createLinePlacement(offset));
261: textSymb.setLabel(label);
262: textSymb.setFonts(new Font[] { font });
263:
264: Style s = createStyle();
265: s.getFeatureTypeStyles()[0].getRules()[0]
266: .setSymbolizers(new Symbolizer[] { textSymb });
267: assertAttributeName(s, new String[] { "testString",
268: "testString2", "testLong", "testBoolean", "testFloat",
269: "testByte" });
270: }
271:
272: public void testHalo() throws Exception {
273: Fill fill = styleFactory.getDefaultFill();
274: fill.setColor(filterFactory.property("testString"));
275:
276: Expression radius = filterFactory.property("testLong");
277: Halo halo = styleFactory.createHalo(fill, radius);
278: TextSymbolizer textSymb = styleFactory.createTextSymbolizer();
279: textSymb.setHalo(halo);
280:
281: Style s = createStyle();
282: s.getFeatureTypeStyles()[0].getRules()[0]
283: .setSymbolizers(new Symbolizer[] { textSymb });
284: assertAttributeName(s,
285: new String[] { "testString", "testLong" });
286: }
287:
288: public void testLinePlacement() throws Exception {
289: LinePlacement linePlacement = styleFactory
290: .createLinePlacement(filterFactory.property("testLong"));
291: TextSymbolizer textSymb = styleFactory.createTextSymbolizer();
292: textSymb.setLabelPlacement(linePlacement);
293:
294: Style s = createStyle();
295: s.getFeatureTypeStyles()[0].getRules()[0]
296: .setSymbolizers(new Symbolizer[] { textSymb });
297: assertAttributeName(s, new String[] { "testLong" });
298: }
299:
300: public void testPointPlacement() throws Exception {
301: PointPlacement pp = styleFactory.getDefaultPointPlacement();
302:
303: Expression x = filterFactory.property("testLong");
304: Expression y = filterFactory.property("testInteger");
305: AnchorPoint ap = styleFactory.createAnchorPoint(x, y);
306:
307: Expression dx = filterFactory.property("testFloat");
308: Expression dy = filterFactory.property("testDouble");
309: Displacement displacement = styleFactory.createDisplacement(dx,
310: dy);
311:
312: pp.setAnchorPoint(ap);
313: pp.setDisplacement(displacement);
314: pp.setRotation(filterFactory.property("testFloat"));
315:
316: TextSymbolizer textSymb = styleFactory.createTextSymbolizer();
317: textSymb.setLabelPlacement(pp);
318:
319: Style s = createStyle();
320: s.getFeatureTypeStyles()[0].getRules()[0]
321: .setSymbolizers(new Symbolizer[] { textSymb });
322: assertAttributeName(s, new String[] { "testLong",
323: "testInteger", "testFloat", "testDouble" });
324: }
325:
326: public void testPolygonSymbolizer() throws Exception {
327: PolygonSymbolizer ps = styleFactory.createPolygonSymbolizer();
328: Stroke stroke = styleFactory.getDefaultStroke();
329: stroke.setColor(filterFactory.property("testString"));
330:
331: Fill fill = styleFactory.getDefaultFill();
332: fill.setOpacity(filterFactory.property("testDouble"));
333: ps.setStroke(stroke);
334: ps.setFill(fill);
335:
336: Style s = createStyle();
337: s.getFeatureTypeStyles()[0].getRules()[0]
338: .setSymbolizers(new Symbolizer[] { ps });
339: assertAttributeName(s, new String[] { "testString",
340: "testDouble" });
341: }
342:
343: public void testLineSymbolizer() throws IllegalFilterException {
344: LineSymbolizer ls = styleFactory.createLineSymbolizer();
345: Stroke stroke = styleFactory.getDefaultStroke();
346: stroke.setColor(filterFactory.property("testString"));
347: ls.setStroke(stroke);
348:
349: Style s = createStyle();
350: s.getFeatureTypeStyles()[0].getRules()[0]
351: .setSymbolizers(new Symbolizer[] { ls });
352: assertAttributeName(s, new String[] { "testString" });
353: }
354:
355: public void testFill() throws IllegalFilterException {
356: Fill fill = styleFactory.getDefaultFill();
357: fill.setBackgroundColor(filterFactory.property("testString"));
358: fill.setColor(filterFactory.property("testString2"));
359:
360: Mark mark = styleFactory.createMark();
361: Expression le = filterFactory.literal(1);
362: Expression rot = filterFactory.property("testFloat");
363: Graphic graphic = styleFactory.createGraphic(null,
364: new Mark[] { mark }, null, le, le, rot);
365: fill.setGraphicFill(graphic);
366:
367: PolygonSymbolizer ps = styleFactory
368: .getDefaultPolygonSymbolizer();
369: ps.setFill(fill);
370:
371: Style s = createStyle();
372: s.getFeatureTypeStyles()[0].getRules()[0]
373: .setSymbolizers(new Symbolizer[] { ps });
374: assertAttributeName(s, new String[] { "testString",
375: "testString2", "testFloat" });
376: }
377:
378: public void testStroke() throws IllegalFilterException {
379: Stroke stroke = styleFactory.getDefaultStroke();
380: stroke.setColor(filterFactory.property("testString2"));
381: stroke.setDashOffset(filterFactory.property("testString"));
382:
383: Mark mark = styleFactory.createMark();
384: Expression le = filterFactory.literal(1);
385: Expression rot = filterFactory.property("testFloat");
386: Graphic graphic = styleFactory.createGraphic(null,
387: new Mark[] { mark }, null, le, le, rot);
388: stroke.setGraphicFill(graphic);
389:
390: LineSymbolizer ls = styleFactory.getDefaultLineSymbolizer();
391: ls.setStroke(stroke);
392:
393: Style s = createStyle();
394: s.getFeatureTypeStyles()[0].getRules()[0]
395: .setSymbolizers(new Symbolizer[] { ls });
396: assertAttributeName(s, new String[] { "testString",
397: "testString2", "testFloat" });
398: }
399:
400: /**
401: * Main for test runner.
402: *
403: * @param args DOCUMENT ME!
404: */
405: public static void main(String[] args) {
406: junit.textui.TestRunner.run(suite());
407: }
408:
409: /**
410: * Required suite builder.
411: *
412: * @return A test suite for this unit test.
413: */
414: public static Test suite() {
415: TestSuite suite = new TestSuite(
416: StyleAttributeExtractorTest.class);
417:
418: return suite;
419: }
420: }
|