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.map.kml;
006:
007: import com.mockrunner.mock.web.MockHttpServletRequest;
008: import com.vividsolutions.jts.geom.Envelope;
009: import org.geoserver.data.test.MockData;
010: import org.geoserver.util.ReaderUtils;
011: import org.geoserver.wms.WMSTestSupport;
012: import org.geotools.data.FeatureSource;
013: import org.geotools.map.DefaultMapLayer;
014: import org.geotools.map.MapLayer;
015: import org.geotools.referencing.CRS;
016: import org.geotools.styling.Style;
017: import org.vfny.geoserver.config.FeatureTypeConfig;
018: import org.vfny.geoserver.global.Data;
019: import org.vfny.geoserver.global.FeatureTypeInfo;
020: import org.vfny.geoserver.global.MapLayerInfo;
021: import org.vfny.geoserver.global.dto.FeatureTypeInfoDTO;
022: import org.vfny.geoserver.wms.WMSMapContext;
023: import org.vfny.geoserver.wms.requests.GetMapRequest;
024: import org.vfny.geoserver.wms.servlets.GetMap;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.NodeList;
028:
029: import java.io.ByteArrayInputStream;
030: import java.io.ByteArrayOutputStream;
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033:
034: public class KMLTransformerTest extends WMSTestSupport {
035: WMSMapContext mapContext;
036: MapLayer mapLayer;
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040:
041: mapLayer = createMapLayer(MockData.BASIC_POLYGONS);
042:
043: mapContext = new WMSMapContext(
044: createGetMapRequest(MockData.BASIC_POLYGONS));
045: mapContext.addLayer(mapLayer);
046: }
047:
048: public void testVectorTransformer() throws Exception {
049: KMLVectorTransformer transformer = new KMLVectorTransformer(
050: mapContext, mapLayer);
051: transformer.setIndentation(2);
052:
053: FeatureSource featureSource = mapLayer.getFeatureSource();
054: int nfeatures = featureSource.getFeatures().size();
055:
056: ByteArrayOutputStream output = new ByteArrayOutputStream();
057: transformer.transform(featureSource.getFeatures(), output);
058:
059: DocumentBuilder docBuilder = DocumentBuilderFactory
060: .newInstance().newDocumentBuilder();
061: Document document = docBuilder.parse(new ByteArrayInputStream(
062: output.toByteArray()));
063:
064: Element element = document.getDocumentElement();
065: assertEquals("kml", element.getNodeName());
066: assertEquals(nfeatures, element.getElementsByTagName("Style")
067: .getLength());
068: assertEquals(nfeatures, element.getElementsByTagName(
069: "Placemark").getLength());
070: }
071:
072: //JD: commented out because there is no way to specify a coordinate reference
073: // system in property datastores.
074: // public void testReprojection() throws Exception {
075: // KMLTransformer transformer = new KMLTransformer();
076: // transformer.setIndentation(2);
077: //
078: // ByteArrayOutputStream output = new ByteArrayOutputStream();
079: // transformer.transform(mapContext, output);
080: // transformer.transform(mapContext,System.out);
081: // DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
082: // Document doc1 = docBuilder.parse(new ByteArrayInputStream(output.toByteArray()));
083: //
084: // mapContext.setCoordinateReferenceSystem(CRS.decode("EPSG:3005"));
085: // output = new ByteArrayOutputStream();
086: // transformer.transform(mapContext, output);
087: // transformer.transform(mapContext,System.out);
088: // Document doc2 = docBuilder.parse(new ByteArrayInputStream(output.toByteArray()));
089: //
090: // NodeList docs1 = doc1.getDocumentElement().getElementsByTagName("Document");
091: // NodeList docs2 = doc2.getDocumentElement().getElementsByTagName("Document");
092: //
093: // assertEquals( docs1.getLength(), docs2.getLength() );
094: // for ( int i = 0; i < docs1.getLength(); i++ ) {
095: // Element e1 = (Element) docs1.item(i);
096: // Element e2 = (Element) docs2.item(i);
097: //
098: // String name1 = ReaderUtils.getChildText( e1, "name" );
099: // String name2 = ReaderUtils.getChildText( e2, "name" );
100: //
101: // assertEquals( name1, name2 );
102: //
103: // Element p1 = (Element) e1.getElementsByTagName("Placemark").item(0);
104: // Element p2 = (Element) e2.getElementsByTagName("Placemark").item(0);
105: //
106: // Element poly1 = (Element) p1.getElementsByTagName("Polygon").item(0);
107: // Element poly2 = (Element) p2.getElementsByTagName("Polygon").item(0);
108: //
109: // Element c1 = (Element) poly1.getElementsByTagName("coordinates").item(0);
110: // Element c2 = (Element) poly2.getElementsByTagName("coordinates").item(0);
111: //
112: // assertFalse(c1.getFirstChild().getNodeValue().equals( c2.getFirstChild().getNodeValue()));
113: // }
114: //
115: // }
116:
117: public void testRasterTransformerInline() throws Exception {
118: KMLRasterTransformer transformer = new KMLRasterTransformer(
119: mapContext);
120: transformer.setInline(true);
121:
122: ByteArrayOutputStream output = new ByteArrayOutputStream();
123: transformer.transform(mapLayer, output);
124:
125: DocumentBuilder docBuilder = DocumentBuilderFactory
126: .newInstance().newDocumentBuilder();
127: Document document = docBuilder.parse(new ByteArrayInputStream(
128: output.toByteArray()));
129:
130: assertEquals("kml", document.getDocumentElement().getNodeName());
131:
132: assertEquals(mapContext.getLayerCount(), document
133: .getElementsByTagName("Folder").getLength());
134: assertEquals(mapContext.getLayerCount(), document
135: .getElementsByTagName("GroundOverlay").getLength());
136:
137: assertEquals(mapContext.getLayerCount(), document
138: .getElementsByTagName("href").getLength());
139:
140: Element href = (Element) document.getElementsByTagName("href")
141: .item(0);
142: assertEquals("layer_0.png", href.getFirstChild().getNodeValue());
143: }
144:
145: public void testRasterTransformerNotInline() throws Exception {
146: KMLRasterTransformer transformer = new KMLRasterTransformer(
147: mapContext);
148: transformer.setInline(false);
149:
150: ByteArrayOutputStream output = new ByteArrayOutputStream();
151: transformer.transform(mapLayer, output);
152:
153: DocumentBuilder docBuilder = DocumentBuilderFactory
154: .newInstance().newDocumentBuilder();
155: Document document = docBuilder.parse(new ByteArrayInputStream(
156: output.toByteArray()));
157:
158: assertEquals("kml", document.getDocumentElement().getNodeName());
159:
160: assertEquals(mapContext.getLayerCount(), document
161: .getElementsByTagName("Folder").getLength());
162: assertEquals(mapContext.getLayerCount(), document
163: .getElementsByTagName("GroundOverlay").getLength());
164:
165: assertEquals(mapContext.getLayerCount(), document
166: .getElementsByTagName("href").getLength());
167:
168: Element href = (Element) document.getElementsByTagName("href")
169: .item(0);
170: assertTrue(href.getFirstChild().getNodeValue().startsWith(
171: "http://localhost"));
172: }
173:
174: public void testSuperOverlayTransformer() throws Exception {
175: KMLSuperOverlayTransformer transformer = new KMLSuperOverlayTransformer(
176: mapContext);
177: transformer.setIndentation(2);
178:
179: mapContext.setAreaOfInterest(new Envelope(-180, 180, -90, 90));
180:
181: ByteArrayOutputStream output = new ByteArrayOutputStream();
182: transformer.transform(mapLayer, output);
183:
184: DocumentBuilder docBuilder = DocumentBuilderFactory
185: .newInstance().newDocumentBuilder();
186: Document document = docBuilder.parse(new ByteArrayInputStream(
187: output.toByteArray()));
188:
189: assertEquals("kml", document.getDocumentElement().getNodeName());
190: assertEquals(3, document.getElementsByTagName("Region")
191: .getLength());
192: assertEquals(2, document.getElementsByTagName("NetworkLink")
193: .getLength());
194: assertEquals(2, document.getElementsByTagName("GroundOverlay")
195: .getLength());
196:
197: }
198:
199: public void testTransformer() throws Exception {
200: KMLTransformer transformer = new KMLTransformer();
201:
202: ByteArrayOutputStream output = new ByteArrayOutputStream();
203: transformer.transform(mapContext, output);
204:
205: DocumentBuilder docBuilder = DocumentBuilderFactory
206: .newInstance().newDocumentBuilder();
207: Document document = docBuilder.parse(new ByteArrayInputStream(
208: output.toByteArray()));
209:
210: assertEquals("kml", document.getDocumentElement().getNodeName());
211: }
212: }
|