package org.vfny.geoserver.wms.responses.map.htmlimagemap; import java.awt.Color; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.StringReader; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.logging.Logger; import junit.framework.TestCase; import org.geoserver.GeoServerResourceLoader; import org.geotools.data.DataSourceException; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFactorySpi; import org.geotools.data.DataUtilities; import org.geotools.data.DefaultQuery; import org.geotools.data.FeatureSource; import org.geotools.data.property.PropertyDataStore; import org.geotools.factory.FactoryFinder; import org.geotools.feature.FeatureType; import org.geotools.feature.SchemaException; import org.geotools.geometry.jts.JTS; import org.geotools.geometry.jts.ReferencedEnvelope; import org.geotools.map.DefaultMapLayer; import org.geotools.map.MapLayer; import org.geotools.referencing.CRS; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.geotools.renderer.style.Style2D; import org.geotools.resources.TestData; import org.geotools.styling.FeatureTypeStyle; import org.geotools.styling.Rule; import org.geotools.styling.SLDParser; import org.geotools.styling.Style; import org.geotools.styling.StyleFactory; import org.geotools.styling.StyleFactoryFinder; import org.geotools.filter.Filter; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.springframework.web.context.support.GenericWebApplicationContext; import org.vfny.geoserver.global.GeoserverDataDirectory; import org.vfny.geoserver.global.WMS; import org.vfny.geoserver.wms.GetMapProducer; import org.vfny.geoserver.wms.GetMapProducerFactorySpi; import org.vfny.geoserver.wms.WMSMapContext; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; /** * Test suite for HTMLImageMapMapProducer GetMapProducer * * @author Mauro Bartolomeoli */ public class HTMLImageMapTest extends TestCase { private static final StyleFactory sFac = StyleFactoryFinder.createStyleFactory(); private static final Logger LOGGER = Logger.getLogger(HTMLImageMapTest.class.getPackage().getName()); GetMapProducerFactorySpi mapFactory=null; GetMapProducer mapProducer=null; CoordinateReferenceSystem WGS84=null; DataStore testDS = null; int mapWidth=600; int mapHeight=600; public void setUp() throws Exception { // initializes GeoServer Resource Loading (is needed by some tests to not produce exceptions) File testdata=TestData.file(this, "."); System.setProperty("GEOSERVER_DATA_DIR", testdata.getAbsolutePath()); GeoServerResourceLoader loader = new GeoServerResourceLoader(testdata); GenericWebApplicationContext context = new GenericWebApplicationContext(); context.getBeanFactory().registerSingleton("resourceLoader", loader); GeoserverDataDirectory.init(context); // initialized WGS84 CRS (used by many tests) WGS84=CRS.decode("EPSG:4326"); testDS=getTestDataStore(); // initializes GetMapProducer factory and actual producer this.mapFactory = getProducerFactory(); this.mapProducer=getProducerInstance(); super.setUp(); } public void tearDown() throws Exception { this.mapFactory = null; this.mapProducer=null; super.tearDown(); } protected GetMapProducerFactorySpi getProducerFactory() { Iterator iter=FactoryFinder.factories(GetMapProducerFactorySpi.class); while(iter.hasNext()) { GetMapProducerFactorySpi factory=(GetMapProducerFactorySpi)iter.next(); if(factory.canProduce("text/html")) { return factory; } } return null; } protected GetMapProducer getProducerInstance() { if(mapFactory!=null) return mapFactory.createMapProducer("text/html", null); return null; } public void testGetMapProducerFactory() throws Exception { assertNotNull(mapFactory); } public void testGetMapProducer() throws Exception { assertNotNull(mapProducer); } public DataStore getTestDataStore() throws IOException { File testdata=TestData.file(this, "featureTypes"); return new MyPropertyDataStore(testdata); } protected Style getTestStyle(String styleName) throws Exception { SLDParser parser = new SLDParser(sFac); File styleRes=TestData.file(this, "styles/"+styleName); parser.setInput(styleRes); Style s = parser.readXML()[0]; return s; } protected void assertTestResult(String testName, GetMapProducer producer) { ByteArrayOutputStream out =null; StringBuffer testText=new StringBuffer(); try { out = new ByteArrayOutputStream(); producer.writeTo(out); out.flush(); out.close(); File testFile=TestData.file(this, "results/"+testName+".txt"); BufferedReader reader=new BufferedReader(new FileReader(testFile)); String s=null; while((s=reader.readLine())!=null) testText.append(s+"\n"); reader.close(); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } assertNotNull(out); assertTrue(out.size()>0); String s=new String(out.toByteArray()); assertEquals(s, testText.toString()); } protected void assertNotBlank(String testName, GetMapProducer producer) { ByteArrayOutputStream out =null; try { out = new ByteArrayOutputStream(); producer.writeTo(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } assertNotNull(out); assertTrue(out.size()>0); String s=new String(out.toByteArray()); System.out.println(s); } public void testMapProduceBasicPolygons() throws Exception { final FeatureSource fs = testDS.getFeatureSource("BasicPolygons"); final ReferencedEnvelope env = new ReferencedEnvelope(fs.getBounds(),WGS84); LOGGER.info("about to create map ctx for BasicPolygons with bounds " + env); final WMSMapContext map = new WMSMapContext(); map.setAreaOfInterest(env); map.setMapWidth(mapWidth); map.setMapHeight(mapHeight); map.setTransparent(false); Style basicStyle = getTestStyle("default.sld"); map.addLayer(fs, basicStyle); this.mapProducer.setOutputFormat("text/html"); this.mapProducer.setMapContext(map); this.mapProducer.produceMap(); assertTestResult("BasicPolygons", this.mapProducer); } public void testMapProduceReprojectAndLabel() throws Exception { final DataStore ds = getTestDataStore(); final FeatureSource fs = ds.getFeatureSource("NamedPlaces"); final Envelope env = fs.getBounds(); LOGGER.info("about to create map ctx for NamedPlaces with bounds " + env); final WMSMapContext map = new WMSMapContext(); CoordinateReferenceSystem sourceCrs=CRS.decode("EPSG:4326"); CoordinateReferenceSystem targetCrs=CRS.decode("EPSG:3004"); MathTransform transform=CRS.findMathTransform(sourceCrs, targetCrs,true); Envelope projEnv=JTS.transform(env, transform); ReferencedEnvelope refEnv=new ReferencedEnvelope(projEnv,targetCrs); map.setAreaOfInterest(refEnv); map.setMapWidth(mapWidth); map.setMapHeight(mapHeight); map.setBgColor(Color.red); map.setTransparent(false); map.setCoordinateReferenceSystem(targetCrs); Style basicStyle=getTestStyle("NamedPlaces.sld"); map.addLayer(fs, basicStyle); this.mapProducer.setOutputFormat("text/html"); this.mapProducer.setMapContext(map); this.mapProducer.produceMap(); assertTestResult("NamedPlaces", this.mapProducer); } public void testMapProduceLines() throws Exception { final FeatureSource fs = testDS.getFeatureSource("RoadSegments"); final ReferencedEnvelope env = new ReferencedEnvelope(fs.getBounds(),WGS84); LOGGER.info("about to create map ctx for RoadSegments with bounds " + env); final WMSMapContext map = new WMSMapContext(); map.setAreaOfInterest(env); map.setMapWidth(mapWidth); map.setMapHeight(mapHeight); map.setTransparent(false); Style basicStyle = getTestStyle("RoadSegments.sld"); map.addLayer(fs, basicStyle); this.mapProducer.setOutputFormat("text/html"); this.mapProducer.setMapContext(map); this.mapProducer.produceMap(); assertTestResult("RoadSegments", this.mapProducer); } public void testMapProducePoints() throws Exception { final FeatureSource fs = testDS.getFeatureSource("BuildingCenters"); final ReferencedEnvelope env = new ReferencedEnvelope(fs.getBounds(),WGS84); LOGGER.info("about to create map ctx for BuildingCenters with bounds " + env); final WMSMapContext map = new WMSMapContext(); map.setAreaOfInterest(env); map.setMapWidth(mapWidth); map.setMapHeight(mapHeight); map.setTransparent(false); Style basicStyle = getTestStyle("BuildingCenters.sld"); map.addLayer(fs, basicStyle); this.mapProducer.setOutputFormat("text/html"); this.mapProducer.setMapContext(map); this.mapProducer.produceMap(); assertTestResult("BuildingCenters", this.mapProducer); } public void testMapProduceMultiPoints() throws Exception { final FeatureSource fs = testDS.getFeatureSource("BuildingCentersMultiPoint"); final ReferencedEnvelope env = new ReferencedEnvelope(fs.getBounds(),WGS84); LOGGER.info("about to create map ctx for BuildingCentersMultiPoint with bounds " + env); final WMSMapContext map = new WMSMapContext(); map.setAreaOfInterest(env); map.setMapWidth(mapWidth); map.setMapHeight(mapHeight); map.setTransparent(false); Style basicStyle = getTestStyle("BuildingCenters.sld"); map.addLayer(fs, basicStyle); this.mapProducer.setOutputFormat("text/html"); this.mapProducer.setMapContext(map); this.mapProducer.produceMap(); assertTestResult("BuildingCentersMultiPoint", this.mapProducer); } public void testMapProduceCollection() throws Exception { final FeatureSource fs = testDS.getFeatureSource("CollectionSample"); final ReferencedEnvelope env = new ReferencedEnvelope(fs.getBounds(),WGS84); LOGGER.info("about to create map ctx for RoadSegments with bounds " + env); final WMSMapContext map = new WMSMapContext(); map.setAreaOfInterest(env); map.setMapWidth(mapWidth); map.setMapHeight(mapHeight); map.setTransparent(false); Style basicStyle = getTestStyle("CollectionSample.sld"); map.addLayer(fs, basicStyle); this.mapProducer.setOutputFormat("text/html"); this.mapProducer.setMapContext(map); this.mapProducer.produceMap(); assertTestResult("CollectionSample", this.mapProducer); } public static void main(String[] args) { junit.textui.TestRunner.run(HTMLImageMapTest.class); } static class MyPropertyDataStore extends PropertyDataStore { /** * Creates a new MyPropertyDataStore object. * * @param dir DOCUMENT ME! */ public MyPropertyDataStore(File dir) { super(dir); } /** * DOCUMENT ME! * * @param typeName DOCUMENT ME! * * @return DOCUMENT ME! * * @throws IOException DOCUMENT ME! * @throws DataSourceException DOCUMENT ME! */ public FeatureType getSchema(String typeName) throws IOException { FeatureType schema = super.getSchema(typeName); try { return DataUtilities.createSubType(schema, null, DefaultGeographicCRS.WGS84); } catch (SchemaException e) { throw new DataSourceException(e.getMessage(), e); } } } }