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.Collections;
019:
020: import junit.framework.TestCase;
021:
022: import org.geotools.factory.CommonFactoryFinder;
023: import org.geotools.filter.FilterFactoryFinder;
024: import org.opengis.filter.FilterFactory;
025: import org.opengis.filter.expression.Expression;
026: import org.opengis.util.Cloneable;
027:
028: /**
029: * Tests style cloning
030: *
031: * @author Sean Geoghegan
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/styling/StyleObjectTest.java $
033: */
034: public class StyleObjectTest extends TestCase {
035: private StyleFactory styleFactory;
036: private FilterFactory filterFactory;
037:
038: /**
039: * Constructor for StyleCloneTest.
040: *
041: * @param arg0
042: */
043: public StyleObjectTest(String arg0) {
044: super (arg0);
045: }
046:
047: /*
048: * @see TestCase#setUp()
049: */
050: protected void setUp() throws Exception {
051: styleFactory = StyleFactoryFinder.createStyleFactory();
052: filterFactory = CommonFactoryFinder.getFilterFactory(null);
053: }
054:
055: /*
056: * @see TestCase#tearDown()
057: */
058: protected void tearDown() throws Exception {
059: styleFactory = null;
060: }
061:
062: public void testStyle() throws Exception {
063: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
064: fts.setFeatureTypeName("feature-type-1");
065:
066: FeatureTypeStyle fts2 = fts2();
067:
068: Style style = styleFactory.getDefaultStyle();
069: style.addFeatureTypeStyle(fts);
070: style.addFeatureTypeStyle(fts2);
071:
072: Style clone = (Style) ((Cloneable) style).clone();
073: assertClone(style, clone);
074:
075: Style notEq = styleFactory.getDefaultStyle();
076:
077: fts2 = fts2();
078: notEq.addFeatureTypeStyle(fts2);
079: assertEqualsContract(clone, notEq, style);
080: }
081:
082: private FeatureTypeStyle fts2() {
083: FeatureTypeStyle fts2 = styleFactory.createFeatureTypeStyle();
084: Rule rule = styleFactory.createRule();
085: fts2.addRule(rule);
086: fts2.setFeatureTypeName("feature-type-2");
087:
088: return fts2;
089: }
090:
091: public void testFeatureTypeStyle() throws Exception {
092: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
093: fts.setFeatureTypeName("feature-type");
094:
095: Rule rule1;
096:
097: rule1 = styleFactory.createRule();
098: rule1.setName("rule1");
099: rule1.setFilter(filterFactory.id(Collections
100: .singleton(filterFactory.featureId("FID"))));
101:
102: Rule rule2 = styleFactory.createRule();
103: rule2.setIsElseFilter(true);
104: rule2.setName("rule2");
105: fts.addRule(rule1);
106: fts.addRule(rule2);
107:
108: FeatureTypeStyle clone = (FeatureTypeStyle) ((Cloneable) fts)
109: .clone();
110: assertClone(fts, clone);
111:
112: rule1 = styleFactory.createRule();
113: rule1.setName("rule1");
114: rule1.setFilter(filterFactory.id(Collections
115: .singleton(filterFactory.featureId("FID"))));
116:
117: FeatureTypeStyle notEq = styleFactory.createFeatureTypeStyle();
118: notEq.setName("fts-not-equal");
119: notEq.addRule(rule1);
120: assertEqualsContract(clone, notEq, fts);
121: }
122:
123: public void testRule() throws Exception {
124: Symbolizer symb1 = styleFactory.createLineSymbolizer(
125: styleFactory.getDefaultStroke(), "geometry");
126:
127: Symbolizer symb2 = styleFactory.createPolygonSymbolizer(
128: styleFactory.getDefaultStroke(), styleFactory
129: .getDefaultFill(), "shape");
130:
131: Rule rule = styleFactory.createRule();
132: rule.setSymbolizers(new Symbolizer[] { symb1, symb2 });
133:
134: Rule clone = (Rule) ((Cloneable) rule).clone();
135: assertClone(rule, clone);
136:
137: symb2 = styleFactory.createPolygonSymbolizer(styleFactory
138: .getDefaultStroke(), styleFactory.getDefaultFill(),
139: "shape");
140:
141: Rule notEq = styleFactory.createRule();
142: notEq.setSymbolizers(new Symbolizer[] { symb2 });
143: assertEqualsContract(clone, notEq, rule);
144:
145: symb1 = styleFactory.createLineSymbolizer(styleFactory
146: .getDefaultStroke(), "geometry");
147: clone.setSymbolizers(new Symbolizer[] { symb1 });
148: assertTrue(!rule.equals(clone));
149: }
150:
151: public void testPointSymbolizer() throws Exception {
152: PointSymbolizer pointSymb = styleFactory
153: .createPointSymbolizer();
154: PointSymbolizer clone = (PointSymbolizer) ((Cloneable) pointSymb)
155: .clone();
156: assertClone(pointSymb, clone);
157:
158: PointSymbolizer notEq = styleFactory
159: .getDefaultPointSymbolizer();
160: notEq.setGeometryPropertyName("something_else");
161: assertEqualsContract(clone, notEq, pointSymb);
162: }
163:
164: public void testTextSymbolizer() {
165: TextSymbolizer textSymb = styleFactory.createTextSymbolizer();
166: Expression offset = filterFactory.literal(10);
167: textSymb.setLabelPlacement(styleFactory
168: .createLinePlacement(offset));
169:
170: TextSymbolizer clone = (TextSymbolizer) ((Cloneable) textSymb)
171: .clone();
172: assertClone(textSymb, clone);
173:
174: TextSymbolizer notEq = styleFactory.getDefaultTextSymbolizer();
175: Expression ancX = filterFactory.literal(10);
176: Expression ancY = filterFactory.literal(10);
177: AnchorPoint ancPoint = styleFactory.createAnchorPoint(ancX,
178: ancY);
179: LabelPlacement placement = styleFactory.createPointPlacement(
180: ancPoint, null, null);
181: notEq.setLabelPlacement(placement);
182: assertEqualsContract(clone, notEq, textSymb);
183: }
184:
185: public void testFont() {
186: Font font = styleFactory.getDefaultFont();
187: Font clone = (Font) ((Cloneable) font).clone();
188: assertClone(font, clone);
189:
190: Font other = styleFactory.createFont(filterFactory
191: .literal("other"), filterFactory.literal("normal"),
192: filterFactory.literal("BOLD"), filterFactory
193: .literal(12));
194:
195: assertEqualsContract(clone, other, font);
196: }
197:
198: public void testHalo() {
199: Halo halo = styleFactory.createHalo(styleFactory
200: .getDefaultFill(), filterFactory.literal(10));
201: Halo clone = (Halo) ((Cloneable) halo).clone();
202: assertClone(halo, clone);
203:
204: Halo other = styleFactory.createHalo(styleFactory
205: .getDefaultFill(), filterFactory.literal(12));
206: assertEqualsContract(clone, other, halo);
207: }
208:
209: public void testLinePlacement() throws Exception {
210: LinePlacement linePlacement = styleFactory
211: .createLinePlacement(filterFactory.literal(12));
212: LinePlacement clone = (LinePlacement) ((Cloneable) linePlacement)
213: .clone();
214: assertClone(linePlacement, clone);
215:
216: LinePlacement other = styleFactory
217: .createLinePlacement(filterFactory.property("NAME"));
218: assertEqualsContract(clone, other, linePlacement);
219: }
220:
221: public void testAnchorPoint() {
222: AnchorPoint anchorPoint = styleFactory.createAnchorPoint(
223: filterFactory.literal(1), filterFactory.literal(2));
224: AnchorPoint clone = (AnchorPoint) ((Cloneable) anchorPoint)
225: .clone();
226: assertClone(anchorPoint, clone);
227:
228: AnchorPoint other = styleFactory.createAnchorPoint(
229: filterFactory.literal(3), filterFactory.literal(4));
230: assertEqualsContract(clone, other, anchorPoint);
231: }
232:
233: public void testDisplacement() {
234: Displacement displacement = styleFactory.createDisplacement(
235: filterFactory.literal(1), filterFactory.literal(2));
236: Displacement clone = (Displacement) ((Cloneable) displacement)
237: .clone();
238: assertClone(displacement, clone);
239:
240: Displacement other = styleFactory.createDisplacement(
241: filterFactory.literal(3), filterFactory.literal(4));
242: assertEqualsContract(clone, other, displacement);
243: }
244:
245: public void testPointPlacement() {
246: PointPlacement pointPl = styleFactory
247: .getDefaultPointPlacement();
248: PointPlacement clone = (PointPlacement) ((Cloneable) pointPl)
249: .clone();
250: assertClone(pointPl, clone);
251:
252: PointPlacement other = (PointPlacement) ((Cloneable) pointPl)
253: .clone();
254: other.setRotation(filterFactory.literal(274.0));
255: assertEqualsContract(clone, other, pointPl);
256: }
257:
258: public void testPolygonSymbolizer() {
259: PolygonSymbolizer polygonSymb = styleFactory
260: .createPolygonSymbolizer();
261: PolygonSymbolizer clone = (PolygonSymbolizer) ((Cloneable) polygonSymb)
262: .clone();
263: assertClone(polygonSymb, clone);
264:
265: PolygonSymbolizer notEq = styleFactory
266: .getDefaultPolygonSymbolizer();
267: notEq.setGeometryPropertyName("something_else");
268: assertEqualsContract(clone, notEq, polygonSymb);
269: }
270:
271: public void testLineSymbolizer() {
272: LineSymbolizer lineSymb = styleFactory.createLineSymbolizer();
273: LineSymbolizer clone = (LineSymbolizer) ((Cloneable) lineSymb)
274: .clone();
275: assertClone(lineSymb, clone);
276:
277: LineSymbolizer notEq = styleFactory.getDefaultLineSymbolizer();
278: notEq.setGeometryPropertyName("something_else");
279: assertEqualsContract(clone, notEq, lineSymb);
280: }
281:
282: public void testGraphic() {
283: Graphic graphic = styleFactory.getDefaultGraphic();
284: graphic.addMark(styleFactory.getDefaultMark());
285:
286: Graphic clone = (Graphic) ((Cloneable) graphic).clone();
287: assertClone(graphic, clone);
288: assertEqualsContract(clone, graphic);
289: assertEquals(clone.getSymbols().length,
290: graphic.getSymbols().length);
291:
292: Graphic notEq = styleFactory.getDefaultGraphic();
293: notEq.setGeometryPropertyName("geomprop");
294: assertEqualsContract(clone, notEq, graphic);
295: }
296:
297: public void testExternalGraphic() {
298: ExternalGraphic exGraphic = styleFactory.createExternalGraphic(
299: "http://somewhere", "image/png");
300: ExternalGraphic clone = (ExternalGraphic) ((Cloneable) exGraphic)
301: .clone();
302: assertClone(exGraphic, clone);
303:
304: ExternalGraphic notEq = styleFactory.createExternalGraphic(
305: "http://somewhereelse", "image/jpeg");
306: assertEqualsContract(clone, notEq, exGraphic);
307:
308: // make sure it works for different format, same url
309: ExternalGraphic notEq2 = (ExternalGraphic) ((Cloneable) clone)
310: .clone();
311: notEq2.setFormat("image/jpeg");
312: assertEqualsContract(clone, notEq2, exGraphic);
313: }
314:
315: public void testMark() {
316: Mark mark = styleFactory.getCircleMark();
317: Mark clone = (Mark) ((Cloneable) mark).clone();
318: assertClone(mark, clone);
319:
320: Mark notEq = styleFactory.getStarMark();
321: assertEqualsContract(clone, notEq, mark);
322: }
323:
324: public void testFill() {
325: Fill fill = styleFactory.getDefaultFill();
326: Fill clone = (Fill) ((Cloneable) fill).clone();
327: assertClone(fill, clone);
328:
329: Fill notEq = styleFactory.createFill(filterFactory
330: .literal("#FF0000"));
331: assertEqualsContract(clone, notEq, fill);
332: }
333:
334: public void testStroke() {
335: Stroke stroke = styleFactory.getDefaultStroke();
336: Stroke clone = (Stroke) stroke.clone();
337: assertClone(stroke, clone);
338:
339: Stroke notEq = styleFactory.createStroke(filterFactory
340: .literal("#FF0000"), filterFactory.literal(10));
341: assertEqualsContract(clone, notEq, stroke);
342:
343: // a stroke is a complex object with lots of properties,
344: // need more extensive tests here.
345: Stroke dashArray = styleFactory.getDefaultStroke();
346: dashArray.setDashArray(new float[] { 1.0f, 2.0f, 3.0f });
347:
348: Stroke dashArray2 = (Stroke) dashArray.clone();
349: assertEqualsContract(dashArray, dashArray2);
350: }
351:
352: private static void assertClone(Object real, Object clone) {
353: assertNotNull("Real was null", real);
354: assertNotNull("Clone was null", clone);
355: assertTrue("" + real.getClass().getName() + " was not cloned",
356: real != clone);
357: }
358:
359: private static void assertEqualsContract(Object controlEqual,
360: Object controlNe, Object test) {
361: assertNotNull(controlEqual);
362: assertNotNull(controlNe);
363: assertNotNull(test);
364:
365: // check reflexivity
366: assertTrue("Reflexivity test failed", test.equals(test));
367:
368: // check symmetric
369: assertTrue("Symmetry test failed", controlEqual.equals(test));
370: assertTrue("Symmetry test failed", test.equals(controlEqual));
371: assertTrue("Symmetry test failed", !test.equals(controlNe));
372: assertTrue("Symmetry test failed", !controlNe.equals(test));
373:
374: // check transitivity
375: assertTrue("Transitivity test failed", !controlEqual
376: .equals(controlNe));
377: assertTrue("Transitivity test failed", !test.equals(controlNe));
378: assertTrue("Transitivity test failed", !controlNe
379: .equals(controlEqual));
380: assertTrue("Transitivity test failed", !controlNe.equals(test));
381:
382: // check non-null
383: assertTrue("Non-null test failed", !test.equals(null));
384:
385: // assertHashcode equality
386: int controlEqHash = controlEqual.hashCode();
387: int testHash = test.hashCode();
388: assertTrue("Equal objects should return equal hashcodes",
389: controlEqHash == testHash);
390: }
391:
392: private static void assertEqualsContract(Object controlEqual,
393: Object test) {
394: assertNotNull(controlEqual);
395: assertNotNull(test);
396:
397: // check reflexivity
398: assertTrue("Reflexivity test failed", test.equals(test));
399:
400: // check symmetric
401: assertTrue("Symmetry test failed", controlEqual.equals(test));
402: assertTrue("Symmetry test failed", test.equals(controlEqual));
403:
404: // check non-null
405: assertTrue("Non-null test failed", !test.equals(null));
406:
407: // assertHashcode equality
408: int controlEqHash = controlEqual.hashCode();
409: int testHash = test.hashCode();
410: assertTrue("Equal objects should return equal hashcodes",
411: controlEqHash == testHash);
412: }
413: }
|