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.vfny.geoserver.wms.responses.map.openlayers;
06:
07: import freemarker.ext.beans.BeansWrapper;
08: import freemarker.template.Configuration;
09: import freemarker.template.Template;
10: import junit.framework.TestCase;
11: import org.geoserver.data.test.MockData;
12: import org.geoserver.wms.WMSTestSupport;
13: import org.vfny.geoserver.wms.WMSMapContext;
14: import org.vfny.geoserver.wms.requests.GetMapRequest;
15: import org.vfny.geoserver.wms.responses.map.openlayers.OpenLayersMapProducer;
16: import org.w3c.dom.Document;
17: import org.xml.sax.EntityResolver;
18: import org.xml.sax.InputSource;
19: import org.xml.sax.SAXException;
20:
21: import java.io.ByteArrayInputStream;
22: import java.io.ByteArrayOutputStream;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.OutputStreamWriter;
26: import java.io.StringReader;
27: import java.util.ArrayList;
28: import java.util.HashMap;
29: import javax.xml.parsers.DocumentBuilder;
30: import javax.xml.parsers.DocumentBuilderFactory;
31:
32: public class OpenLayersMapTemplateTest extends WMSTestSupport {
33: public void test() throws Exception {
34: Configuration cfg = new Configuration();
35: cfg.setClassForTemplateLoading(OpenLayersMapProducer.class, "");
36: cfg.setObjectWrapper(new BeansWrapper());
37:
38: Template template = cfg
39: .getTemplate("OpenLayersMapTemplate.ftl");
40: assertNotNull(template);
41:
42: GetMapRequest request = createGetMapRequest(MockData.BASIC_POLYGONS);
43: WMSMapContext mapContext = new WMSMapContext();
44: mapContext.addLayer(createMapLayer(MockData.BASIC_POLYGONS));
45: mapContext.setRequest(request);
46: mapContext.setMapWidth(256);
47: mapContext.setMapHeight(256);
48:
49: ByteArrayOutputStream output = new ByteArrayOutputStream();
50: HashMap map = new HashMap();
51: map.put("context", mapContext);
52: map.put("request", mapContext.getRequest());
53: map.put("maxResolution", new Double(0.0005)); // just a random number
54: map.put("baseUrl", "http://localhost:8080/geoserver/wms");
55: map.put("parameters", new ArrayList());
56: map.put("layerName", "layer");
57: map.put("units", "degrees");
58: template.process(map, new OutputStreamWriter(output));
59:
60: DocumentBuilderFactory dbf = DocumentBuilderFactory
61: .newInstance();
62: dbf.setValidating(false);
63: dbf.setExpandEntityReferences(false);
64:
65: DocumentBuilder docBuilder = dbf.newDocumentBuilder();
66: docBuilder.setEntityResolver(new EntityResolver() {
67:
68: public InputSource resolveEntity(String publicId,
69: String systemId) throws SAXException, IOException {
70: StringReader reader = new StringReader(
71: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
72: InputSource source = new InputSource(reader);
73: source.setPublicId(publicId);
74: source.setSystemId(systemId);
75: return source;
76: }
77: });
78:
79: Document document = docBuilder.parse(new ByteArrayInputStream(
80: output.toByteArray()));
81: assertNotNull(document);
82:
83: assertEquals("html", document.getDocumentElement()
84: .getNodeName());
85: }
86: }
|