package org.geotools.util;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;

import junit.framework.TestCase;

public class GeometryConverterFactoryTest extends TestCase {

	GeometryConverterFactory factory;
	
	protected void setUp() throws Exception {
		factory = new GeometryConverterFactory();
	}
	
	public void testEnvelopeToGeometry() throws Exception {
		Geometry geometry = (Geometry) factory.createConverter( Envelope.class, Geometry.class, null )
			.convert( new Envelope( new Coordinate( 0,0 ), new Coordinate( 1,1 ) ), Geometry.class );
		assertNotNull( geometry );
		assertTrue(
			new GeometryFactory().createPolygon(
				new GeometryFactory().createLinearRing( 
					new Coordinate[] {
						new Coordinate(0,0), new Coordinate(1,0),new Coordinate(1,1), 
						new Coordinate(0,1), new Coordinate(0,0)
					}
				), null
			).equals( geometry )	
		);
		
	}
	
	public void testGeometryToEnvelope() throws Exception {
		Envelope envelope = (Envelope) factory.createConverter( Geometry.class, Envelope.class, null ).convert( 
			new GeometryFactory().createPolygon(
				new GeometryFactory().createLinearRing( 
					new Coordinate[] {
						new Coordinate(0,0), new Coordinate(1,0),new Coordinate(1,1), 
						new Coordinate(0,1), new Coordinate(0,0)
					}
				), null
			), Envelope.class
		);
		
		assertEquals( new Envelope( new Coordinate( 0,0 ), new Coordinate( 1,1 ) ), envelope );
	}
	
	public void testStringToGeometry()  throws Exception {
		Geometry geometry = (Geometry) factory.createConverter( String.class, Geometry.class, null )
			.convert( "POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", Geometry.class );
		assertNotNull( geometry );
		assertTrue(
			new GeometryFactory().createPolygon(
				new GeometryFactory().createLinearRing( 
					new Coordinate[] {
						new Coordinate(0,0), new Coordinate(1,0),new Coordinate(1,1), 
						new Coordinate(0,1), new Coordinate(0,0)
					}
				), null
			).equals( geometry )	
		);
	}
	
	public void testGeometryToString() throws Exception {
		String wkt = (String) factory.createConverter( Geometry.class, String.class, null ).convert( 
			new GeometryFactory().createPolygon(
				new GeometryFactory().createLinearRing( 
					new Coordinate[] {
						new Coordinate(0,0), new Coordinate(1,0),new Coordinate(1,1), 
						new Coordinate(0,1), new Coordinate(0,0)
					}
				), null
			), String.class
		);
		
		assertEquals( "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", wkt );
	}
}
