01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.template;
06:
07: import com.vividsolutions.jts.geom.Coordinate;
08: import com.vividsolutions.jts.geom.GeometryFactory;
09: import freemarker.template.Configuration;
10: import freemarker.template.SimpleObjectWrapper;
11: import freemarker.template.Template;
12: import junit.framework.TestCase;
13: import org.geotools.data.DataUtilities;
14: import org.geotools.feature.DefaultFeature;
15: import org.geotools.feature.DefaultFeatureCollection;
16: import org.geotools.feature.DefaultFeatureType;
17: import org.geotools.feature.Feature;
18: import org.geotools.feature.FeatureCollection;
19: import org.geotools.feature.FeatureType;
20: import java.io.OutputStreamWriter;
21: import java.io.StringWriter;
22: import java.io.Writer;
23:
24: public class FeatureWrapperTest extends TestCase {
25: FeatureCollection features;
26: Configuration cfg;
27:
28: protected void setUp() throws Exception {
29: super .setUp();
30:
31: //create some data
32: GeometryFactory gf = new GeometryFactory();
33: FeatureType featureType = DataUtilities.createType("testType",
34: "string:String,int:Integer,double:Double,geom:Point");
35:
36: features = new DefaultFeatureCollection(null, null) {
37: };
38: features
39: .add(new DefaultFeature(
40: (DefaultFeatureType) featureType, new Object[] {
41: "one", new Integer(1), new Double(1.1),
42: gf.createPoint(new Coordinate(1, 1)) },
43: "fid.1") {
44: });
45: features
46: .add(new DefaultFeature(
47: (DefaultFeatureType) featureType, new Object[] {
48: "two", new Integer(2), new Double(2.2),
49: gf.createPoint(new Coordinate(2, 2)) },
50: "fid.2") {
51: });
52: features
53: .add(new DefaultFeature(
54: (DefaultFeatureType) featureType, new Object[] {
55: "three", new Integer(3),
56: new Double(3.3),
57: gf.createPoint(new Coordinate(3, 3)) },
58: "fid.3") {
59: });
60:
61: cfg = new Configuration();
62: cfg.setClassForTemplateLoading(getClass(), "");
63: cfg.setObjectWrapper(new FeatureWrapper());
64: }
65:
66: public void testFeatureCollection() throws Exception {
67: Template template = cfg.getTemplate("FeatureCollection.ftl");
68:
69: StringWriter out = new StringWriter();
70: template.process(features, out);
71: assertEquals("fid.1\nfid.2\nfid.3\n", out.toString());
72: }
73:
74: public void testFeatureSimple() throws Exception {
75: Template template = cfg.getTemplate("FeatureSimple.ftl");
76:
77: StringWriter out = new StringWriter();
78: template.process(features.iterator().next(), out);
79:
80: //replace ',' with '.' for locales which use a comma for decimal point
81: assertEquals("one\n1\n1.1\nPOINT (1 1)", out.toString()
82: .replace(',', '.'));
83: }
84:
85: public void testFeatureDynamic() throws Exception {
86: Template template = cfg.getTemplate("FeatureDynamic.ftl");
87:
88: StringWriter out = new StringWriter();
89: template.process(features.iterator().next(), out);
90:
91: //replace ',' with '.' for locales which use a comma for decimal point
92: assertEquals(
93: "string=one\nint=1\ndouble=1.1\ngeom=POINT (1 1)\n",
94: out.toString().replace(',', '.'));
95: }
96: }
|